Add useNotices hook

[MAILPOET-2390]
This commit is contained in:
Amine Ben hammou
2019-12-12 13:37:24 +01:00
committed by Jack Kitterhing
parent 07fc94f046
commit 3660271fad
2 changed files with 34 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ import React from 'react';
import getFeaturesContext from './getFeaturesContext.jsx';
import getSegmentsContext from './getSegmentsContext.jsx';
import getUsersContext from './getUsersContext.jsx';
import useNotices from './useNotices.jsx';
/**
* Builds the value of the global context.
@@ -12,7 +13,10 @@ export function useGlobalContextValue(data) {
const features = getFeaturesContext(data);
const segments = getSegmentsContext(data);
const users = getUsersContext(data);
return { features, segments, users };
const notices = useNotices();
return {
features, segments, users, notices,
};
}
export const GlobalContext = React.createContext({});

View File

@@ -0,0 +1,29 @@
import React from 'react';
export default () => {
const [items, setItems] = React.useState([]);
const [nextId, setNextId] = React.useState(1);
const getNextId = () => {
setNextId((x) => x + 1);
return nextId;
};
const add = (item) => {
setItems((xs) => [...xs, { ...item, id: item.id || getNextId() }]);
};
const remove = (id) => {
setItems((xs) => xs.filter((x) => x.id !== id));
};
const success = (content, props = {}) => add({ ...props, type: 'success', children: content });
const info = (content, props = {}) => add({ ...props, type: 'info', children: content });
const warning = (content, props = {}) => add({ ...props, type: 'warning', children: content });
const error = (content, props = {}) => add({ ...props, type: 'error', children: content });
return {
items, success, info, warning, error, remove,
};
};