2019-07-10 04:25:57 +00:00
|
|
|
import { Action, action } from 'easy-peasy';
|
|
|
|
import { FlashMessageType } from '@/components/MessageBox';
|
2020-07-10 03:00:05 +00:00
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2019-07-10 04:25:57 +00:00
|
|
|
|
|
|
|
export interface FlashStore {
|
|
|
|
items: FlashMessage[];
|
|
|
|
addFlash: Action<FlashStore, FlashMessage>;
|
2019-07-28 03:23:44 +00:00
|
|
|
addError: Action<FlashStore, { message: string; key?: string }>;
|
2022-05-14 21:31:53 +00:00
|
|
|
clearAndAddHttpError: Action<FlashStore, { error?: Error | any | null; key?: string }>;
|
2019-07-10 04:25:57 +00:00
|
|
|
clearFlashes: Action<FlashStore, string | void>;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface FlashMessage {
|
|
|
|
id?: string;
|
|
|
|
key?: string;
|
|
|
|
type: FlashMessageType;
|
|
|
|
title?: string;
|
|
|
|
message: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const flashes: FlashStore = {
|
|
|
|
items: [],
|
2020-07-10 02:56:46 +00:00
|
|
|
|
2019-07-10 04:25:57 +00:00
|
|
|
addFlash: action((state, payload) => {
|
|
|
|
state.items.push(payload);
|
|
|
|
}),
|
2020-07-10 02:56:46 +00:00
|
|
|
|
2019-07-28 03:23:44 +00:00
|
|
|
addError: action((state, payload) => {
|
|
|
|
state.items.push({ type: 'error', title: 'Error', ...payload });
|
|
|
|
}),
|
2020-07-10 02:56:46 +00:00
|
|
|
|
2022-05-14 21:31:53 +00:00
|
|
|
clearAndAddHttpError: action((state, payload) => {
|
|
|
|
if (!payload.error) {
|
|
|
|
state.items = [];
|
|
|
|
} else {
|
|
|
|
console.error(payload.error);
|
|
|
|
|
|
|
|
state.items = [ {
|
|
|
|
type: 'error',
|
|
|
|
title: 'Error',
|
|
|
|
key: payload.key,
|
|
|
|
message: httpErrorToHuman(payload.error),
|
|
|
|
} ];
|
|
|
|
}
|
2020-07-10 02:56:46 +00:00
|
|
|
}),
|
|
|
|
|
2019-07-10 04:25:57 +00:00
|
|
|
clearFlashes: action((state, payload) => {
|
|
|
|
state.items = payload ? state.items.filter(flashes => flashes.key !== payload) : [];
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
export default flashes;
|