| 
									
										
										
										
											2024-03-11 08:28:54 -05:00
										 |  |  | import { useAsyncValidator } from "~/composables/use-validators"; | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  | import type { VForm } from "~/types/vuetify"; | 
					
						
							| 
									
										
										
										
											2024-03-11 08:28:54 -05:00
										 |  |  | import { usePublicApi } from "~/composables/api/api-client"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const domAccountForm = ref<VForm | null>(null); | 
					
						
							|  |  |  | const username = ref(""); | 
					
						
							|  |  |  | const fullName = ref(""); | 
					
						
							|  |  |  | const email = ref(""); | 
					
						
							|  |  |  | const password1 = ref(""); | 
					
						
							|  |  |  | const password2 = ref(""); | 
					
						
							|  |  |  | const advancedOptions = ref(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export const useUserRegistrationForm = () => { | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |   const i18n = useI18n(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-11 08:28:54 -05:00
										 |  |  |   function safeValidate(form: Ref<VForm | null>) { | 
					
						
							|  |  |  |     if (form.value && form.value.validate) { | 
					
						
							|  |  |  |       return form.value.validate(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return false; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   // ================================================================
 | 
					
						
							|  |  |  |   // Provide Group Details
 | 
					
						
							|  |  |  |   const publicApi = usePublicApi(); | 
					
						
							|  |  |  |   // ================================================================
 | 
					
						
							|  |  |  |   // Provide Account Details
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const usernameErrorMessages = ref<string[]>([]); | 
					
						
							|  |  |  |   const { validate: validateUsername, valid: validUsername } = useAsyncValidator( | 
					
						
							|  |  |  |     username, | 
					
						
							|  |  |  |     (v: string) => publicApi.validators.username(v), | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     i18n.t("validation.username-is-taken"), | 
					
						
							|  |  |  |     usernameErrorMessages, | 
					
						
							| 
									
										
										
										
											2024-03-11 08:28:54 -05:00
										 |  |  |   ); | 
					
						
							|  |  |  |   const emailErrorMessages = ref<string[]>([]); | 
					
						
							|  |  |  |   const { validate: validateEmail, valid: validEmail } = useAsyncValidator( | 
					
						
							|  |  |  |     email, | 
					
						
							|  |  |  |     (v: string) => publicApi.validators.email(v), | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     i18n.t("validation.email-is-taken"), | 
					
						
							|  |  |  |     emailErrorMessages, | 
					
						
							| 
									
										
										
										
											2024-03-11 08:28:54 -05:00
										 |  |  |   ); | 
					
						
							|  |  |  |   const accountDetails = { | 
					
						
							|  |  |  |     username, | 
					
						
							|  |  |  |     fullName, | 
					
						
							|  |  |  |     email, | 
					
						
							|  |  |  |     advancedOptions, | 
					
						
							|  |  |  |     validate: async () => { | 
					
						
							|  |  |  |       if (!(validUsername.value && validEmail.value)) { | 
					
						
							|  |  |  |         await Promise.all([validateUsername(), validateEmail()]); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       return (safeValidate(domAccountForm as Ref<VForm>) && validUsername.value && validEmail.value); | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     reset: () => { | 
					
						
							|  |  |  |       accountDetails.username.value = ""; | 
					
						
							|  |  |  |       accountDetails.fullName.value = ""; | 
					
						
							|  |  |  |       accountDetails.email.value = ""; | 
					
						
							|  |  |  |       accountDetails.advancedOptions.value = false; | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   // ================================================================
 | 
					
						
							|  |  |  |   // Provide Credentials
 | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |   const passwordMatch = () => password1.value === password2.value || i18n.t("user.password-must-match"); | 
					
						
							| 
									
										
										
										
											2024-03-11 08:28:54 -05:00
										 |  |  |   const credentials = { | 
					
						
							|  |  |  |     password1, | 
					
						
							|  |  |  |     password2, | 
					
						
							|  |  |  |     passwordMatch, | 
					
						
							|  |  |  |     reset: () => { | 
					
						
							|  |  |  |       credentials.password1.value = ""; | 
					
						
							|  |  |  |       credentials.password2.value = ""; | 
					
						
							| 
									
										
										
										
											2025-06-20 00:09:12 +07:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2024-03-11 08:28:54 -05:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     accountDetails, | 
					
						
							|  |  |  |     credentials, | 
					
						
							|  |  |  |     emailErrorMessages, | 
					
						
							|  |  |  |     usernameErrorMessages, | 
					
						
							|  |  |  |     // Fields
 | 
					
						
							|  |  |  |     advancedOptions, | 
					
						
							|  |  |  |     // Validators
 | 
					
						
							|  |  |  |     validateUsername, | 
					
						
							|  |  |  |     validateEmail, | 
					
						
							|  |  |  |     // Dom Refs
 | 
					
						
							|  |  |  |     domAccountForm, | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }; |