| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <div> | 
					
						
							|  |  |  |     <!-- Create Dialog --> | 
					
						
							|  |  |  |     <BaseDialog | 
					
						
							|  |  |  |       v-model="state.createDialog" | 
					
						
							|  |  |  |       :title="$t('data-pages.tools.new-tool')" | 
					
						
							|  |  |  |       :icon="$globals.icons.potSteam" | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       can-submit | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       @submit="createTool" | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |       <v-card-text> | 
					
						
							|  |  |  |         <v-form ref="domNewToolForm"> | 
					
						
							|  |  |  |           <v-text-field | 
					
						
							|  |  |  |             v-model="createTarget.name" | 
					
						
							|  |  |  |             autofocus | 
					
						
							|  |  |  |             :label="$t('general.name')" | 
					
						
							|  |  |  |             :rules="[validators.required]" | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |           /> | 
					
						
							|  |  |  |           <v-checkbox | 
					
						
							|  |  |  |             v-model="createTarget.onHand" | 
					
						
							|  |  |  |             :label="$t('tool.on-hand')" | 
					
						
							|  |  |  |           /> | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |         </v-form> | 
					
						
							|  |  |  |       </v-card-text> | 
					
						
							|  |  |  |     </BaseDialog> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     <!-- Edit Dialog --> | 
					
						
							|  |  |  |     <BaseDialog | 
					
						
							|  |  |  |       v-model="state.editDialog" | 
					
						
							|  |  |  |       :icon="$globals.icons.potSteam" | 
					
						
							|  |  |  |       :title="$t('data-pages.tools.edit-tool')" | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       :submit-text="$t('general.save')" | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       @submit="editSaveTool" | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |       <v-card-text v-if="editTarget"> | 
					
						
							|  |  |  |         <div class="mt-4"> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |           <v-text-field | 
					
						
							|  |  |  |             v-model="editTarget.name" | 
					
						
							|  |  |  |             :label="$t('general.name')" | 
					
						
							|  |  |  |           /> | 
					
						
							|  |  |  |           <v-checkbox | 
					
						
							|  |  |  |             v-model="editTarget.onHand" | 
					
						
							|  |  |  |             :label="$t('tool.on-hand')" | 
					
						
							|  |  |  |           /> | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |         </div> | 
					
						
							|  |  |  |       </v-card-text> | 
					
						
							|  |  |  |     </BaseDialog> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     <!-- Delete Dialog --> | 
					
						
							|  |  |  |     <BaseDialog | 
					
						
							|  |  |  |       v-model="state.deleteDialog" | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       :title="$t('general.confirm')" | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       :icon="$globals.icons.alertCircle" | 
					
						
							|  |  |  |       color="error" | 
					
						
							| 
									
										
										
										
											2025-07-20 17:06:39 +02:00
										 |  |  |       can-confirm | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       @confirm="deleteTool" | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |       <v-card-text> | 
					
						
							| 
									
										
										
										
											2023-11-21 15:22:36 +00:00
										 |  |  |         {{ $t("general.confirm-delete-generic") }} | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         <p | 
					
						
							|  |  |  |           v-if="deleteTarget" | 
					
						
							|  |  |  |           class="mt-4 ml-4" | 
					
						
							|  |  |  |         > | 
					
						
							|  |  |  |           {{ deleteTarget.name }} | 
					
						
							|  |  |  |         </p> | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       </v-card-text> | 
					
						
							|  |  |  |     </BaseDialog> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  |     <!-- Bulk Delete Dialog --> | 
					
						
							|  |  |  |     <BaseDialog | 
					
						
							|  |  |  |       v-model="state.bulkDeleteDialog" | 
					
						
							|  |  |  |       width="650px" | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       :title="$t('general.confirm')" | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  |       :icon="$globals.icons.alertCircle" | 
					
						
							|  |  |  |       color="error" | 
					
						
							|  |  |  |       @confirm="deleteSelected" | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |       <v-card-text> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         <p class="h4"> | 
					
						
							|  |  |  |           {{ $t('general.confirm-delete-generic-items') }} | 
					
						
							|  |  |  |         </p> | 
					
						
							|  |  |  |         <v-card variant="outlined"> | 
					
						
							|  |  |  |           <v-virtual-scroll | 
					
						
							|  |  |  |             height="400" | 
					
						
							|  |  |  |             item-height="25" | 
					
						
							|  |  |  |             :items="bulkDeleteTarget" | 
					
						
							|  |  |  |           > | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  |             <template #default="{ item }"> | 
					
						
							|  |  |  |               <v-list-item class="pb-2"> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |                 <v-list-item-title>{{ item.name }}</v-list-item-title> | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  |               </v-list-item> | 
					
						
							|  |  |  |             </template> | 
					
						
							|  |  |  |           </v-virtual-scroll> | 
					
						
							|  |  |  |         </v-card> | 
					
						
							|  |  |  |       </v-card-text> | 
					
						
							|  |  |  |     </BaseDialog> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-21 14:46:34 +00:00
										 |  |  |     <!-- Data Table --> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     <BaseCardSectionTitle | 
					
						
							|  |  |  |       :icon="$globals.icons.potSteam" | 
					
						
							|  |  |  |       section | 
					
						
							|  |  |  |       :title="$t('data-pages.tools.tool-data')" | 
					
						
							|  |  |  |     /> | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |     <CrudTable | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       v-model:headers="tableHeaders" | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       :table-config="tableConfig" | 
					
						
							|  |  |  |       :data="tools || []" | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       :bulk-actions="[{ icon: $globals.icons.delete, text: $t('general.delete'), event: 'delete-selected' }]" | 
					
						
							| 
									
										
										
										
											2024-09-22 09:59:20 -05:00
										 |  |  |       initial-sort="name" | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       @delete-one="deleteEventHandler" | 
					
						
							|  |  |  |       @edit-one="editEventHandler" | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  |       @delete-selected="bulkDeleteEventHandler" | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |     > | 
					
						
							|  |  |  |       <template #button-row> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         <BaseButton | 
					
						
							|  |  |  |           create | 
					
						
							|  |  |  |           @click="state.createDialog = true" | 
					
						
							|  |  |  |         > | 
					
						
							|  |  |  |           {{ $t("general.create") }} | 
					
						
							|  |  |  |         </BaseButton> | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       </template> | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       <template #[`item.onHand`]="{ item }"> | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |         <v-icon :color="item.onHand ? 'success' : undefined"> | 
					
						
							|  |  |  |           {{ item.onHand ? $globals.icons.check : $globals.icons.close }} | 
					
						
							|  |  |  |         </v-icon> | 
					
						
							|  |  |  |       </template> | 
					
						
							|  |  |  |     </CrudTable> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script lang="ts"> | 
					
						
							|  |  |  | import { validators } from "~/composables/use-validators"; | 
					
						
							|  |  |  | import { useToolStore, useToolData } from "~/composables/store"; | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  | import type { RecipeTool } from "~/lib/api/types/recipe"; | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | interface RecipeToolWithOnHand extends RecipeTool { | 
					
						
							|  |  |  |   onHand: boolean; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  | export default defineNuxtComponent({ | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |   setup() { | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     const i18n = useI18n(); | 
					
						
							|  |  |  |     const $auth = useMealieAuth(); | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |     const tableConfig = { | 
					
						
							|  |  |  |       hideColumns: true, | 
					
						
							|  |  |  |       canExport: true, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |     const tableHeaders = [ | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         text: i18n.t("general.id"), | 
					
						
							|  |  |  |         value: "id", | 
					
						
							|  |  |  |         show: false, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         text: i18n.t("general.name"), | 
					
						
							|  |  |  |         value: "name", | 
					
						
							|  |  |  |         show: true, | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         sortable: true, | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       }, | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         text: i18n.t("tool.on-hand"), | 
					
						
							|  |  |  |         value: "onHand", | 
					
						
							|  |  |  |         show: true, | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         sortable: true, | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       }, | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const state = reactive({ | 
					
						
							|  |  |  |       createDialog: false, | 
					
						
							|  |  |  |       editDialog: false, | 
					
						
							|  |  |  |       deleteDialog: false, | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  |       bulkDeleteDialog: false, | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     const userHousehold = computed(() => $auth.user.value?.householdSlug || ""); | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |     const toolData = useToolData(); | 
					
						
							|  |  |  |     const toolStore = useToolStore(); | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |     const tools = computed(() => toolStore.store.value.map((tools) => { | 
					
						
							|  |  |  |       const onHand = tools.householdsWithTool?.includes(userHousehold.value) || false; | 
					
						
							|  |  |  |       return { ...tools, onHand } as RecipeToolWithOnHand; | 
					
						
							|  |  |  |     })); | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // ============================================================
 | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |     // Create Tool
 | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     async function createTool() { | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |       if (toolData.data.onHand) { | 
					
						
							|  |  |  |         toolData.data.householdsWithTool = [userHousehold.value]; | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       } | 
					
						
							|  |  |  |       else { | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |         toolData.data.householdsWithTool = []; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       await toolStore.actions.createOne({ | 
					
						
							|  |  |  |         name: toolData.data.name, householdsWithTool: toolData.data.householdsWithTool, | 
					
						
							|  |  |  |         id: "", | 
					
						
							|  |  |  |         slug: "", | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       toolData.reset(); | 
					
						
							|  |  |  |       state.createDialog = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // ============================================================
 | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |     // Edit Tool
 | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |     const editTarget = ref<RecipeToolWithOnHand | null>(null); | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |     function editEventHandler(item: RecipeToolWithOnHand) { | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       state.editDialog = true; | 
					
						
							|  |  |  |       editTarget.value = item; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function editSaveTool() { | 
					
						
							|  |  |  |       if (!editTarget.value) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |       if (editTarget.value.onHand && !editTarget.value.householdsWithTool?.includes(userHousehold.value)) { | 
					
						
							|  |  |  |         if (!editTarget.value.householdsWithTool) { | 
					
						
							|  |  |  |           editTarget.value.householdsWithTool = [userHousehold.value]; | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |         } | 
					
						
							|  |  |  |         else { | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |           editTarget.value.householdsWithTool.push(userHousehold.value); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |       } | 
					
						
							|  |  |  |       else if (!editTarget.value.onHand && editTarget.value.householdsWithTool?.includes(userHousehold.value)) { | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |         editTarget.value.householdsWithTool = editTarget.value.householdsWithTool.filter( | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |           household => household !== userHousehold.value, | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |         ); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       await toolStore.actions.updateOne(editTarget.value); | 
					
						
							|  |  |  |       state.editDialog = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // ============================================================
 | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |     // Delete Tool
 | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |     const deleteTarget = ref<RecipeToolWithOnHand | null>(null); | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |     function deleteEventHandler(item: RecipeToolWithOnHand) { | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       state.deleteDialog = true; | 
					
						
							|  |  |  |       deleteTarget.value = item; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function deleteTool() { | 
					
						
							|  |  |  |       if (!deleteTarget.value || deleteTarget.value.id === undefined) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       await toolStore.actions.deleteOne(deleteTarget.value.id); | 
					
						
							|  |  |  |       state.deleteDialog = false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  |     // ============================================================
 | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |     // Bulk Delete Tool
 | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |     const bulkDeleteTarget = ref<RecipeToolWithOnHand[]>([]); | 
					
						
							|  |  |  |     function bulkDeleteEventHandler(selection: RecipeToolWithOnHand[]) { | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  |       bulkDeleteTarget.value = selection; | 
					
						
							|  |  |  |       state.bulkDeleteDialog = true; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function deleteSelected() { | 
					
						
							|  |  |  |       for (const item of bulkDeleteTarget.value) { | 
					
						
							|  |  |  |         await toolStore.actions.deleteOne(item.id); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       bulkDeleteTarget.value = []; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |     return { | 
					
						
							|  |  |  |       state, | 
					
						
							|  |  |  |       tableConfig, | 
					
						
							|  |  |  |       tableHeaders, | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |       tools, | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       validators, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // create
 | 
					
						
							|  |  |  |       createTarget: toolData.data, | 
					
						
							|  |  |  |       createTool, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // edit
 | 
					
						
							|  |  |  |       editTarget, | 
					
						
							|  |  |  |       editEventHandler, | 
					
						
							|  |  |  |       editSaveTool, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // delete
 | 
					
						
							|  |  |  |       deleteTarget, | 
					
						
							|  |  |  |       deleteEventHandler, | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  |       deleteTool, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // bulk delete
 | 
					
						
							|  |  |  |       bulkDeleteTarget, | 
					
						
							|  |  |  |       bulkDeleteEventHandler, | 
					
						
							|  |  |  |       deleteSelected, | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | </script> |