Skip to content

Commit

Permalink
whattheduck: Fix error handling on signup and login forms
Browse files Browse the repository at this point in the history
  • Loading branch information
bperel committed Nov 19, 2024
1 parent 618fc98 commit efdc3a8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 34 deletions.
41 changes: 29 additions & 12 deletions apps/whattheduck/src/composables/useFormErrorHandling.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
import type { ScopedError } from '~socket.io-services';
import type { Errorable, ScopedError } from '~socket.io-services';

export default (
fields: string[],
): {
validInputs: ComputedRef<string[]>;
invalidInputs: ComputedRef<string[]>;
touchedInputs: Ref<string[]>;
errorTexts: Ref<Record<string, string>>;
showError: (scopedError: ScopedError) => void;
clearErrors: () => void;
} => {
export default (fields: string[]) => {
const errorTexts = ref<Record<string, string>>({});
const validInputs = computed(() => fields.filter((field) => !invalidInputs.value.includes(field)));
const invalidInputs = computed(() => Object.keys(errorTexts.value));
Expand All @@ -21,5 +12,31 @@ export default (
errorTexts.value[scopedError.selector] = scopedError.message;
};

return { validInputs, invalidInputs, touchedInputs, errorTexts, showError, clearErrors };
const handleErrorIfExists = (response: Errorable<string, string>) => {
if (typeof response === 'object' && 'error' in response) {
if ('selector' in response && response.selector) {
errorTexts.value[response.selector!.replace('#', '')] = response.message;
} else {
debugger;
errorTexts.value[fields[0]] = response.errorDetails || response.error;
console.error(response);
}
return true;
} else {
return false;
}
};

const call = async (fn: () => Promise<Errorable<string, string>>, onSuccess: (response: string) => void) =>
fn()
.then((response) => {
if (!handleErrorIfExists(response)) {
onSuccess(response as string);
}
})
.catch((e) => {
handleErrorIfExists(e);
});

return { validInputs, invalidInputs, touchedInputs, errorTexts, showError, clearErrors, call };
};
22 changes: 12 additions & 10 deletions apps/whattheduck/src/views/Login.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
}"
:aria-label="t('Nom d\'utilisateur DucksManager')"
:placeholder="t('Nom d\'utilisateur DucksManager')"
:error-text="errorTexts.username"
@ion-blur="touchedInputs.push('username')"
/>
</ion-col>
Expand Down Expand Up @@ -96,7 +97,7 @@ const password = ref('');
const showPassword = ref(false);
const { validInputs, invalidInputs, touchedInputs, errorTexts } = useFormErrorHandling(['username', 'password']);
const { validInputs, invalidInputs, touchedInputs, errorTexts, call } = useFormErrorHandling(['username', 'password']);
const forgotPassword = () => {
router.push('/forgot');
Expand All @@ -106,15 +107,16 @@ const signup = () => {
};
const submitLogin = async () => {
const response = await socket.value?.auth.services.login({
username: username.value,
password: password.value,
});
if (typeof response !== 'string' && response && 'error' in response) {
errorTexts.value['password'] = response.error;
} else {
token.value = response;
}
await call(
() =>
socket.value!.auth.services.login({
username: username.value,
password: password.value,
}),
(response) => {
token.value = response;
},
);
};
if (!token.value) {
Expand Down
23 changes: 12 additions & 11 deletions apps/whattheduck/src/views/Signup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const { t } = useI18n();
const router = useRouter();
const { validInputs, invalidInputs, touchedInputs, errorTexts, clearErrors } = useFormErrorHandling([
const { validInputs, invalidInputs, touchedInputs, errorTexts, clearErrors, call } = useFormErrorHandling([
'username',
'email',
'password',
Expand All @@ -119,17 +119,18 @@ const submitSignup = async () => {
return;
}
clearErrors();
const response = await socket.value?.auth.services.signup({
username: username.value,
password: password.value,
email: email.value,
});
if (typeof response !== 'string' && response && 'error' in response) {
errorTexts.value[response.selector!.replace('#', '')] = response.message!;
} else {
token.value = response;
}
await call(
() =>
socket.value!.auth.services.signup({
username: username.value,
password: password.value,
email: email.value,
}),
(response) => {
token.value = response;
},
);
};
watch(
Expand Down
2 changes: 1 addition & 1 deletion apps/whattheduck/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
"Numéros possédés de ": "Possessed issues for ",
"Numéros sans dates d'achat": "Issues without purchase date",
"Numéros": "Collection size",
"OK": "Save",
"OK": "OK",
"Par sélection de numéro": "By issue selection",
"Par titre d'histoire": "By story title",
"Paramètres": "Settings",
Expand Down

0 comments on commit efdc3a8

Please sign in to comment.