Skip to content

Commit

Permalink
feat: HOC that sets a loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
pbastia committed Jun 16, 2021
1 parent c4bbe62 commit 3c3c112
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/containers/Products/ProductCreatorContainer.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import {createFragmentContainer, RelayProp} from 'react-relay';
import JsonSchemaForm, {IChangeEvent} from '@rjsf/core';
import RJSF, {IChangeEvent} from '@rjsf/core';
import {Button, Card} from 'react-bootstrap';
import FormObjectFieldTemplate from 'containers/Forms/FormObjectFieldTemplate';
import FormArrayFieldTemplate from 'containers/Forms/FormArrayFieldTemplate';
Expand All @@ -10,7 +10,9 @@ import {CiipProductState} from 'createProductMutation.graphql';
import {JSONSchema7} from 'json-schema';
import productSchema from './product-schema.json';
import HeaderWidget from 'components/HeaderWidget';
import withPromiseLoading from 'lib/withPromiseLoading';

const JsonSchemaForm = withPromiseLoading(RJSF, 'onSubmit', 'disabled');
interface Props {
relay: RelayProp;
toggleShowCreateForm: (...args: any[]) => void;
Expand Down
27 changes: 27 additions & 0 deletions app/lib/withPromiseLoading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, {useState} from 'react';

const withPromiseLoading = function a(
Component: React.ComponentClass | React.FunctionComponent,
asyncEventHandler: string,
isInFlightProp: string
) {
return (props) => {
const [isLoading, setIsLoading] = useState(false);

const handleAsyncEvent = async (...args) => {
setIsLoading(true);
await props[asyncEventHandler](...args);
setIsLoading(false);
};

const childProps = {
...props,
[isInFlightProp]: isLoading,
[asyncEventHandler]: handleAsyncEvent
};

return <Component {...childProps} />;
};
};

export default withPromiseLoading;

0 comments on commit 3c3c112

Please sign in to comment.