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: add option to store information in localstorage #140

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
Loading