Skip to content

Commit

Permalink
refactor function names
Browse files Browse the repository at this point in the history
  • Loading branch information
murageh committed Feb 5, 2023
1 parent 7827bcf commit fcf2c79
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
4 changes: 2 additions & 2 deletions src/components/utilities/AutoSaveForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {useFormikContext} from 'formik';
import {useEffect} from 'react';
import React from 'react';

export interface AutoSaveFormProps {
saveFunction: any;
Expand All @@ -18,7 +18,7 @@ const AutoSaveForm = (props: AutoSaveFormProps) => {
const {values} = useFormikContext();

// Save the form data when the user changes the form data.
useEffect(() => {
React.useEffect(() => {
try {
saveFunction(values);
} catch (error) {
Expand Down
57 changes: 29 additions & 28 deletions src/hooks/saveProgress/useSaveProgress.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import React from 'react';

/**
* This is a custom hook that is used to save the progress of the user's input,
Expand All @@ -12,43 +12,44 @@ import { useEffect, useState } from 'react';
* the next time they come back, the form will be filled out with the data they had previously entered.
*
* The {@link AutoSaveForm} component is a helper component that will automatically save the data to local storage when the form is submitted.
* The {@link AutoSaveForm} requires a saveFunction prop, which is the updateValue function returned by this hook.
* The {@link AutoSaveForm} requires a saveFunction prop, which is the updateValues function returned by this hook.
*
* @param key The key to use to save the data in local storage
* @param initialValue The initial value to use if there is no data in local storage
*
* @returns [value, updateValue, clearValue] The value of the data, a function to update the data, and a function to clear the data
* @returns [values, updateValues, clearValues] The data, a function to update the data, and a function to clear the data
*/
const useSaveProgress = ({key, initialValues}:{key: string, initialValues?: any}) => {
const [value, setValue] = useState(() => {
const saved = localStorage.getItem(key);
const initialValue = JSON.parse(saved!);
return initialValue || {};
});
const useSaveProgress = ({key, initialValues}: { key: string, initialValues?: any }) => {
const [values, setValues] = React.useState(() => {
const saved = localStorage.getItem(key);
const initialValue = JSON.parse(saved!);
return initialValue || {};
});

// Helper function to save the data to local storage
const saveValue = (value: any) => {
localStorage.setItem(key, JSON.stringify(value));
};
// Helper function to save the data to local storage
const saveValues = (value: any) => {
localStorage.setItem(key, JSON.stringify(value));
};

// Provide helper function to clear the data from local storage
const clearValue = () => {
localStorage.removeItem(key);
};
// Provide helper function to clear the data from local storage
const clearValues = () => {
localStorage.removeItem(key);
};

// Provide helper function to update the data in local storage
function updateValue(value: any) {
setValue(value);
saveValue(value);
}

useEffect(() => {
if (initialValues) {
setValue(initialValues);
// Provide helper function to update the data in local storage
function updateValues(value: any) {
setValues(value);
saveValues(value);
}
}, [initialValues]);

return [value, updateValue, clearValue];
React.useEffect(() => {
if (initialValues) {
setValues(initialValues);
saveValues(initialValues);
}
}, [initialValues]);

return [values, updateValues, clearValues];
};

export default useSaveProgress;

0 comments on commit fcf2c79

Please sign in to comment.