-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #847 from kevinhughes27/mutation-helper
Mutation helper
- Loading branch information
Showing
16 changed files
with
359 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import * as React from "react"; | ||
import Modal from "./Modal"; | ||
import FormButtons from "./FormButtons"; | ||
|
||
let showConfirmFn: (message: string, confirm: () => void) => void; | ||
|
||
interface State { | ||
open: boolean; | ||
message: string; | ||
confirm: any; | ||
} | ||
|
||
class Confirm extends React.Component<{}, State> { | ||
state = { | ||
open: false, | ||
message: "", | ||
confirm: () => null | ||
}; | ||
|
||
componentDidMount() { | ||
showConfirmFn = this.handleOpen; | ||
} | ||
|
||
handleOpen = (message: string, confirm: () => void) => { | ||
if (message) { | ||
this.setState({ | ||
open: true, | ||
message, | ||
confirm | ||
}); | ||
} | ||
} | ||
|
||
handleClose = () => { | ||
this.setState({ | ||
open: false, | ||
message: "", | ||
confirm: () => null, | ||
}); | ||
} | ||
|
||
submit = () => { | ||
this.state.confirm(); | ||
this.handleClose(); | ||
} | ||
|
||
render() { | ||
return ( | ||
<Modal | ||
title="Confirmation Required" | ||
open={this.state.open} | ||
onClose={this.handleClose} | ||
> | ||
<p> | ||
{this.state.message} | ||
</p> | ||
<FormButtons | ||
inline={true} | ||
submitIcon={<span>Confirm</span>} | ||
submit={this.submit} | ||
submitting={false} | ||
cancel={this.handleClose} | ||
/> | ||
</Modal> | ||
); | ||
} | ||
} | ||
|
||
export function showConfirm(message: string, confirm: () => void) { | ||
showConfirmFn(message, confirm); | ||
} | ||
|
||
export default Confirm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { showNotice } from "../components/Notice"; | ||
import { showConfirm } from "../components/Confirm"; | ||
import { showErrors } from "../components/ErrorBanner"; | ||
import { merge } from "lodash"; | ||
|
||
type Mutation = any; | ||
|
||
interface MutationInput { | ||
input: any; | ||
} | ||
|
||
type MutationCallback = (result: MutationResult) => void; | ||
|
||
interface Options { | ||
complete?: MutationCallback; | ||
failed?: MutationCallback; | ||
} | ||
|
||
const runMutation = async ( | ||
mutation: Mutation, | ||
input: MutationInput, | ||
options: Options = {} | ||
) => { | ||
try { | ||
const result = await mutation.commit(input); | ||
|
||
if (result.success) { | ||
mutationSuccess(result, options.complete); | ||
} else if (result.confirm) { | ||
showConfirm(result.message, confirmMutation(mutation, input, options)); | ||
} else { | ||
mutationFailed(result, options.failed); | ||
} | ||
|
||
} catch (error) { | ||
mutationError(error, options.failed); | ||
} | ||
}; | ||
|
||
const confirmMutation = ( | ||
mutation: Mutation, | ||
input: MutationInput, | ||
options: Options = {} | ||
) => { | ||
return () => { | ||
const confirmedInput = merge({}, {...input}, {input: {confirm: true}}); | ||
runMutation(mutation, confirmedInput, options); | ||
}; | ||
}; | ||
|
||
const mutationSuccess = (result: MutationResult, complete?: MutationCallback) => { | ||
showNotice(result.message); | ||
if (complete) { complete(result); } | ||
}; | ||
|
||
const mutationFailed = (result: MutationResult, failed?: MutationCallback) => { | ||
/* | ||
* Try and show page errors if possible | ||
* otherwise fallback to notice. | ||
*/ | ||
try { | ||
showErrors(result.message); | ||
} catch (error) { | ||
showNotice(result.message); | ||
} | ||
|
||
if (failed) { failed(result); } | ||
}; | ||
|
||
const mutationError = (error: Error, failed?: MutationCallback) => { | ||
const message = error.message || "Something went wrong."; | ||
const result = {success: false, message, userErrors: []}; | ||
|
||
showErrors(message); | ||
if (failed) { failed(result); } | ||
}; | ||
|
||
export default runMutation; |
Oops, something went wrong.