| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  | import { useAsync, ref, reactive, Ref } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2021-08-22 15:23:45 -08:00
										 |  |  | import { useAsyncKey } from "./use-utils"; | 
					
						
							|  |  |  | import { useApiSingleton } from "~/composables/use-api"; | 
					
						
							|  |  |  | import { Unit } from "~/api/class-interfaces/recipe-units"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  | let unitStore: Ref<Unit[] | null> | null = null; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-22 15:23:45 -08:00
										 |  |  | export const useUnits = function () { | 
					
						
							|  |  |  |   const api = useApiSingleton(); | 
					
						
							|  |  |  |   const loading = ref(false); | 
					
						
							|  |  |  |   const deleteTargetId = ref(0); | 
					
						
							|  |  |  |   const validForm = ref(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const workingUnitData = reactive({ | 
					
						
							|  |  |  |     id: 0, | 
					
						
							|  |  |  |     name: "", | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |     fraction: true, | 
					
						
							| 
									
										
										
										
											2021-08-22 15:23:45 -08:00
										 |  |  |     abbreviation: "", | 
					
						
							|  |  |  |     description: "", | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const actions = { | 
					
						
							|  |  |  |     getAll() { | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							|  |  |  |       const units = useAsync(async () => { | 
					
						
							|  |  |  |         const { data } = await api.units.getAll(); | 
					
						
							|  |  |  |         return data; | 
					
						
							|  |  |  |       }, useAsyncKey()); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |       loading.value = false; | 
					
						
							| 
									
										
										
										
											2021-08-22 15:23:45 -08:00
										 |  |  |       return units; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     async refreshAll() { | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							|  |  |  |       const { data } = await api.units.getAll(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |       if (data && unitStore) { | 
					
						
							|  |  |  |         unitStore.value = data; | 
					
						
							| 
									
										
										
										
											2021-08-22 15:23:45 -08:00
										 |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       loading.value = false; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     async createOne(domForm: VForm | null = null) { | 
					
						
							|  |  |  |       if (domForm && !domForm.validate()) { | 
					
						
							|  |  |  |         validForm.value = false; | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							|  |  |  |       const { data } = await api.units.createOne(workingUnitData); | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |       if (data && unitStore?.value) { | 
					
						
							|  |  |  |         unitStore.value.push(data); | 
					
						
							| 
									
										
										
										
											2021-08-22 15:23:45 -08:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         this.refreshAll(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       domForm?.reset(); | 
					
						
							|  |  |  |       validForm.value = true; | 
					
						
							|  |  |  |       this.resetWorking(); | 
					
						
							|  |  |  |       loading.value = false; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     async updateOne() { | 
					
						
							|  |  |  |       if (!workingUnitData.id) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							|  |  |  |       const { data } = await api.units.updateOne(workingUnitData.id, workingUnitData); | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |       if (data && unitStore?.value) { | 
					
						
							| 
									
										
										
										
											2021-08-22 15:23:45 -08:00
										 |  |  |         this.refreshAll(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       loading.value = false; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     async deleteOne(id: string | number) { | 
					
						
							|  |  |  |       loading.value = true; | 
					
						
							|  |  |  |       const { data } = await api.units.deleteOne(id); | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |       if (data && unitStore?.value) { | 
					
						
							| 
									
										
										
										
											2021-08-22 15:23:45 -08:00
										 |  |  |         this.refreshAll(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     resetWorking() { | 
					
						
							|  |  |  |       workingUnitData.id = 0; | 
					
						
							|  |  |  |       workingUnitData.name = ""; | 
					
						
							|  |  |  |       workingUnitData.abbreviation = ""; | 
					
						
							|  |  |  |       workingUnitData.description = ""; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     setWorking(item: Unit) { | 
					
						
							|  |  |  |       workingUnitData.id = item.id; | 
					
						
							|  |  |  |       workingUnitData.name = item.name; | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |       workingUnitData.fraction = item.fraction; | 
					
						
							| 
									
										
										
										
											2021-08-22 15:23:45 -08:00
										 |  |  |       workingUnitData.abbreviation = item.abbreviation; | 
					
						
							|  |  |  |       workingUnitData.description = item.description; | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |     flushStore() { | 
					
						
							|  |  |  |       unitStore = null; | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-08-22 15:23:45 -08:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |   if (!unitStore) { | 
					
						
							|  |  |  |     unitStore = actions.getAll(); | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-08-22 15:23:45 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 20:05:02 -08:00
										 |  |  |   return { units: unitStore, workingUnitData, deleteTargetId, actions, validForm }; | 
					
						
							| 
									
										
										
										
											2021-08-22 15:23:45 -08:00
										 |  |  | }; |