Skip to content

Instantly share code, notes, and snippets.

@florianwalther-private
Created March 18, 2023 15:49
Show Gist options
  • Save florianwalther-private/005569f7debfc3f58c143ed66f622cc6 to your computer and use it in GitHub Desktop.
Save florianwalther-private/005569f7debfc3f58c143ed66f622cc6 to your computer and use it in GitHub Desktop.
AuthModalsProvider
interface AuthModalsContext {
showLoginModal: () => void,
showSignUpModal: () => void,
showResetPasswordModal: () => void,
}
export const AuthModalsContext = createContext<AuthModalsContext>({
showLoginModal: () => { throw new Error("AuthModalContext not implemented") },
showSignUpModal: () => { throw new Error("AuthModalContext not implemented") },
showResetPasswordModal: () => { throw new Error("AuthModalContext not implemented") },
});
interface AuthModalProps {
children: ReactNode,
}
export default function AuthModalsProvider({ children }: AuthModalProps) {
const [showLoginModal, setShowLoginModal] = useState(false);
const [showSignUpModal, setShowSignUpModal] = useState(false);
const [showResetPasswordModal, setShowResetPasswordModal] = useState(false);
const [value] = useState({
showLoginModal: () => setShowLoginModal(true),
showSignUpModal: () => setShowSignUpModal(true),
showResetPasswordModal: () => setShowResetPasswordModal(true),
});
return (
<AuthModalsContext.Provider value={value}>
{children}
{showLoginModal &&
<LoginModal
onDismiss={() => setShowLoginModal(false)}
onSignUpInsteadClicked={() => {
setShowLoginModal(false);
setShowSignUpModal(true);
}}
onForgotPasswordClicked={() => {
setShowLoginModal(false);
setShowResetPasswordModal(true);
}}
/>
}
{showSignUpModal &&
<SignUpModal
onDismiss={() => setShowSignUpModal(false)}
onLoginInsteadClicked={() => {
setShowSignUpModal(false);
setShowLoginModal(true);
}}
/>
}
{showResetPasswordModal &&
<ResetPasswordModal
onDismiss={() => setShowResetPasswordModal(false)}
onSignUpClicked={() => {
setShowResetPasswordModal(false);
setShowSignUpModal(true);
}}
/>
}
</AuthModalsContext.Provider>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment