All files / client/src/views/registration registrationThunks.ts

78.57% Statements 11/14
50% Branches 3/6
100% Functions 3/3
78.57% Lines 11/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41            356x 356x 356x 356x     356x     6x 6x   4x 1x   1x                     3x                
import { postRegistration } from "client/services/userServices";
import { submitLogin } from "client/views/login/loginThunks";
import { KonstiRegistrationFormFields } from "client/views/registration/components/KonstiRegistrationForm";
import { AppThunk } from "client/types/reduxTypes";
import { exhaustiveSwitchGuard } from "shared/utils/exhaustiveSwitchGuard";
 
export enum RegistrationErrorMessage {
  USERNAME_TAKEN = "error.usernameTaken",
  INVALID_SERIAL = "error.invalidSerial",
  UNKNOWN = "error.unknown",
}
 
export const submitRegistration = (
  registrationFormFields: KonstiRegistrationFormFields,
): AppThunk<Promise<RegistrationErrorMessage | undefined>> => {
  return async (dispatch): Promise<RegistrationErrorMessage | undefined> => {
    const registrationResponse = await postRegistration(registrationFormFields);
 
    if (registrationResponse.status === "error") {
      switch (registrationResponse.errorId) {
        case "usernameNotFree":
          return RegistrationErrorMessage.USERNAME_TAKEN;
        case "invalidSerial":
          return RegistrationErrorMessage.INVALID_SERIAL;
        case "unknown":
          return RegistrationErrorMessage.UNKNOWN;
        default:
          return exhaustiveSwitchGuard(registrationResponse.errorId);
      }
    }
 
    // eslint-disable-next-line @typescript-eslint/no-floating-promises
    dispatch(
      submitLogin({
        username: registrationFormFields.username,
        password: registrationFormFields.password,
      }),
    );
  };
};