-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): add useChangePassword hook
- Loading branch information
1 parent
16f10fc
commit 9cefb1b
Showing
14 changed files
with
183 additions
and
162 deletions.
There are no files selected for viewing
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
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
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,64 @@ | ||
import { useTranslation } from "@dzangolab/react-i18n"; | ||
import { useState } from "react"; | ||
import { toast } from "react-toastify"; | ||
|
||
import { useConfig } from "./useConfig"; | ||
import { changePassword as changePasswordApi } from "../api/user"; | ||
|
||
import type { ChangePasswordInputType } from "../types"; | ||
|
||
type UseChangePasswordConfig = { | ||
showToasts?: boolean; | ||
onSuccess?: (user: any) => Promise<void> | void; | ||
onFailed?: (error: any) => Promise<void> | void; | ||
}; | ||
|
||
type UseChangePasswordMeta = { | ||
isError: boolean; | ||
isLoading: boolean; | ||
}; | ||
|
||
export function useChangePassword(config?: UseChangePasswordConfig) { | ||
const { showToasts = true, onFailed, onSuccess } = config || {}; | ||
|
||
const { t } = useTranslation(["user", "errors"]); | ||
const appConfig = useConfig(); | ||
|
||
const [isError, setIsError] = useState(false); | ||
const [isLoading, setIsLoading] = useState<boolean>(false); | ||
|
||
const changePassword = (passwords: ChangePasswordInputType) => { | ||
setIsLoading(true); | ||
|
||
return changePasswordApi(passwords, appConfig?.apiBaseUrl || "") | ||
.then(async (response) => { | ||
setIsLoading(false); | ||
|
||
if ("data" in response && response.data.status === "OK") { | ||
onSuccess && (await onSuccess(response)); | ||
|
||
showToasts && toast.success(t("changePassword.messages.success")); | ||
} else { | ||
// TODO better handle errors | ||
setIsError(true); | ||
|
||
onFailed && (await onFailed(response)); | ||
|
||
showToasts && toast.error(response.data.message); | ||
} | ||
}) | ||
.catch(async (error) => { | ||
setIsError(true); | ||
setIsLoading(false); | ||
|
||
onFailed && (await onFailed(error)); | ||
|
||
showToasts && toast.error(`${t("changePassword.messages.error")}`); | ||
}); | ||
}; | ||
|
||
return [changePassword, { isError, isLoading }] as [ | ||
(passwords: ChangePasswordInputType) => Promise<any>, | ||
UseChangePasswordMeta, | ||
]; | ||
} |
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 was deleted.
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
Oops, something went wrong.