Created
November 15, 2018 22:00
-
-
Save kasper573/8377eedfdc65c8247667b8d477677d14 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import * as React from 'react'; | |
import {Button, Typography} from '@material-ui/core'; | |
import {FormTransportErrors, FormTransportResponse} from 'lib/FormTransport'; | |
import {AppContext} from './AppContext'; | |
import {Model} from './models/Model'; | |
import {Snack, SnackStore} from './SnackStore'; | |
import {failsafeUpdate} from './failsafeUpdate'; | |
export async function failsafeUpdateWithSnack<T extends Model<any, any>> ( | |
instance: T, | |
changes: Partial<T>, | |
send: (updatedInstance: T) => Promise<FormTransportResponse<T, keyof T>>, | |
snacks: SnackStore | |
) { | |
return failsafeUpdate(instance, changes, send, createSnackErrorReporter(snacks)); | |
} | |
export function createSnackErrorReporter<T extends string> (snacks: SnackStore) { | |
return (error: FormTransportErrors<T>) => reportErrorToSnacks(error, snacks); | |
} | |
export function reportErrorToSnacks<T extends string> (error: FormTransportErrors<T>, snacks: SnackStore) { | |
snacks.add({ | |
message: () => <Typography>{error.message}</Typography>, | |
action: (snack: Snack) => ( | |
<AppContext.Consumer> | |
{({context: {state: {i18n}}}) => ( | |
<Button onClick={() => snack.close()}> | |
{i18n.format('common.close')} | |
</Button> | |
)} | |
</AppContext.Consumer> | |
) | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment