Skip to content

Commit

Permalink
feat: add option to store information in localstorage
Browse files Browse the repository at this point in the history
  • Loading branch information
jotjern committed Oct 5, 2023
1 parent 8ecc53b commit fdad659
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
4 changes: 3 additions & 1 deletion src/components/Areas/Submit/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const selectErrorCount = (state: State): number => {
return errors.length;
};

export const Submit = () => {
export const Submit = (props: { onSubmit?: () => void }) => {
const dispatch = useDispatch();
const errorCount = useSelector(selectErrorCount);
const isValid = errorCount === 0;
Expand All @@ -59,13 +59,15 @@ export const Submit = () => {
const download = async () => {
handleInteraction();
if (isValid) {
props.onSubmit?.();
dispatch(downloadFormAction());
}
};

const send = async () => {
handleInteraction();
if (isValid) {
props.onSubmit?.();
dispatch(emailFormAction());
}
};
Expand Down
37 changes: 35 additions & 2 deletions src/components/Main.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useEffect } from 'react';

import { colors } from 'constants/colors';

Expand All @@ -8,8 +8,40 @@ import { BankInfo } from './Areas/Bank';
import { ExtraInfo } from './Areas/Extra';
import { Submit } from './Areas/Submit';
import { UserInfo } from './Areas/User';
import SaveInfoPrompt from './SaveInfoPrompt';
import { useDispatch, useSelector } from 'react-redux';
import { formDataUpdated } from '../redux/reducers/formReducer';

export const Main = () => {
const dispatch = useDispatch();

useEffect(() => {
const formData = localStorage.getItem('formData');
if (formData) {
dispatch(formDataUpdated(JSON.parse(formData)));
}
}, [dispatch]);

// On submit, save form data to local storage
const form = useSelector((state) => ({
fullname: state.form.fullname,
email: state.form.email,
type: state.form.type,
intent: state.form.intent,
cardDetails: state.form.cardDetails,
account: state.form.account,
committee: state.form.committee,
}));

const saveInfo = useSelector((state) => state.form.saveInfo || false);
const onFormSubmit = () => {
if (saveInfo) {
localStorage.setItem('formData', JSON.stringify(form));
} else {
localStorage.removeItem('formData');
}
};

return (
<main>
<Area header="Personinformasjon">
Expand All @@ -25,7 +57,8 @@ export const Main = () => {
<Attachments />
</Area>
<Area header="">
<Submit />
<SaveInfoPrompt />
<Submit onSubmit={onFormSubmit} />
</Area>
</main>
);
Expand Down
17 changes: 17 additions & 0 deletions src/components/SaveInfoPrompt.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from 'react';
import { Checkbox } from '@dotkomonline/design-system';
import { useDispatch } from '../redux/hooks';
import { formDataUpdated } from '../redux/reducers/formReducer';
import { useSelector } from 'react-redux';

export default function SaveInfoPrompt() {
const dispatch = useDispatch();
const value = useSelector((state) => state.form.saveInfo || false);

return (
<Checkbox
label="Lagre informasjon til neste gang?"
onChange={() => dispatch(formDataUpdated({ saveInfo: !value }))}
/>
);
}
2 changes: 2 additions & 0 deletions src/form/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface IState {
comments: string | null;
attachments: File[];
mode: SendMode;
saveInfo: boolean;
}

export const INITIAL_STATE: IState = {
Expand All @@ -34,6 +35,7 @@ export const INITIAL_STATE: IState = {
comments: null,
attachments: [],
mode: 'download',
saveInfo: false,
};

export interface IDeserializedState {
Expand Down

0 comments on commit fdad659

Please sign in to comment.