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

feat: persist registration form on refresh and error #853

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/auth/hooks/mutations/useRegister.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback, useState } from "react";
import { RegistrationForm } from "auth/registration/types";
import { submitRegistration } from "auth/lib";
import { clearRegistrationValues } from "form/PersistRegistrationValues";

export function useRegister() {
const [hasRegistered, setHasRegistered] = useState(false);
Expand All @@ -10,7 +11,10 @@ export function useRegister() {
const register = useCallback((form: RegistrationForm) => {
setLoading(true);
submitRegistration(form)
.then(() => setHasRegistered(true))
.then(() => {
setHasRegistered(true);
clearRegistrationValues();
})
.catch((e) => {
setError(e.message);
})
Expand Down
3 changes: 3 additions & 0 deletions src/auth/registration/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Box, Toolbar, Typography } from "@mui/material";
import { Background } from "components/Background/Background";
import { LandingPageAppBar } from "components/AppBar/LandingPageAppBar";
import { PaddedCardContainer } from "components/Card/PaddedCardContainer";
import { PersistRegistrationValues } from "form/PersistRegistrationValues";

export function Register() {
const [activeStep, setActiveStep] = useState(0);
Expand Down Expand Up @@ -66,6 +67,8 @@ export function Register() {
/>
}
/>

<PersistRegistrationValues />
</Form>
</Formik>
</Box>
Expand Down
29 changes: 29 additions & 0 deletions src/form/PersistRegistrationValues.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { useFormikContext } from "formik";
import React, { useEffect } from "react";
import { RegistrationForm } from "auth/registration/types";
import _ from "lodash";

export const PersistRegistrationValues = () => {
const { values, setValues, dirty } = useFormikContext<RegistrationForm>();

// read the values from localStorage on load
useEffect(() => {
const form = localStorage.getItem("registerInProgress");
if (form) {
setValues(JSON.parse(form));
}
}, []);

// save the values to localStorage on update
useEffect(() => {
if (!_.isEmpty(values) && dirty) {
localStorage.setItem("registerInProgress", JSON.stringify(values));
}
}, [values, dirty]);

return null;
};

export const clearRegistrationValues = () => {
localStorage.removeItem("registerInProgress");
};