| 
									
										
										
										
											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" | 
					
						
							|  |  |  |       @submit="createTool" | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |       <v-card-text> | 
					
						
							|  |  |  |         <v-form ref="domNewToolForm"> | 
					
						
							|  |  |  |           <v-text-field | 
					
						
							|  |  |  |             v-model="createTarget.name" | 
					
						
							|  |  |  |             autofocus | 
					
						
							|  |  |  |             :label="$t('general.name')" | 
					
						
							|  |  |  |             :rules="[validators.required]" | 
					
						
							|  |  |  |           ></v-text-field> | 
					
						
							|  |  |  |           <v-checkbox v-model="createTarget.onHand" :label="$t('tool.on-hand')"> | 
					
						
							|  |  |  |           </v-checkbox> | 
					
						
							|  |  |  |         </v-form> | 
					
						
							|  |  |  |       </v-card-text> | 
					
						
							|  |  |  |     </BaseDialog> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     <!-- Edit Dialog --> | 
					
						
							|  |  |  |     <BaseDialog | 
					
						
							|  |  |  |       v-model="state.editDialog" | 
					
						
							|  |  |  |       :icon="$globals.icons.potSteam" | 
					
						
							|  |  |  |       :title="$t('data-pages.tools.edit-tool')" | 
					
						
							|  |  |  |       :submit-text="$tc('general.save')" | 
					
						
							|  |  |  |       @submit="editSaveTool" | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |       <v-card-text v-if="editTarget"> | 
					
						
							|  |  |  |         <div class="mt-4"> | 
					
						
							|  |  |  |           <v-text-field v-model="editTarget.name" :label="$t('general.name')"> </v-text-field> | 
					
						
							|  |  |  |           <v-checkbox v-model="editTarget.onHand" :label="$t('tool.on-hand')"> </v-checkbox> | 
					
						
							|  |  |  |         </div> | 
					
						
							|  |  |  |       </v-card-text> | 
					
						
							|  |  |  |     </BaseDialog> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     <!-- Delete Dialog --> | 
					
						
							|  |  |  |     <BaseDialog | 
					
						
							|  |  |  |       v-model="state.deleteDialog" | 
					
						
							|  |  |  |       :title="$tc('general.confirm')" | 
					
						
							|  |  |  |       :icon="$globals.icons.alertCircle" | 
					
						
							|  |  |  |       color="error" | 
					
						
							|  |  |  |       @confirm="deleteTool" | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |       <v-card-text> | 
					
						
							| 
									
										
										
										
											2023-11-21 15:22:36 +00:00
										 |  |  |         {{ $t("general.confirm-delete-generic") }} | 
					
						
							|  |  |  |         <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" | 
					
						
							|  |  |  |       :title="$tc('general.confirm')" | 
					
						
							|  |  |  |       :icon="$globals.icons.alertCircle" | 
					
						
							|  |  |  |       color="error" | 
					
						
							|  |  |  |       @confirm="deleteSelected" | 
					
						
							|  |  |  |     > | 
					
						
							|  |  |  |       <v-card-text> | 
					
						
							|  |  |  |         <p class="h4">{{ $t('general.confirm-delete-generic-items') }}</p> | 
					
						
							|  |  |  |         <v-card outlined> | 
					
						
							|  |  |  |           <v-virtual-scroll height="400" item-height="25" :items="bulkDeleteTarget"> | 
					
						
							|  |  |  |             <template #default="{ item }"> | 
					
						
							|  |  |  |               <v-list-item class="pb-2"> | 
					
						
							|  |  |  |                 <v-list-item-content> | 
					
						
							|  |  |  |                   <v-list-item-title>{{ item.name }}</v-list-item-title> | 
					
						
							|  |  |  |                 </v-list-item-content> | 
					
						
							|  |  |  |               </v-list-item> | 
					
						
							|  |  |  |             </template> | 
					
						
							|  |  |  |           </v-virtual-scroll> | 
					
						
							|  |  |  |         </v-card> | 
					
						
							|  |  |  |       </v-card-text> | 
					
						
							|  |  |  |     </BaseDialog> | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-21 14:46:34 +00:00
										 |  |  |     <!-- Data Table --> | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |     <BaseCardSectionTitle :icon="$globals.icons.potSteam" section :title="$tc('data-pages.tools.tool-data')"> </BaseCardSectionTitle> | 
					
						
							|  |  |  |     <CrudTable | 
					
						
							|  |  |  |       :table-config="tableConfig" | 
					
						
							|  |  |  |       :headers.sync="tableHeaders" | 
					
						
							|  |  |  |       :data="tools || []" | 
					
						
							| 
									
										
										
										
											2024-02-04 19:55:14 +01:00
										 |  |  |       :bulk-actions="[{icon: $globals.icons.delete, text: $tc('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> | 
					
						
							|  |  |  |         <BaseButton create @click="state.createDialog = true">{{ $t("general.create") }}</BaseButton> | 
					
						
							|  |  |  |       </template> | 
					
						
							|  |  |  |       <template #item.onHand="{ item }"> | 
					
						
							|  |  |  |         <v-icon :color="item.onHand ? 'success' : undefined"> | 
					
						
							|  |  |  |           {{ item.onHand ? $globals.icons.check : $globals.icons.close }} | 
					
						
							|  |  |  |         </v-icon> | 
					
						
							|  |  |  |       </template> | 
					
						
							|  |  |  |     </CrudTable> | 
					
						
							|  |  |  |   </div> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script lang="ts"> | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  | import { computed, defineComponent, reactive, ref, useContext } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  | import { validators } from "~/composables/use-validators"; | 
					
						
							|  |  |  | import { useToolStore, useToolData } from "~/composables/store"; | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  | import { RecipeTool } from "~/lib/api/types/recipe"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | interface RecipeToolWithOnHand extends RecipeTool { | 
					
						
							|  |  |  |   onHand: boolean; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default defineComponent({ | 
					
						
							|  |  |  |   setup() { | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |     const { $auth, i18n } = useContext(); | 
					
						
							| 
									
										
										
										
											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, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         text: i18n.t("tool.on-hand"), | 
					
						
							|  |  |  |         value: "onHand", | 
					
						
							|  |  |  |         show: true, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     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-01-13 10:19:49 -06:00
										 |  |  |     const userHousehold = computed(() => $auth.user?.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]; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         toolData.data.householdsWithTool = []; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-21 15:31:05 +01:00
										 |  |  |       // @ts-ignore - only property really required is the name and onHand (RecipeOrganizerPage)
 | 
					
						
							| 
									
										
										
										
											2025-01-13 10:19:49 -06:00
										 |  |  |       await toolStore.actions.createOne({ name: toolData.data.name, householdsWithTool: toolData.data.householdsWithTool }); | 
					
						
							| 
									
										
										
										
											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]; | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |           editTarget.value.householdsWithTool.push(userHousehold.value); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } else if (!editTarget.value.onHand && editTarget.value.householdsWithTool?.includes(userHousehold.value)) { | 
					
						
							|  |  |  |         editTarget.value.householdsWithTool = editTarget.value.householdsWithTool.filter( | 
					
						
							|  |  |  |           (household) => household !== userHousehold.value | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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> |