mirror of
https://github.com/TandoorRecipes/recipes.git
synced 2026-01-07 07:08:03 -05:00
moved to category relation instead of category
This commit is contained in:
@@ -33,13 +33,13 @@
|
|||||||
:move="trackMovedItem"
|
:move="trackMovedItem"
|
||||||
>
|
>
|
||||||
<template #item="{element}">
|
<template #item="{element}">
|
||||||
<v-list-item border :key="element.id">
|
<v-list-item border :key="element.category.id">
|
||||||
<template #prepend>
|
<template #prepend>
|
||||||
<v-icon class="drag-handle cursor-grab" icon="$dragHandle"></v-icon>
|
<v-icon class="drag-handle cursor-grab" icon="$dragHandle"></v-icon>
|
||||||
</template>
|
</template>
|
||||||
{{ element.name }}
|
{{ element.category.name }}
|
||||||
<template #append>
|
<template #append>
|
||||||
<v-btn color="create" size="small" @click="addCategory(element)">
|
<v-btn color="create" size="small" @click="addCategoryRelation(element)">
|
||||||
<i class="fa-solid fa-plus"></i>
|
<i class="fa-solid fa-plus"></i>
|
||||||
</v-btn>
|
</v-btn>
|
||||||
</template>
|
</template>
|
||||||
@@ -48,7 +48,8 @@
|
|||||||
<template #footer>
|
<template #footer>
|
||||||
<v-list-item class="cursor-pointer" border prepend-icon="$create" variant="tonal" base-color="create">
|
<v-list-item class="cursor-pointer" border prepend-icon="$create" variant="tonal" base-color="create">
|
||||||
{{ $t('New_Supermarket_Category') }}
|
{{ $t('New_Supermarket_Category') }}
|
||||||
<model-edit-dialog model="SupermarketCategory" @create="args => supermarketCategories.push(args)"></model-edit-dialog>
|
<model-edit-dialog model="SupermarketCategory"
|
||||||
|
@create="(args: SupermarketCategory) => supermarketCategories.push(args)"></model-edit-dialog>
|
||||||
</v-list-item>
|
</v-list-item>
|
||||||
</template>
|
</template>
|
||||||
</draggable>
|
</draggable>
|
||||||
@@ -58,23 +59,24 @@
|
|||||||
<h3> {{ $t('SelectedCategories') }} </h3>
|
<h3> {{ $t('SelectedCategories') }} </h3>
|
||||||
<draggable
|
<draggable
|
||||||
tag="VList"
|
tag="VList"
|
||||||
v-model="editingObjSupermarketCategories"
|
v-model="editingObjectSupermarketCategoriesRelations"
|
||||||
handle=".drag-handle" item-key="id" group="categories"
|
handle=".drag-handle" item-key="id" group="categories"
|
||||||
:empty-insert-threshold="20"
|
:empty-insert-threshold="20"
|
||||||
@sort="updateEditingObjectSupermarketCategoryRelation"
|
@sort="sortCategoryRelations()"
|
||||||
:move="trackMovedItem"
|
:move="trackMovedItem"
|
||||||
@add="addCategory(lastMovedCategory)"
|
@add="addCategoryRelation(lastMovedCategoryRelation)"
|
||||||
@remove="removeCategory(lastMovedCategory)"
|
@remove="removeCategoryRelation(lastMovedCategoryRelation)"
|
||||||
>
|
>
|
||||||
<template #item="{element}">
|
<template #item="{element}">
|
||||||
<v-list-item border :key="element.id">
|
<v-list-item border :key="element.category.id">
|
||||||
<template #prepend>
|
<template #prepend>
|
||||||
<v-icon class="drag-handle" icon="$dragHandle"></v-icon>
|
<v-icon class="drag-handle" icon="$dragHandle"></v-icon>
|
||||||
</template>
|
</template>
|
||||||
{{ element.name }}
|
{{ element.category.name }}
|
||||||
|
<v-chip>{{ element.order }}</v-chip>
|
||||||
|
|
||||||
<template #append>
|
<template #append>
|
||||||
<v-btn color="warning" size="small" @click="removeCategory(element)">
|
<v-btn color="warning" size="small" @click="removeCategoryRelation(element)">
|
||||||
<i class="fa-solid fa-minus"></i>
|
<i class="fa-solid fa-minus"></i>
|
||||||
</v-btn>
|
</v-btn>
|
||||||
</template>
|
</template>
|
||||||
@@ -124,15 +126,21 @@ const supermarketCategories = ref([] as SupermarketCategory[])
|
|||||||
// all relations existing on the editing obj (internal datastructure, UI works with computed/manual list of categories)
|
// all relations existing on the editing obj (internal datastructure, UI works with computed/manual list of categories)
|
||||||
const editingObjectSupermarketCategoriesRelations = ref([] as SupermarketCategoryRelation[])
|
const editingObjectSupermarketCategoriesRelations = ref([] as SupermarketCategoryRelation[])
|
||||||
|
|
||||||
// categories used by the selected supermarket
|
|
||||||
const editingObjSupermarketCategories = ref([] as SupermarketCategory[])
|
|
||||||
|
|
||||||
// track the last moved item in the draggable list to use in add/remove functions
|
// track the last moved item in the draggable list to use in add/remove functions
|
||||||
const lastMovedCategory = ref({} as SupermarketCategory)
|
const lastMovedCategoryRelation = ref({} as SupermarketCategoryRelation)
|
||||||
|
|
||||||
|
// variable to prevent sorting while some other operation is currently running that would conflict / would make sorting unnecessary (e.g. adding item)
|
||||||
|
const preventSort = ref(false)
|
||||||
|
|
||||||
// computed: categories not yet used in editing supermarket
|
// computed: categories not yet used in editing supermarket
|
||||||
const unusedSupermarketCategories = computed(() => {
|
const unusedSupermarketCategories = computed(() => {
|
||||||
return supermarketCategories.value.filter(e => editingObjSupermarketCategories.value.findIndex(sc => sc.id == e.id) == -1)
|
let sCR = [] as SupermarketCategoryRelation[]
|
||||||
|
supermarketCategories.value.forEach((sc) => {
|
||||||
|
if (editingObjectSupermarketCategoriesRelations.value.findIndex(e => e.category.id == sc.id) == -1) {
|
||||||
|
sCR.push({supermarket: editingObj.value.id, category: sc, order: 0,} as SupermarketCategoryRelation)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return sCR
|
||||||
})
|
})
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
@@ -145,78 +153,101 @@ onMounted(() => {
|
|||||||
existingItemFunction: () => {
|
existingItemFunction: () => {
|
||||||
// initialize list of supermarket category relations
|
// initialize list of supermarket category relations
|
||||||
editingObjectSupermarketCategoriesRelations.value = editingObj.value.categoryToSupermarket
|
editingObjectSupermarketCategoriesRelations.value = editingObj.value.categoryToSupermarket
|
||||||
|
|
||||||
// load categories from relation list
|
|
||||||
editingObjectSupermarketCategoriesRelations.value.forEach(cTS => {
|
|
||||||
editingObjSupermarketCategories.value.push(cTS.category)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
function updateEditingObjectSupermarketCategoryRelation(operation: any) {
|
|
||||||
if (operation.to == operation.from) {
|
|
||||||
console.log('sort', operation)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* called whenever something in the list is moved to track the last moved element (to be used in add/remove functions)
|
* called whenever something in the list is moved to track the last moved element (to be used in add/remove functions)
|
||||||
* @param operation
|
* @param operation
|
||||||
*/
|
*/
|
||||||
function trackMovedItem(operation: any) {
|
function trackMovedItem(operation: any) {
|
||||||
lastMovedCategory.value = operation.draggedContext.element
|
lastMovedCategoryRelation.value = operation.draggedContext.element
|
||||||
console.log('LAST MOVED', lastMovedCategory.value)
|
}
|
||||||
|
|
||||||
|
function sortCategoryRelations(startIndex: number = 0) {
|
||||||
|
const api = new ApiApi()
|
||||||
|
console.log('sort called start index: ', startIndex)
|
||||||
|
|
||||||
|
if (!preventSort.value) {
|
||||||
|
editingObjectSupermarketCategoriesRelations.value.forEach((sc, index) => {
|
||||||
|
console.log('sorting ', index, startIndex)
|
||||||
|
if (index >= startIndex) {
|
||||||
|
if (index == 0) {
|
||||||
|
sc.order = 0
|
||||||
|
} else {
|
||||||
|
sc.order = editingObjectSupermarketCategoriesRelations.value[index - 1].order! + 1
|
||||||
|
}
|
||||||
|
api.apiSupermarketCategoryRelationUpdate({id: sc.id!, supermarketCategoryRelation: sc}).catch((err: any) => {
|
||||||
|
useMessageStore().addError(ErrorMessageType.UPDATE_ERROR, err)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* create supermarket category relation from given category and save it
|
* add category relation to client list (if not already done by draggable) and to the server
|
||||||
* @param sc SupermarketCategory to create relation from
|
* if required re-order existing client items so newly inserted items stays at correct order
|
||||||
|
* @param sCR SupermarketCategoryRelation to add
|
||||||
*/
|
*/
|
||||||
function addCategory(sc: SupermarketCategory) {
|
function addCategoryRelation(sCR: SupermarketCategoryRelation) {
|
||||||
let api = new ApiApi()
|
let api = new ApiApi()
|
||||||
|
|
||||||
let sC = {
|
preventSort.value = true
|
||||||
category: sc,
|
|
||||||
order: editingObjSupermarketCategories.value.length + 1,
|
|
||||||
supermarket: editingObj.value.id,
|
|
||||||
} as SupermarketCategoryRelation
|
|
||||||
|
|
||||||
api.apiSupermarketCategoryRelationCreate({supermarketCategoryRelation: sC}).then(r => {
|
let relationIndex = editingObjectSupermarketCategoriesRelations.value.findIndex(e => e.category.id == sCR.category.id)
|
||||||
editingObjectSupermarketCategoriesRelations.value.push(r)
|
if (relationIndex != -1) {
|
||||||
|
if (relationIndex != 0 && editingObjectSupermarketCategoriesRelations.value[relationIndex - 1]) {
|
||||||
// if the category has not been added already (by draggable) add it to the end of the list
|
sCR.order = editingObjectSupermarketCategoriesRelations.value[relationIndex - 1].order! + 1
|
||||||
if(editingObjSupermarketCategories.value.findIndex(e => e.id == sc.id) == -1){
|
} else {
|
||||||
editingObjSupermarketCategories.value.push(sc)
|
sCR.order = 0
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// item will be added last to list so give it the highest order
|
||||||
|
sCR.order = editingObjectSupermarketCategoriesRelations.value[editingObjectSupermarketCategoriesRelations.value.length - 1].order! + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
api.apiSupermarketCategoryRelationCreate({supermarketCategoryRelation: sCR}).then(r => {
|
||||||
|
|
||||||
|
// add to / update in client list
|
||||||
|
if (relationIndex != -1) {
|
||||||
|
// replace with sCR from API response
|
||||||
|
editingObjectSupermarketCategoriesRelations.value.splice(relationIndex, 1, r)
|
||||||
|
} else {
|
||||||
|
// add new to end of the list
|
||||||
|
editingObjectSupermarketCategoriesRelations.value.push(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
preventSort.value = false
|
||||||
|
// TODO reorder existing items after index
|
||||||
|
|
||||||
}).catch((err: any) => {
|
}).catch((err: any) => {
|
||||||
useMessageStore().addError(ErrorMessageType.CREATE_ERROR, err)
|
useMessageStore().addError(ErrorMessageType.CREATE_ERROR, err)
|
||||||
|
preventSort.value = false
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* remove category from supermarket category relation
|
* remove SupermarketCategoryRelation from client list (if not already done by draggable) and from server
|
||||||
* finds relation by looking through relation list for given category
|
* @param sCR SupermarketCategoryRelation to remove
|
||||||
* @param sc SupermarketCategory to remove from relation
|
|
||||||
*/
|
*/
|
||||||
function removeCategory(sc: SupermarketCategory) {
|
function removeCategoryRelation(sCR: SupermarketCategoryRelation) {
|
||||||
let api = new ApiApi()
|
let api = new ApiApi()
|
||||||
|
|
||||||
// find relation and delete it / remove it from frontend list
|
// remove from client list
|
||||||
let relationIndex = editingObjectSupermarketCategoriesRelations.value.findIndex(e => e.category.id == sc.id)
|
let relationIndex = editingObjectSupermarketCategoriesRelations.value.findIndex(e => e.category.id == sCR.category.id)
|
||||||
if (relationIndex > -1) {
|
if (relationIndex != -1) {
|
||||||
api.apiSupermarketCategoryRelationDestroy({id: editingObjectSupermarketCategoriesRelations.value[relationIndex].id!}).then(r => {
|
|
||||||
editingObjectSupermarketCategoriesRelations.value.splice(relationIndex, 1)
|
editingObjectSupermarketCategoriesRelations.value.splice(relationIndex, 1)
|
||||||
|
}
|
||||||
|
|
||||||
// remove category from list (might have already been done by draggable)
|
// delete on server if ID is present (should always be)
|
||||||
editingObjSupermarketCategories.value = editingObjSupermarketCategories.value.filter(e => e.id != sc.id)
|
if (sCR.id) {
|
||||||
}).catch((err: any) => {
|
api.apiSupermarketCategoryRelationDestroy({id: sCR.id}).catch((err: any) => {
|
||||||
useMessageStore().addError(ErrorMessageType.DELETE_ERROR, err)
|
useMessageStore().addError(ErrorMessageType.DELETE_ERROR, err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|||||||
Reference in New Issue
Block a user