Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update useSignup.tsx to send capitalized name and surname to backend #801

Open
wants to merge 7 commits into
base: next
Choose a base branch
from

Conversation

JotaceCode
Copy link

I created a function to capitalize first letters on Sign In

Copy link

nx-cloud bot commented Sep 19, 2024

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 8c8cc26. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 1 target

Sent with 💌 from NxCloud.

@chavda-bhavik
Copy link
Member

@JotaceCode Looks good to me, can you check with spacing once? the indentation of functions is not in sync with the file. Thanks in advance!
image

@JotaceCode
Copy link
Author

Ok, i'm on it. @chavda-bhavik

@chavda-bhavik
Copy link
Member

@JotaceCode Can you update this code? I've just updated the indentation. Thanks in advance.

import jwt from 'jwt-decode';
import { useState } from 'react';
import { useRouter } from 'next/router';
import { useForm } from 'react-hook-form';
import { useMutation } from '@tanstack/react-query';

import { API_KEYS } from '@config';
import { commonApi } from '@libs/api';
import { track } from '@libs/amplitude';
import { useAppState } from 'store/app.context';
import { handleRouteBasedOnScreenResponse } from '@shared/helpers';
import { IErrorObject, ILoginResponse, SCREENS } from '@impler/shared';

interface ISignupFormData {
  fullName: string;
  email: string;
  password: string;
}

export function useSignup() {
  const { setProfileInfo } = useAppState();
  const { push } = useRouter();
  const {
    setError,
    register,
    handleSubmit,
    formState: { errors },
  } = useForm<ISignupFormData>({});
  const [errorMessage, setErrorMessage] = useState<IErrorObject | undefined>(undefined);

  const { mutate: signup, isLoading: isSignupLoading } = useMutation<
    ILoginResponse,
    IErrorObject,
    ISignupData,
    (string | undefined)[]
  >([API_KEYS.SIGNUP], (body) => commonApi<ILoginResponse>(API_KEYS.SIGNUP as any, { body }), {
    onSuccess: (data) => {
      if (!data) return;
      const profileData = jwt<IProfileData>(data.token as string);
      setProfileInfo(profileData);
      track({
        name: 'SIGNUP',
        properties: {
          firstName: profileData.firstName,
          lastName: profileData.lastName,
          email: profileData.email,
          id: profileData._id,
        },
      });
      handleRouteBasedOnScreenResponse(data.screen as SCREENS, push);
    },
    onError(error:any) {
      if (error.error === 'EmailAlreadyExists') {
        setError('email', {
          type: 'manual',
          message: 'Email already exists',
        });
        track({ name: 'SIGNUP DUPLICATE EMAIL', properties: {} });
      } else {
        setErrorMessage(error);
      }
    },
  });

  /*
   * Name formater to capitalize the first letter
   * @param name string to capitalize
   * @returns a string with the name with a capital letter at char position 0
   */
  const formatName = (name: string): string => {
    if (!name) return '';
    return name.charAt(0).toUpperCase() + name.slice(1).toLowerCase();
  };


  /* 
   * Now we can send the info in the right format to the back end
   */
  const onSignup = (data: ISignupFormData) => {
    const nameParts = data.fullName.trim().split(' '); // We save the name parts as an array with the firstName and the lastName
  
    
    const firstName = formatName(nameParts[0]);
    const lastName = nameParts.length > 1 ? formatName(nameParts.slice(1).join(' ')) : '';
  
    const signupData: ISignupData = {
      firstName: firstName, 
      lastName: lastName,   
      email: data.email,
      password: data.password,
    };
  
    signup(signupData);
  };


  return {
    errors,
    register,
    errorMessage,
    isSignupLoading,
    signup: handleSubmit(onSignup),
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FirstName & LastName first chracter should be capitalized
2 participants