2022-01-09 07:24:34 +00:00
|
|
|
<template>
|
2022-01-17 00:24:24 +00:00
|
|
|
<v-container v-if="shoppingList" class="md-container">
|
2022-01-09 07:24:34 +00:00
|
|
|
<BasePageTitle divider>
|
|
|
|
<template #header>
|
|
|
|
<v-img max-height="100" max-width="100" :src="require('~/static/svgs/shopping-cart.svg')"></v-img>
|
|
|
|
</template>
|
|
|
|
<template #title> {{ shoppingList.name }} </template>
|
|
|
|
</BasePageTitle>
|
2022-01-17 00:24:24 +00:00
|
|
|
|
2022-01-09 07:24:34 +00:00
|
|
|
<!-- Viewer -->
|
|
|
|
<section v-if="!edit" class="py-2">
|
|
|
|
<div v-if="!byLabel">
|
2023-01-08 17:23:24 +00:00
|
|
|
<draggable :value="shoppingList.listItems" handle=".handle" @start="loadingCounter += 1" @end="loadingCounter -= 1" @input="updateIndex">
|
2022-01-17 00:24:24 +00:00
|
|
|
<v-lazy v-for="(item, index) in listItems.unchecked" :key="item.id">
|
|
|
|
<ShoppingListItem
|
|
|
|
v-model="listItems.unchecked[index]"
|
|
|
|
class="my-2 my-sm-0"
|
2022-10-22 04:35:45 +00:00
|
|
|
:labels="allLabels || []"
|
2022-01-17 00:24:24 +00:00
|
|
|
:units="allUnits || []"
|
|
|
|
:foods="allFoods || []"
|
2023-01-08 17:23:24 +00:00
|
|
|
@checked="saveListItem"
|
|
|
|
@save="saveListItem"
|
2022-01-17 00:24:24 +00:00
|
|
|
@delete="deleteListItem(item)"
|
|
|
|
/>
|
|
|
|
</v-lazy>
|
2022-01-09 07:24:34 +00:00
|
|
|
</draggable>
|
|
|
|
</div>
|
2022-01-17 00:24:24 +00:00
|
|
|
|
|
|
|
<!-- View By Label -->
|
2022-01-09 07:24:34 +00:00
|
|
|
<div v-else>
|
|
|
|
<div v-for="(value, key) in itemsByLabel" :key="key" class="mb-6">
|
|
|
|
<div @click="toggleShowChecked()">
|
|
|
|
<span>
|
|
|
|
<v-icon>
|
|
|
|
{{ $globals.icons.tags }}
|
|
|
|
</v-icon>
|
|
|
|
</span>
|
|
|
|
{{ key }}
|
|
|
|
</div>
|
2022-01-17 00:24:24 +00:00
|
|
|
<v-lazy v-for="(item, index) in value" :key="item.id">
|
|
|
|
<ShoppingListItem
|
|
|
|
v-model="value[index]"
|
2022-10-22 04:35:45 +00:00
|
|
|
:labels="allLabels || []"
|
2022-01-17 00:24:24 +00:00
|
|
|
:units="allUnits || []"
|
|
|
|
:foods="allFoods || []"
|
2023-01-08 17:23:24 +00:00
|
|
|
@checked="saveListItem"
|
|
|
|
@save="saveListItem"
|
2022-01-17 00:24:24 +00:00
|
|
|
@delete="deleteListItem(item)"
|
|
|
|
/>
|
|
|
|
</v-lazy>
|
2022-01-09 07:24:34 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
<!-- Create Item -->
|
|
|
|
<div v-if="createEditorOpen">
|
|
|
|
<ShoppingListItemEditor
|
|
|
|
v-model="createListItemData"
|
|
|
|
class="my-4"
|
2022-10-22 04:35:45 +00:00
|
|
|
:labels="allLabels || []"
|
2022-01-17 00:24:24 +00:00
|
|
|
:units="allUnits || []"
|
|
|
|
:foods="allFoods || []"
|
|
|
|
@delete="createEditorOpen = false"
|
|
|
|
@cancel="createEditorOpen = false"
|
|
|
|
@save="createListItem"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div v-else class="mt-4 d-flex justify-end">
|
|
|
|
<BaseButton create @click="createEditorOpen = true" />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<!-- Action Bar -->
|
|
|
|
<div class="d-flex justify-end mb-4 mt-2">
|
|
|
|
<BaseButtonGroup
|
|
|
|
:buttons="[
|
|
|
|
{
|
|
|
|
icon: $globals.icons.contentCopy,
|
|
|
|
text: '',
|
|
|
|
event: 'edit',
|
|
|
|
children: [
|
|
|
|
{
|
|
|
|
icon: $globals.icons.contentCopy,
|
|
|
|
text: 'Copy as Text',
|
|
|
|
event: 'copy-plain',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: $globals.icons.contentCopy,
|
|
|
|
text: 'Copy as Markdown',
|
|
|
|
event: 'copy-markdown',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: $globals.icons.delete,
|
|
|
|
text: 'Delete Checked',
|
|
|
|
event: 'delete',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: $globals.icons.tags,
|
|
|
|
text: 'Toggle Label Sort',
|
|
|
|
event: 'sort-by-labels',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
icon: $globals.icons.checkboxBlankOutline,
|
|
|
|
text: 'Uncheck All Items',
|
|
|
|
event: 'uncheck',
|
|
|
|
},
|
|
|
|
]"
|
|
|
|
@edit="edit = true"
|
|
|
|
@delete="deleteChecked"
|
|
|
|
@uncheck="uncheckAll"
|
|
|
|
@sort-by-labels="sortByLabels"
|
|
|
|
@copy-plain="copyListItems('plain')"
|
|
|
|
@copy-markdown="copyListItems('markdown')"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<!-- Checked Items -->
|
2022-01-09 07:24:34 +00:00
|
|
|
<div v-if="listItems.checked && listItems.checked.length > 0" class="mt-6">
|
|
|
|
<button @click="toggleShowChecked()">
|
|
|
|
<span>
|
|
|
|
<v-icon>
|
|
|
|
{{ showChecked ? $globals.icons.chevronDown : $globals.icons.chevronRight }}
|
|
|
|
</v-icon>
|
|
|
|
</span>
|
|
|
|
{{ listItems.checked ? listItems.checked.length : 0 }} items checked
|
|
|
|
</button>
|
|
|
|
<v-divider class="my-4"></v-divider>
|
|
|
|
<v-expand-transition>
|
|
|
|
<div v-show="showChecked">
|
2022-01-17 00:24:24 +00:00
|
|
|
<div v-for="(item, idx) in listItems.checked" :key="item.id">
|
|
|
|
<ShoppingListItem
|
|
|
|
v-model="listItems.checked[idx]"
|
|
|
|
class="strike-through-note"
|
|
|
|
:labels="allLabels"
|
|
|
|
:units="allUnits || []"
|
|
|
|
:foods="allFoods || []"
|
2023-01-08 17:23:24 +00:00
|
|
|
@checked="saveListItem"
|
|
|
|
@save="saveListItem"
|
2022-01-17 00:24:24 +00:00
|
|
|
@delete="deleteListItem(item)"
|
|
|
|
/>
|
2022-01-09 07:24:34 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</v-expand-transition>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
<!-- Recipe References -->
|
|
|
|
<v-lazy v-if="shoppingList.recipeReferences && shoppingList.recipeReferences.length > 0">
|
|
|
|
<section>
|
|
|
|
<div>
|
|
|
|
<span>
|
|
|
|
<v-icon left class="mb-1">
|
|
|
|
{{ $globals.icons.primary }}
|
|
|
|
</v-icon>
|
|
|
|
</span>
|
|
|
|
{{ shoppingList.recipeReferences ? shoppingList.recipeReferences.length : 0 }} Linked Recipes
|
2022-01-09 07:24:34 +00:00
|
|
|
</div>
|
2022-01-17 00:24:24 +00:00
|
|
|
<v-divider class="my-4"></v-divider>
|
|
|
|
<RecipeList :recipes="listRecipes">
|
|
|
|
<template v-for="(recipe, index) in listRecipes" #[`actions-${recipe.id}`]>
|
|
|
|
<v-list-item-action :key="'item-actions-decrease' + recipe.id">
|
|
|
|
<v-btn icon @click.prevent="removeRecipeReferenceToList(recipe.id)">
|
|
|
|
<v-icon color="grey lighten-1">{{ $globals.icons.minus }}</v-icon>
|
|
|
|
</v-btn>
|
|
|
|
</v-list-item-action>
|
|
|
|
<div :key="'item-actions-quantity' + recipe.id" class="pl-3">
|
|
|
|
{{ shoppingList.recipeReferences[index].recipeQuantity }}
|
|
|
|
</div>
|
|
|
|
<v-list-item-action :key="'item-actions-increase' + recipe.id">
|
|
|
|
<v-btn icon @click.prevent="addRecipeReferenceToList(recipe.id)">
|
|
|
|
<v-icon color="grey lighten-1">{{ $globals.icons.createAlt }}</v-icon>
|
|
|
|
</v-btn>
|
|
|
|
</v-list-item-action>
|
|
|
|
</template>
|
|
|
|
</RecipeList>
|
|
|
|
</section>
|
|
|
|
</v-lazy>
|
|
|
|
|
|
|
|
<v-lazy>
|
|
|
|
<div class="d-flex justify-end mt-10">
|
2022-03-19 19:31:17 +00:00
|
|
|
<ButtonLink to="/group/data/labels" text="Manage Labels" :icon="$globals.icons.tags" />
|
2022-01-17 00:24:24 +00:00
|
|
|
</div>
|
|
|
|
</v-lazy>
|
2022-01-09 07:24:34 +00:00
|
|
|
</v-container>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
|
|
|
import draggable from "vuedraggable";
|
|
|
|
|
2022-11-11 00:17:49 +00:00
|
|
|
import { defineComponent, useAsync, useRoute, computed, ref, watch, onUnmounted } from "@nuxtjs/composition-api";
|
|
|
|
import { useIdle, useToggle } from "@vueuse/core";
|
2022-01-17 00:24:24 +00:00
|
|
|
import { useCopyList } from "~/composables/use-copy";
|
2022-01-09 07:24:34 +00:00
|
|
|
import { useUserApi } from "~/composables/api";
|
2022-01-17 00:24:24 +00:00
|
|
|
import { useAsyncKey } from "~/composables/use-utils";
|
2022-01-09 07:24:34 +00:00
|
|
|
import ShoppingListItem from "~/components/Domain/ShoppingList/ShoppingListItem.vue";
|
2022-10-22 19:51:07 +00:00
|
|
|
import { ShoppingListItemCreate, ShoppingListItemOut } from "~/lib/api/types/group";
|
2022-01-17 00:24:24 +00:00
|
|
|
import RecipeList from "~/components/Domain/Recipe/RecipeList.vue";
|
|
|
|
import ShoppingListItemEditor from "~/components/Domain/ShoppingList/ShoppingListItemEditor.vue";
|
|
|
|
import { getDisplayText } from "~/composables/use-display-text";
|
2022-06-25 19:39:38 +00:00
|
|
|
import { useFoodStore, useLabelStore, useUnitStore } from "~/composables/store";
|
2022-01-17 00:24:24 +00:00
|
|
|
|
2022-01-09 07:24:34 +00:00
|
|
|
type CopyTypes = "plain" | "markdown";
|
|
|
|
|
|
|
|
interface PresentLabel {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export default defineComponent({
|
|
|
|
components: {
|
|
|
|
draggable,
|
|
|
|
ShoppingListItem,
|
2022-01-17 00:24:24 +00:00
|
|
|
RecipeList,
|
|
|
|
ShoppingListItemEditor,
|
2022-01-09 07:24:34 +00:00
|
|
|
},
|
|
|
|
setup() {
|
2022-11-11 00:17:49 +00:00
|
|
|
const { idle } = useIdle(5 * 60 * 1000) // 5 minutes
|
2023-01-08 17:23:24 +00:00
|
|
|
const loadingCounter = ref(1);
|
|
|
|
const recipeReferenceLoading = ref(false);
|
2022-01-09 07:24:34 +00:00
|
|
|
const userApi = useUserApi();
|
|
|
|
|
|
|
|
const edit = ref(false);
|
|
|
|
const byLabel = ref(false);
|
|
|
|
|
|
|
|
const route = useRoute();
|
|
|
|
const id = route.value.params.id;
|
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
// ===============================================================
|
|
|
|
// Shopping List Actions
|
|
|
|
|
2022-01-09 07:24:34 +00:00
|
|
|
const shoppingList = useAsync(async () => {
|
|
|
|
return await fetchShoppingList();
|
|
|
|
}, useAsyncKey());
|
|
|
|
|
|
|
|
async function fetchShoppingList() {
|
|
|
|
const { data } = await userApi.shopping.lists.getOne(id);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function refresh() {
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value += 1;
|
|
|
|
const newListValue = await fetchShoppingList();
|
|
|
|
loadingCounter.value -= 1;
|
|
|
|
|
|
|
|
// only update the list with the new value if we're not loading, to prevent UI jitter
|
|
|
|
if (!loadingCounter.value) {
|
|
|
|
shoppingList.value = newListValue;
|
|
|
|
}
|
2022-01-09 07:24:34 +00:00
|
|
|
}
|
|
|
|
|
2022-11-11 00:17:49 +00:00
|
|
|
// constantly polls for changes
|
|
|
|
async function pollForChanges() {
|
2022-12-01 05:29:27 +00:00
|
|
|
// pause polling if the user isn't active or we're busy
|
2023-01-08 17:23:24 +00:00
|
|
|
if (idle.value || loadingCounter.value) {
|
2022-11-11 00:17:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await refresh();
|
|
|
|
|
|
|
|
if (shoppingList.value) {
|
|
|
|
attempts = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the refresh was unsuccessful, the shopping list will be null, so we increment the attempt counter
|
|
|
|
attempts ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (error) {
|
|
|
|
attempts ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we hit too many errors, stop polling
|
|
|
|
if (attempts >= maxAttempts) {
|
|
|
|
clearInterval(pollTimer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// start polling
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value -= 1;
|
2022-11-11 00:17:49 +00:00
|
|
|
const pollFrequency = 5000;
|
|
|
|
|
|
|
|
let attempts = 0;
|
|
|
|
const maxAttempts = 3;
|
|
|
|
|
|
|
|
const pollTimer: ReturnType<typeof setInterval> = setInterval(() => { pollForChanges() }, pollFrequency);
|
|
|
|
onUnmounted(() => {
|
|
|
|
clearInterval(pollTimer);
|
|
|
|
});
|
|
|
|
|
2022-01-09 07:24:34 +00:00
|
|
|
// =====================================
|
2022-01-17 00:24:24 +00:00
|
|
|
// List Item CRUD
|
2022-01-09 07:24:34 +00:00
|
|
|
|
|
|
|
const listItems = computed(() => {
|
|
|
|
return {
|
2022-01-17 00:24:24 +00:00
|
|
|
checked: shoppingList.value?.listItems?.filter((item) => item.checked) ?? [],
|
|
|
|
unchecked: shoppingList.value?.listItems?.filter((item) => !item.checked) ?? [],
|
2022-01-09 07:24:34 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const [showChecked, toggleShowChecked] = useToggle(false);
|
|
|
|
|
|
|
|
// =====================================
|
|
|
|
// Copy List Items
|
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
const copy = useCopyList();
|
2022-01-09 07:24:34 +00:00
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
function copyListItems(copyType: CopyTypes) {
|
|
|
|
const items = shoppingList.value?.listItems?.filter((item) => !item.checked);
|
2022-01-09 07:24:34 +00:00
|
|
|
|
|
|
|
if (!items) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
const text = items.map((itm) => getDisplayText(itm.note, itm.quantity, itm.food, itm.unit));
|
2022-01-09 07:24:34 +00:00
|
|
|
|
|
|
|
switch (copyType) {
|
|
|
|
case "markdown":
|
2022-01-17 00:24:24 +00:00
|
|
|
copy.copyMarkdownCheckList(text);
|
2022-01-09 07:24:34 +00:00
|
|
|
break;
|
|
|
|
default:
|
2022-01-17 00:24:24 +00:00
|
|
|
copy.copyPlain(text);
|
2022-01-09 07:24:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// =====================================
|
|
|
|
// Check / Uncheck All
|
|
|
|
|
|
|
|
function uncheckAll() {
|
|
|
|
let hasChanged = false;
|
2022-01-17 00:24:24 +00:00
|
|
|
shoppingList.value?.listItems?.forEach((item) => {
|
2022-01-09 07:24:34 +00:00
|
|
|
if (item.checked) {
|
|
|
|
hasChanged = true;
|
|
|
|
item.checked = false;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (hasChanged) {
|
2022-01-17 00:24:24 +00:00
|
|
|
updateListItems();
|
2022-01-09 07:24:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function deleteChecked() {
|
2022-01-17 00:24:24 +00:00
|
|
|
const checked = shoppingList.value?.listItems?.filter((item) => item.checked);
|
2022-01-09 07:24:34 +00:00
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
if (!checked || checked?.length === 0) {
|
2022-01-09 07:24:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value += 1;
|
2022-01-17 00:24:24 +00:00
|
|
|
deleteListItems(checked);
|
2022-01-09 07:24:34 +00:00
|
|
|
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value -= 1;
|
2022-01-17 00:24:24 +00:00
|
|
|
refresh();
|
2022-01-09 07:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// =====================================
|
|
|
|
// List Item Context Menu
|
|
|
|
|
|
|
|
const contextActions = {
|
|
|
|
delete: "delete",
|
|
|
|
setIngredient: "setIngredient",
|
|
|
|
};
|
|
|
|
|
|
|
|
const contextMenu = [
|
|
|
|
{ title: "Delete", action: contextActions.delete },
|
|
|
|
{ title: "Ingredient", action: contextActions.setIngredient },
|
|
|
|
];
|
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
function contextMenuAction(action: string, item: ShoppingListItemOut, idx: number) {
|
2022-01-09 07:24:34 +00:00
|
|
|
if (!shoppingList.value?.listItems) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (action) {
|
|
|
|
case contextActions.delete:
|
|
|
|
shoppingList.value.listItems = shoppingList.value?.listItems.filter((itm) => itm.id !== item.id);
|
|
|
|
break;
|
|
|
|
case contextActions.setIngredient:
|
|
|
|
shoppingList.value.listItems[idx].isFood = !shoppingList.value.listItems[idx].isFood;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// =====================================
|
2022-01-17 00:24:24 +00:00
|
|
|
// Labels, Units, Foods
|
|
|
|
// TODO: Extract to Composable
|
|
|
|
|
2022-06-25 19:39:38 +00:00
|
|
|
const { labels: allLabels } = useLabelStore();
|
|
|
|
const { units: allUnits } = useUnitStore();
|
|
|
|
const { foods: allFoods } = useFoodStore();
|
2022-01-09 07:24:34 +00:00
|
|
|
|
|
|
|
function sortByLabels() {
|
|
|
|
byLabel.value = !byLabel.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
const presentLabels = computed(() => {
|
|
|
|
const labels: PresentLabel[] = [];
|
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
shoppingList.value?.listItems?.forEach((item) => {
|
|
|
|
if (item.labelId && item.label) {
|
2022-01-09 07:24:34 +00:00
|
|
|
labels.push({
|
|
|
|
name: item.label.name,
|
|
|
|
id: item.labelId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return labels;
|
|
|
|
});
|
|
|
|
|
2022-06-02 17:12:05 +00:00
|
|
|
const itemsByLabel = ref<{ [key: string]: ShoppingListItemOut[] }>({});
|
|
|
|
|
|
|
|
function updateItemsByLabel() {
|
|
|
|
const items: { [prop: string]: ShoppingListItemOut[] } = {};
|
2022-01-09 07:24:34 +00:00
|
|
|
|
|
|
|
const noLabel = {
|
2022-06-02 17:12:05 +00:00
|
|
|
"No Label": [] as ShoppingListItemOut[],
|
2022-01-09 07:24:34 +00:00
|
|
|
};
|
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
shoppingList.value?.listItems?.forEach((item) => {
|
|
|
|
if (item.checked) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-09 07:24:34 +00:00
|
|
|
if (item.labelId) {
|
|
|
|
if (item.label && item.label.name in items) {
|
|
|
|
items[item.label.name].push(item);
|
|
|
|
} else if (item.label) {
|
|
|
|
items[item.label.name] = [item];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
noLabel["No Label"].push(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (noLabel["No Label"].length > 0) {
|
|
|
|
items["No Label"] = noLabel["No Label"];
|
|
|
|
}
|
|
|
|
|
2022-06-02 17:12:05 +00:00
|
|
|
itemsByLabel.value = items;
|
|
|
|
}
|
|
|
|
|
|
|
|
watch(shoppingList, () => {
|
|
|
|
updateItemsByLabel();
|
2022-01-09 07:24:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
async function refreshLabels() {
|
|
|
|
const { data } = await userApi.multiPurposeLabels.getAll();
|
2022-06-25 19:39:38 +00:00
|
|
|
|
|
|
|
if (data) {
|
|
|
|
allLabels.value = data.items ?? [];
|
|
|
|
}
|
2022-01-09 07:24:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
refreshLabels();
|
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
// =====================================
|
|
|
|
// Add/Remove Recipe References
|
|
|
|
|
|
|
|
const listRecipes = computed<Array<any>>(() => {
|
|
|
|
return shoppingList.value?.recipeReferences?.map((ref) => ref.recipe) ?? [];
|
|
|
|
});
|
|
|
|
|
2022-02-13 21:23:42 +00:00
|
|
|
async function addRecipeReferenceToList(recipeId: string) {
|
2023-01-08 17:23:24 +00:00
|
|
|
if (!shoppingList.value || recipeReferenceLoading.value) {
|
2022-01-17 00:24:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value += 1;
|
|
|
|
recipeReferenceLoading.value = true;
|
2022-01-17 00:24:24 +00:00
|
|
|
const { data } = await userApi.shopping.lists.addRecipe(shoppingList.value.id, recipeId);
|
2023-01-08 17:23:24 +00:00
|
|
|
recipeReferenceLoading.value = false;
|
|
|
|
loadingCounter.value -= 1;
|
2022-01-17 00:24:24 +00:00
|
|
|
|
|
|
|
if (data) {
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 21:23:42 +00:00
|
|
|
async function removeRecipeReferenceToList(recipeId: string) {
|
2023-01-08 17:23:24 +00:00
|
|
|
if (!shoppingList.value || recipeReferenceLoading.value) {
|
2022-01-17 00:24:24 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value += 1;
|
|
|
|
recipeReferenceLoading.value = true;
|
2022-01-17 00:24:24 +00:00
|
|
|
const { data } = await userApi.shopping.lists.removeRecipe(shoppingList.value.id, recipeId);
|
2023-01-08 17:23:24 +00:00
|
|
|
recipeReferenceLoading.value = false;
|
|
|
|
loadingCounter.value -= 1;
|
2022-01-17 00:24:24 +00:00
|
|
|
|
|
|
|
if (data) {
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// =====================================
|
|
|
|
// List Item CRUD
|
|
|
|
|
2022-10-22 04:35:45 +00:00
|
|
|
/*
|
|
|
|
* saveListItem updates and update on the backend server. Additionally, if the item is
|
|
|
|
* checked it will also append that item to the end of the list so that the unchecked items
|
|
|
|
* are at the top of the list.
|
|
|
|
*/
|
2022-01-17 00:24:24 +00:00
|
|
|
async function saveListItem(item: ShoppingListItemOut) {
|
|
|
|
if (!shoppingList.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value += 1;
|
2022-10-22 04:35:45 +00:00
|
|
|
if (item.checked && shoppingList.value.listItems) {
|
|
|
|
const lst = shoppingList.value.listItems.filter((itm) => itm.id !== item.id);
|
|
|
|
lst.push(item);
|
|
|
|
updateIndex(lst);
|
|
|
|
}
|
|
|
|
|
2022-01-17 00:24:24 +00:00
|
|
|
const { data } = await userApi.shopping.items.updateOne(item.id, item);
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value -= 1;
|
2022-01-17 00:24:24 +00:00
|
|
|
|
|
|
|
if (data) {
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteListItem(item: ShoppingListItemOut) {
|
|
|
|
if (!shoppingList.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value += 1;
|
2022-01-17 00:24:24 +00:00
|
|
|
const { data } = await userApi.shopping.items.deleteOne(item.id);
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value -= 1;
|
2022-01-17 00:24:24 +00:00
|
|
|
|
|
|
|
if (data) {
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// =====================================
|
|
|
|
// Create New Item
|
|
|
|
|
|
|
|
const createEditorOpen = ref(false);
|
|
|
|
const createListItemData = ref<ShoppingListItemCreate>(ingredientResetFactory());
|
|
|
|
|
|
|
|
function ingredientResetFactory(): ShoppingListItemCreate {
|
|
|
|
return {
|
|
|
|
shoppingListId: id,
|
|
|
|
checked: false,
|
|
|
|
position: shoppingList.value?.listItems?.length || 1,
|
|
|
|
isFood: false,
|
|
|
|
quantity: 1,
|
|
|
|
note: "",
|
|
|
|
unit: undefined,
|
|
|
|
food: undefined,
|
|
|
|
labelId: undefined,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function createListItem() {
|
|
|
|
if (!shoppingList.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value += 1;
|
|
|
|
|
|
|
|
// make sure it's inserted into the end of the list, which may have been updated
|
|
|
|
createListItemData.value.position = shoppingList.value?.listItems?.length || 1;
|
2022-01-17 00:24:24 +00:00
|
|
|
const { data } = await userApi.shopping.items.createOne(createListItemData.value);
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value -= 1;
|
2022-01-17 00:24:24 +00:00
|
|
|
|
|
|
|
if (data) {
|
|
|
|
createListItemData.value = ingredientResetFactory();
|
|
|
|
createEditorOpen.value = false;
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateIndex(data: ShoppingListItemOut[]) {
|
|
|
|
if (shoppingList.value?.listItems) {
|
|
|
|
shoppingList.value.listItems = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateListItems();
|
|
|
|
}
|
|
|
|
|
|
|
|
async function deleteListItems(items: ShoppingListItemOut[]) {
|
|
|
|
if (!shoppingList.value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value += 1;
|
2022-01-17 00:24:24 +00:00
|
|
|
const { data } = await userApi.shopping.items.deleteMany(items);
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value -= 1;
|
2022-01-17 00:24:24 +00:00
|
|
|
|
|
|
|
if (data) {
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateListItems() {
|
|
|
|
if (!shoppingList.value?.listItems) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set Position
|
|
|
|
shoppingList.value.listItems = shoppingList.value.listItems.map((itm: ShoppingListItemOut, idx: number) => {
|
|
|
|
itm.position = idx;
|
|
|
|
return itm;
|
|
|
|
});
|
|
|
|
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value += 1;
|
2022-01-17 00:24:24 +00:00
|
|
|
const { data } = await userApi.shopping.items.updateMany(shoppingList.value.listItems);
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter.value -= 1;
|
2022-01-17 00:24:24 +00:00
|
|
|
|
|
|
|
if (data) {
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-09 07:24:34 +00:00
|
|
|
return {
|
2022-01-17 00:24:24 +00:00
|
|
|
addRecipeReferenceToList,
|
|
|
|
updateListItems,
|
2022-01-09 07:24:34 +00:00
|
|
|
allLabels,
|
2022-01-17 00:24:24 +00:00
|
|
|
byLabel,
|
2022-01-09 07:24:34 +00:00
|
|
|
contextMenu,
|
2022-01-17 00:24:24 +00:00
|
|
|
contextMenuAction,
|
|
|
|
copyListItems,
|
|
|
|
createEditorOpen,
|
|
|
|
createListItem,
|
|
|
|
createListItemData,
|
2022-01-09 07:24:34 +00:00
|
|
|
deleteChecked,
|
2022-01-17 00:24:24 +00:00
|
|
|
deleteListItem,
|
2022-01-09 07:24:34 +00:00
|
|
|
edit,
|
2022-01-17 00:24:24 +00:00
|
|
|
itemsByLabel,
|
|
|
|
listItems,
|
|
|
|
listRecipes,
|
2023-01-08 17:23:24 +00:00
|
|
|
loadingCounter,
|
2022-01-17 00:24:24 +00:00
|
|
|
presentLabels,
|
|
|
|
removeRecipeReferenceToList,
|
|
|
|
saveListItem,
|
2022-01-09 07:24:34 +00:00
|
|
|
shoppingList,
|
2022-01-17 00:24:24 +00:00
|
|
|
showChecked,
|
|
|
|
sortByLabels,
|
|
|
|
toggleShowChecked,
|
|
|
|
uncheckAll,
|
|
|
|
updateIndex,
|
|
|
|
allUnits,
|
|
|
|
allFoods,
|
2022-01-09 07:24:34 +00:00
|
|
|
};
|
|
|
|
},
|
|
|
|
head() {
|
|
|
|
return {
|
|
|
|
title: this.$t("shopping-list.shopping-list") as string,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
.number-input-container {
|
|
|
|
max-width: 50px;
|
|
|
|
}
|
|
|
|
</style>
|