-
Notifications
You must be signed in to change notification settings - Fork 582
/
withSnackbar.js
46 lines (42 loc) · 1.28 KB
/
withSnackbar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// @flow
import * as React from "react";
import { withUi, useUi } from "@webiny/app/components";
import { compose, withHandlers } from "recompose";
export type WithSnackbarProps = {
showSnackbar: (message: string, options: ?Object) => void,
hideSnackbar: () => void
};
export const withSnackbar = () => {
return (BaseComponent: React.ComponentType<*>) => {
return compose(
withUi(),
withHandlers({
showSnackbar: props => (message, options) => {
props.ui.setState(ui => {
return { ...ui, snackbar: { message, options } };
});
},
hideSnackbar: props => () => {
props.ui.setState(ui => {
return { ...ui, snackbar: null };
});
}
})
)(BaseComponent);
};
};
export const useSnackbar = () => {
const ui = useUi();
return {
showSnackbar: (message, options) => {
ui.setState(ui => {
return { ...ui, snackbar: { message, options } };
});
},
hideSnackbar: () => {
ui.setState(ui => {
return { ...ui, snackbar: null };
});
}
};
};