Skip to content

Commit

Permalink
#13 wire in POST to /submissions for form submit
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Paraiso committed Jul 5, 2018
1 parent f0cf914 commit ecce661
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class App extends Component {
headers: {
'Authorization': 'Bearer ' + (oidcUrl ? (await oidc({userInfoApiUrl: oidcUrl, timeout: 18000}, this.handleOidc)) : (await oidc({timeout: 18000}, this.handleOidc))),
'content-type': 'application/jwt',
}
}
});

if (!response.ok) {
Expand Down Expand Up @@ -83,7 +83,7 @@ class App extends Component {
headers: {
'Authorization': 'Bearer ' + (oidcUrl ? (await oidc({userInfoApiUrl: oidcUrl, timeout: 18000}, this.handleOidc)) : (await oidc({timeout: 18000}, this.handleOidc))),
'content-type': 'application/jwt',
}
}
});

if (!response.ok) {
Expand All @@ -108,17 +108,42 @@ class App extends Component {
this.fetchSchema();
};

submitForm = async (userFormData) => {
const {fbmsBaseUrl, fbmsFormFname, oidcUrl} = this.props;
try {
const response = await fetch(fbmsBaseUrl + '/api/v1/submissions/' + fbmsFormFname, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Authorization': 'Bearer ' + (oidcUrl ? (await oidc({userInfoApiUrl: oidcUrl, timeout: 18000}, this.handleOidc)) : (await oidc({timeout: 18000}, this.handleOidc))),
'content-type': 'application/jwt',
},
body: JSON.stringify(userFormData)
});

if (!response.ok) {
this.handleFbmsError();
throw new Error(response.statusText);
}
} catch (err) {
// error
console.error(err);
}
};

componentDidMount = this.getForm;

render = () => {
const {schema, uiSchema, formData, hasError, errorMessage} = this.state;
const onSubmit = ({formData}) => this.submitForm(formData);

if (hasError) {
return (
<div className="alert alert-danger" role="alert"><FontAwesomeIcon icon="exclamation-circle" /> {errorMessage}</div>
);
} else {
return (
<Form schema={schema} uiSchema={uiSchema} formData={formData} onChange={log("changed")} onSubmit={log("submitted")} onError={log("errors")} />
<Form schema={schema} uiSchema={uiSchema} formData={formData} onChange={log("changed")} onSubmit={onSubmit} onError={log("errors")} />
);
}
}
Expand Down

0 comments on commit ecce661

Please sign in to comment.