| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  | import { useAsync, ref } from "@nuxtjs/composition-api"; | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  | import { useUserApi } from "~/composables/api"; | 
					
						
							| 
									
										
										
										
											2024-03-15 19:50:39 +00:00
										 |  |  | import { GroupBase, GroupSummary } from "~/lib/api/types/user"; | 
					
						
							| 
									
										
										
										
											2024-03-15 19:41:26 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-15 19:50:39 +00:00
										 |  |  | const groupSelfRef = ref<GroupSummary | null>(null); | 
					
						
							| 
									
										
										
										
											2024-03-15 19:41:26 +00:00
										 |  |  | const loading = ref(false); | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-05 22:05:29 -08:00
										 |  |  | export const useGroupSelf = function () { | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |   const api = useUserApi(); | 
					
						
							| 
									
										
										
										
											2024-03-15 19:41:26 +00:00
										 |  |  |   async function refreshGroupSelf() { | 
					
						
							|  |  |  |     loading.value = true; | 
					
						
							|  |  |  |     const { data } = await api.groups.getCurrentUserGroup(); | 
					
						
							|  |  |  |     groupSelfRef.value = data; | 
					
						
							|  |  |  |     loading.value = false; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2021-09-05 22:05:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const actions = { | 
					
						
							|  |  |  |     get() { | 
					
						
							| 
									
										
										
										
											2024-03-15 19:41:26 +00:00
										 |  |  |       if (!(groupSelfRef.value || loading.value)) { | 
					
						
							|  |  |  |         refreshGroupSelf(); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-09-05 22:05:29 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-15 19:41:26 +00:00
										 |  |  |       return groupSelfRef; | 
					
						
							| 
									
										
										
										
											2021-09-05 22:05:29 -08:00
										 |  |  |     }, | 
					
						
							|  |  |  |     async updatePreferences() { | 
					
						
							| 
									
										
										
										
											2024-03-15 19:41:26 +00:00
										 |  |  |       if (!groupSelfRef.value) { | 
					
						
							|  |  |  |         await refreshGroupSelf(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (!groupSelfRef.value?.preferences) { | 
					
						
							| 
									
										
										
										
											2021-09-05 22:05:29 -08:00
										 |  |  |         return; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-15 19:41:26 +00:00
										 |  |  |       const { data } = await api.groups.setPreferences(groupSelfRef.value.preferences); | 
					
						
							| 
									
										
										
										
											2021-09-05 22:05:29 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (data) { | 
					
						
							| 
									
										
										
										
											2024-03-15 19:41:26 +00:00
										 |  |  |         groupSelfRef.value.preferences = data; | 
					
						
							| 
									
										
										
										
											2021-09-05 22:05:29 -08:00
										 |  |  |       } | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const group = actions.get(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { actions, group }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  | export const useGroups = function () { | 
					
						
							| 
									
										
										
										
											2021-11-06 11:28:47 -08:00
										 |  |  |   const api = useUserApi(); | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |   const loading = ref(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   function getAllGroups() { | 
					
						
							|  |  |  |     loading.value = true; | 
					
						
							|  |  |  |     const asyncKey = String(Date.now()); | 
					
						
							|  |  |  |     const groups = useAsync(async () => { | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |       const { data } = await api.groups.getAll(1, -1, {orderBy: "name", orderDirection: "asc"});; | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (data) { | 
					
						
							|  |  |  |         return data.items; | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |     }, asyncKey); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     loading.value = false; | 
					
						
							|  |  |  |     return groups; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async function refreshAllGroups() { | 
					
						
							|  |  |  |     loading.value = true; | 
					
						
							| 
									
										
										
										
											2024-08-22 10:14:32 -05:00
										 |  |  |     const { data } = await api.groups.getAll(1, -1, {orderBy: "name", orderDirection: "asc"});; | 
					
						
							| 
									
										
										
										
											2022-06-25 14:39:38 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (data) { | 
					
						
							|  |  |  |       groups.value = data.items; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       groups.value = null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |     loading.value = false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async function deleteGroup(id: string | number) { | 
					
						
							|  |  |  |     loading.value = true; | 
					
						
							|  |  |  |     const { data } = await api.groups.deleteOne(id); | 
					
						
							|  |  |  |     loading.value = false; | 
					
						
							|  |  |  |     refreshAllGroups(); | 
					
						
							|  |  |  |     return data; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-21 21:22:02 +02:00
										 |  |  |   async function createGroup(payload: GroupBase) { | 
					
						
							| 
									
										
										
										
											2021-08-06 16:28:12 -08:00
										 |  |  |     loading.value = true; | 
					
						
							|  |  |  |     const { data } = await api.groups.createOne(payload); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (data && groups.value) { | 
					
						
							|  |  |  |       groups.value.push(data); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const groups = getAllGroups(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { groups, getAllGroups, refreshAllGroups, deleteGroup, createGroup }; | 
					
						
							|  |  |  | }; |