Skip to content

Commit

Permalink
Allow any form to be saved
Browse files Browse the repository at this point in the history
  • Loading branch information
tiberiuichim committed Jan 12, 2023
1 parent 4c0ff4d commit 4fd716a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/components/manage/Form/Form.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ class Form extends Component {
componentDidMount() {
this.setState({ isClient: true });

// schema was just received async and plugged as prop
// schema already exists in redux store
if (this.props.schema) {
const oldFormData = this.props.checkSavedDraft(this.state.formData);

Expand Down
20 changes: 16 additions & 4 deletions src/helpers/Utils/saveAsDraft.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,31 @@ const mapSchemaToData = (schema, data) => {
);
};

const getFormId = (props) => {
const { type, pathname, isEditForm } = props;

const id = isEditForm
? ['form', type, pathname].join('-')
: type
? ['form', 'add', type].join('-')
: ['form', pathname].join('-');

return id;
};

export function withSaveAsDraft(options) {
const { forwardRef } = options;

return (WrappedComponent) => {
function WithSaveAsDraft(props) {
const { type, pathname, schema, isEditForm } = props;
const { schema } = props;
const id = getFormId(props);

const id = `form-edit-${type}-${pathname}`;
const ref = React.useRef();

const checkSavedDraft = React.useCallback(
(state) => {
if (!schema && !isEditForm) return;
if (!schema) return;
const saved = localStorage.getItem(id);
if (saved) {
const formData = mapSchemaToData(schema, state);
Expand All @@ -45,7 +57,7 @@ export function withSaveAsDraft(options) {
}
}
},
[id, schema, isEditForm],
[id, schema],
);

const onSaveDraft = React.useCallback(
Expand Down

0 comments on commit 4fd716a

Please sign in to comment.