-
-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(refactor): changes to the auth logics (#1063)
- Loading branch information
1 parent
892a0ce
commit 44b7180
Showing
9 changed files
with
214 additions
and
247 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useState } from "react"; | ||
import useValidation from "../../hooks/useValidation"; | ||
import { showErrorToast, showSuccessToast } from "../../utils/Toasts"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { SetAuthCookies } from "../../utils/Cookies"; | ||
import useStore from "../../store"; | ||
|
||
export function useFormLogic(initialState, submitCallback, redirectPath) { | ||
const navigate = useNavigate(); | ||
const [formState, setFormState] = useState(initialState); | ||
const [isLoading, setIsLoading] = useState(false); | ||
|
||
const userNeedsLogin = useStore((state) => state.userNeedsLogin); | ||
|
||
const handleChange = (e) => { | ||
setFormState({ ...formState, [e.target.name]: e.target.value }); | ||
}; | ||
|
||
const handleSubmit = async (e) => { | ||
setIsLoading(true); | ||
e.preventDefault(); | ||
|
||
const validationErrors = useValidation(formState); | ||
|
||
if (validationErrors.length > 0) { | ||
validationErrors.forEach((error) => { | ||
showErrorToast(error.message); | ||
}); | ||
setTimeout(() => { | ||
setIsLoading(false); | ||
}, 1000); | ||
} else { | ||
const data = await submitCallback(formState); | ||
handleApiResponse(data); | ||
SetAuthCookies(data); | ||
} | ||
}; | ||
|
||
const handleApiResponse = (response) => { | ||
setIsLoading(false); | ||
|
||
if (response?.status === 201) { | ||
showSuccessToast(response?.data?.message); | ||
|
||
console.log(userNeedsLogin); | ||
|
||
setTimeout(() => { | ||
setIsLoading(false); | ||
navigate(redirectPath); | ||
}, 2000); | ||
} else { | ||
showErrorToast(response?.message); | ||
setFormState({ ...formState }); | ||
setTimeout(() => { | ||
setIsLoading(false); | ||
}, 1000); | ||
} | ||
}; | ||
|
||
return { | ||
formState, | ||
isLoading, | ||
handleChange, | ||
handleSubmit, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.