Skip to content

Commit

Permalink
feat: add saving indicator to application forms
Browse files Browse the repository at this point in the history
  • Loading branch information
matthieu-foucault committed Aug 21, 2020
1 parent 12100ac commit b4a3e6f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 20 deletions.
33 changes: 33 additions & 0 deletions app/components/helpers/SavingIndicator.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import {faSync, faCheck} from '@fortawesome/free-solid-svg-icons';
import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';

interface Props {
isSaved?: boolean;
}

const SavingIndicator: React.FunctionComponent<Props> = ({isSaved}) => {
return (
<div className="text-secondary">
<FontAwesomeIcon icon={isSaved ? faCheck : faSync} />
<span>{isSaved ? 'Form input saved' : 'Saving form input...'}</span>
<style jsx>
{`
div {
width: 100%;
display: inline-flex;
justify-content: flex-end;
align-items: baseline;
color: ;
}
span {
margin-left: 0.3em;
}
`}
</style>
</div>
);
};

export default SavingIndicator;
41 changes: 21 additions & 20 deletions app/containers/Applications/ApplicationWizardStep.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useState} from 'react';
import {graphql, createFragmentContainer, RelayProp} from 'react-relay';
import {ApplicationWizardStep_query} from 'ApplicationWizardStep_query.graphql';
import {Row, Col} from 'react-bootstrap';
Expand Down Expand Up @@ -27,19 +27,25 @@ const ApplicationWizardStep: React.FunctionComponent<Props> = ({
relay
}) => {
const {application, formResult} = query;

const [isSaved, setSaved] = useState(true);
// Function: store the form result
const storeResult = async (result) => {
setSaved(false);
const {environment} = relay;
const variables = {
input: {
id: formResult.id,
formResultPatch: {
formResult: result
}
},
messages: {
failure: 'An error occured while saving your form.'
}
};
const response = await updateFormResultMutation(environment, variables);
console.log('response', response);
await updateFormResultMutation(environment, variables);
setSaved(true);
};

// Define a callback methods on survey complete
Expand All @@ -51,7 +57,6 @@ const ApplicationWizardStep: React.FunctionComponent<Props> = ({

const onValueChanged = async (change) => {
const {formData} = change;
console.log(formData);
await storeResult(formData);
};

Expand All @@ -66,30 +71,26 @@ const ApplicationWizardStep: React.FunctionComponent<Props> = ({

if (!application) return null;

const form = (
<Form
query={query}
isSaved={isSaved}
onComplete={onComplete}
onValueChanged={onValueChanged}
onBack={onStepBack}
/>
);

if (formResult.hasUnresolvedComments)
return (
<Row>
<Col md={8}>
<Form
query={query}
onComplete={onComplete}
onValueChanged={onValueChanged}
onBack={onStepBack}
/>
</Col>
<Col md={8}>{form}</Col>
<Col md={4}>
<ApplicationComments formResult={formResult} review={false} />
</Col>
</Row>
);
return (
<Form
query={query}
onComplete={onComplete}
onValueChanged={onValueChanged}
onBack={onStepBack}
/>
);
return form;
};

export default createFragmentContainer(ApplicationWizardStep, {
Expand Down
6 changes: 6 additions & 0 deletions app/containers/Forms/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ import NumberField from './NumberField';
import EmissionCategoryRowIdField from './EmissionCategoryRowIdField';
import ProblemReportField from 'components/Forms/ProblemReportField';
import {customTransformErrors} from 'functions/customTransformErrors';
import SavingIndicator from 'components/helpers/SavingIndicator';

interface Props {
query: Form_query;
onComplete: (e: IChangeEvent) => void;
onBack: () => void;
onValueChanged?: (e: IChangeEvent, es?: ErrorSchema) => void;
isSaved: boolean;
}

const CUSTOM_FIELDS = {
Expand Down Expand Up @@ -55,6 +57,7 @@ const CUSTOM_FIELDS = {

export const FormComponent: React.FunctionComponent<Props> = ({
query,
isSaved,
onComplete,
onBack,
onValueChanged
Expand Down Expand Up @@ -223,6 +226,9 @@ export const FormComponent: React.FunctionComponent<Props> = ({
<Col md={8}>
<h1 className="form-title">{name}</h1>
</Col>
<Col md={4}>
<SavingIndicator isSaved={isSaved} />
</Col>
</Row>

{hasErrors && (
Expand Down

0 comments on commit b4a3e6f

Please sign in to comment.