kandimat/redaktions-app/src/components/SignUpErrorHandler.ts
Christoph Lienhard b26d6d6e69
Fix "used before it was defined" warning
* updated react-scripts (fix eslint-plugin problems)
* reorder functions where needed
2021-02-07 23:06:58 +01:00

69 lines
1.9 KiB
TypeScript

import { ApolloError } from "@apollo/client";
export interface SignUpError {
message: string;
emailInvalid: boolean;
firstNameInvalid: boolean;
lastNameInvalid: boolean;
passwordInvalid: boolean;
}
const isEmailAlreadyUsed = (error: ApolloError): boolean => {
const errorMessage = error.message.toLowerCase();
return (
errorMessage.includes("unique-constraint") && errorMessage.includes("email")
);
};
const isFirstNameInvalid = (error: ApolloError): boolean => {
const errorMessage = error.message.toLowerCase();
return (
errorMessage.includes("invalid") && errorMessage.includes("first name")
);
};
const isLastNameInvalid = (error: ApolloError): boolean => {
const errorMessage = error.message.toLowerCase();
return errorMessage.includes("invalid") && errorMessage.includes("last name");
};
const isPasswordInvalid = (error: ApolloError): boolean => {
const errorMessage = error.message.toLowerCase();
return errorMessage.includes("invalid") && errorMessage.includes("password");
};
const parseErrorMessage = (error: ApolloError): string => {
let result = "Sign-up failed because of the following reason(s): ";
if (isEmailAlreadyUsed(error)) {
result += "The E-Mail is already in use. ";
}
if (isFirstNameInvalid(error)) {
result += "The provided 'First Name' is invalid. ";
}
if (isLastNameInvalid(error)) {
result += "The provided 'Last Name' is invalid. ";
}
if (isPasswordInvalid(error)) {
result += "The provided password is invalid. ";
}
return result;
};
export const errorHandler = (
error: undefined | ApolloError
): undefined | SignUpError => {
return error
? {
message: parseErrorMessage(error),
emailInvalid: isEmailAlreadyUsed(error),
firstNameInvalid: isFirstNameInvalid(error),
lastNameInvalid: isLastNameInvalid(error),
passwordInvalid: isPasswordInvalid(error),
}
: undefined;
};