| 
									
										
										
										
											2021-11-23 18:57:24 -09:00
										 |  |  | <template> | 
					
						
							|  |  |  |   <v-container v-if="user" class="narrow-container"> | 
					
						
							|  |  |  |     <BasePageTitle> | 
					
						
							|  |  |  |       <template #header> | 
					
						
							|  |  |  |         <v-img max-height="125" max-width="125" :src="require('~/static/svgs/manage-profile.svg')"></v-img> | 
					
						
							|  |  |  |       </template> | 
					
						
							|  |  |  |       <template #title> Admin User Management </template> | 
					
						
							|  |  |  |       Changes to this user will be reflected immediately. | 
					
						
							|  |  |  |     </BasePageTitle> | 
					
						
							|  |  |  |     <AppToolbar back> </AppToolbar> | 
					
						
							|  |  |  |     <v-form v-if="!userError" ref="refNewUserForm" @submit.prevent="handleSubmit"> | 
					
						
							|  |  |  |       <v-card outlined> | 
					
						
							|  |  |  |         <v-card-text> | 
					
						
							|  |  |  |           <div class="d-flex"> | 
					
						
							|  |  |  |             <p>User Id: {{ user.id }}</p> | 
					
						
							|  |  |  |           </div> | 
					
						
							|  |  |  |           <v-select | 
					
						
							|  |  |  |             v-if="groups" | 
					
						
							|  |  |  |             v-model="user.group" | 
					
						
							|  |  |  |             :items="groups" | 
					
						
							|  |  |  |             rounded | 
					
						
							|  |  |  |             class="rounded-lg" | 
					
						
							|  |  |  |             item-text="name" | 
					
						
							|  |  |  |             item-value="name" | 
					
						
							|  |  |  |             :return-object="false" | 
					
						
							|  |  |  |             filled | 
					
						
							|  |  |  |             label="User Group" | 
					
						
							|  |  |  |             :rules="[validators.required]" | 
					
						
							|  |  |  |           ></v-select> | 
					
						
							|  |  |  |           <AutoForm v-model="user" :items="userForm" update-mode /> | 
					
						
							|  |  |  |         </v-card-text> | 
					
						
							|  |  |  |       </v-card> | 
					
						
							|  |  |  |       <div class="d-flex pa-2"> | 
					
						
							| 
									
										
										
										
											2021-11-25 14:17:02 -09:00
										 |  |  |         <BaseButton type="submit" edit class="ml-auto"> {{ $t("general.update") }}</BaseButton> | 
					
						
							| 
									
										
										
										
											2021-11-23 18:57:24 -09:00
										 |  |  |       </div> | 
					
						
							|  |  |  |     </v-form> | 
					
						
							|  |  |  |   </v-container> | 
					
						
							|  |  |  | </template> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <script lang="ts"> | 
					
						
							|  |  |  | import { defineComponent, useRoute, onMounted, ref } from "@nuxtjs/composition-api"; | 
					
						
							|  |  |  | import { useAdminApi } from "~/composables/api"; | 
					
						
							|  |  |  | import { useGroups } from "~/composables/use-groups"; | 
					
						
							|  |  |  | import { alert } from "~/composables/use-toast"; | 
					
						
							|  |  |  | import { useUserForm } from "~/composables/use-users"; | 
					
						
							|  |  |  | import { validators } from "~/composables/use-validators"; | 
					
						
							| 
									
										
										
										
											2022-01-09 07:15:23 +01:00
										 |  |  | import { VForm } from "~/types/vuetify"; | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  | import { UserOut } from "~/types/api-types/user"; | 
					
						
							| 
									
										
										
										
											2021-11-23 18:57:24 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  | export default defineComponent({ | 
					
						
							|  |  |  |   layout: "admin", | 
					
						
							|  |  |  |   setup() { | 
					
						
							|  |  |  |     const { userForm } = useUserForm(); | 
					
						
							|  |  |  |     const { groups } = useGroups(); | 
					
						
							|  |  |  |     const route = useRoute(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const userId = route.value.params.id; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // ==============================================
 | 
					
						
							|  |  |  |     // New User Form
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const refNewUserForm = ref<VForm | null>(null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     const adminApi = useAdminApi(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |     const user = ref<UserOut | null>(null); | 
					
						
							| 
									
										
										
										
											2021-11-23 18:57:24 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |     const userError = ref(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     onMounted(async () => { | 
					
						
							|  |  |  |       const { data, error } = await adminApi.users.getOne(userId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (error?.response?.status === 404) { | 
					
						
							|  |  |  |         alert.error("User Not Found"); | 
					
						
							|  |  |  |         userError.value = true; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (data) { | 
					
						
							|  |  |  |         user.value = data; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     async function handleSubmit() { | 
					
						
							| 
									
										
										
										
											2022-01-16 03:38:11 +01:00
										 |  |  |       if (!refNewUserForm.value?.validate() || user.value === null) return; | 
					
						
							| 
									
										
										
										
											2021-11-23 18:57:24 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  |       const { response, data } = await adminApi.users.updateOne(user.value.id, user.value); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (response?.status === 200 && data) { | 
					
						
							|  |  |  |         user.value = data; | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return { | 
					
						
							|  |  |  |       user, | 
					
						
							|  |  |  |       userError, | 
					
						
							|  |  |  |       userForm, | 
					
						
							|  |  |  |       refNewUserForm, | 
					
						
							|  |  |  |       handleSubmit, | 
					
						
							|  |  |  |       groups, | 
					
						
							|  |  |  |       validators, | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | </script> |