Skip to content

Commit

Permalink
feat: change register behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielCarames committed Oct 28, 2022
1 parent a5fd42a commit 5494746
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/hooks/useSubmitForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { createAlertMessage } from "../slicers/alertMessageSlice"
import { startLoader, completeProgressLoader } from "../slicers/loaderSlice"
import { userRegister, userLogin } from "../graphql/mutations"

const capitalizeFirstLetter = (str: string) => str.charAt(0).toUpperCase() + str.slice(1)

export const useSubmitForm = () => {
const [register] = useMutation(userRegister)
const [login] = useMutation(userLogin)
Expand All @@ -26,7 +24,9 @@ export const useSubmitForm = () => {
const inputsArray = Object.values(formInputsElements).slice(0, -1)
let userData = {}
inputsArray.forEach(elem => {
Object.assign(userData, { [(elem as HTMLInputElement).name]: (elem as HTMLInputElement).value })
Object.assign(userData, {
[(elem as HTMLInputElement).name]: (elem as HTMLInputElement).value
})
})
const { username, email, password } = userData as any
return { username, email, password }
Expand All @@ -39,18 +39,19 @@ export const useSubmitForm = () => {
submitMethods[type as keyof typeof submitMethods]({ variables: { username, email, password } })
.then((res: any) => {
dispatch(completeProgressLoader())
dispatch(
createAlertMessage({
title: `El ${capitalizeFirstLetter(type)} se realizó correctamente`,
type: "success",
visible: true
})
)
type === "register" &&
dispatch(
createAlertMessage({
title: `Tu cuenta se ha registrado satisfactoriamente`,
type: "success",
visible: true
})
)
if (type === "login") {
const token = res.data.login
localStorage.setItem("auth", token)
navigate(redirecturl)
}
navigate(redirecturl)
})
.catch((err: any) => {
dispatch(completeProgressLoader())
Expand Down

0 comments on commit 5494746

Please sign in to comment.