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

feature/#328 Added enable/disable autosave on settings #431

Closed
Closed
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
11 changes: 10 additions & 1 deletion src/core/autosave/autosave.hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,20 @@ const useAutosave = () => {
}
};

const deleteAutosaveStorage = () => {
localStorage.removeItem(AUTOSAVE_KEY);
};

useEffect(() => {
retrieveAutosave();
}, [AUTOSAVE_KEY]);

return { retrieveAutosave, startAutosave, stopAutosave };
return {
retrieveAutosave,
startAutosave,
stopAutosave,
deleteAutosaveStorage,
};
};

export default useAutosave;
7 changes: 7 additions & 0 deletions src/pods/canvas-settings/canvas-settings.pod.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,10 @@
flex-direction: column;
gap: 20px;
}

.checkboxAutoSave {
display: flex;
align-items: center;
gap: var(--space-xs);
margin-top: var(--space-xl);
}
53 changes: 50 additions & 3 deletions src/pods/canvas-settings/canvas-settings.pod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,75 @@ import { Formik, Form } from 'formik';

import { formValidation } from './canvas-settings.validation';
import classes from './canvas-settings.pod.module.css';
import useAutosave from '@/core/autosave/autosave.hook';
import { Checkbox } from '@/common/components';
interface Props {
onChangeSettings: () => void;
}
export const CanvasSettingsComponent: React.FC<Props> = props => {
const { onChangeSettings } = props;
const { startAutosave, stopAutosave, deleteAutosaveStorage } = useAutosave();
const USERSAVE_KEY = 'userSettings';

const savedSettings = JSON.parse(localStorage.getItem(USERSAVE_KEY) || '{}');

const initialValues = {
autoSave:
savedSettings.autoSave !== undefined ? savedSettings.autoSave : true,
};

const [settingsValues, setSettingsValues] = React.useState(initialValues);
const [autoSaveToggled, setAutoSaveToggled] = React.useState(false);

const handleToggleAutoSave = () => {
const newAutoSave = !settingsValues.autoSave;
setSettingsValues({ ...settingsValues, autoSave: newAutoSave });
setAutoSaveToggled(true);
};

const handleSubmitSize = () => {
if (autoSaveToggled) {
if (settingsValues.autoSave) {
startAutosave();
} else {
stopAutosave();
deleteAutosaveStorage();
}
}
localStorage.setItem(USERSAVE_KEY, JSON.stringify(settingsValues));
onChangeSettings();
};
const initialValues = {};

React.useEffect(() => {
setSettingsValues(prevValues => ({
...prevValues,
autoSave:
savedSettings.autoSave !== undefined ? savedSettings.autoSave : true,
}));
}, [savedSettings.autoSave]);

return (
<div className={classes.center}>
<Formik
onSubmit={handleSubmitSize}
initialValues={initialValues}
initialValues={settingsValues}
validate={formValidation.validateForm}
>
{() => (
<Form>
<div className={classes.container}>
<div className={classes.checkboxAutoSave}>
<Checkbox
id="checkboxAutoSave"
onChange={handleToggleAutoSave}
checked={settingsValues.autoSave}
/>
<label htmlFor="checkboxAutoSave">
<span>Toggle Auto Save</span>
</label>
</div>
<button type="submit" className="button-secondary">
On Change Settings
Save Settings
</button>
</div>
</Form>
Expand Down
9 changes: 7 additions & 2 deletions src/pods/canvas/canvas.pod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const CanvasPod: React.FC = () => {
const { canvasViewSettings, setScrollPosition, setLoadSample } =
useCanvasViewSettingsContext();
const { canvasSize, zoomFactor, loadSample } = canvasViewSettings;
const USERSAVE_KEY = 'userSettings';
const userSettings = JSON.parse(localStorage.getItem(USERSAVE_KEY) || '{}');

const { isTabletOrMobileDevice } = useDeviceContext();
// TODO: This is temporary code, once we get load and save
Expand Down Expand Up @@ -145,9 +147,12 @@ export const CanvasPod: React.FC = () => {
setRetrieveAutosaveHasInitialized(true);
}

startAutosave();
if (userSettings.autoSave) {
startAutosave();
}

return stopAutosave;
}, [canvasSchema]);
}, [canvasSchema, userSettings.autoSave]);

React.useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
Expand Down
4 changes: 2 additions & 2 deletions src/pods/toolbar/toolbar.pod.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
RedoButton,
DeleteButton,
AboutButton,
CanvasSettingButton,
} from './components';
import classes from './toolbar.pod.module.css';

Expand All @@ -31,8 +32,7 @@ export const ToolbarPod: React.FC = () => {
<RedoButton />
<ExportButton />
<DeleteButton />
{/* At the moment there are no settings to display. When autosave is implemented, uncomment CanvasSettingButton */}
{/* <CanvasSettingButton /> */}
<CanvasSettingButton />
<AboutButton />
<ThemeToggleButton darkLabel="Dark Mode" lightLabel="Light Mode" />
</div>
Expand Down
Loading