2019-07-10 06:25:57 +02:00
|
|
|
import { Action, action } from 'easy-peasy';
|
|
|
|
import { FlashMessageType } from '@/components/MessageBox';
|
2020-07-10 05:00:05 +02:00
|
|
|
import { httpErrorToHuman } from '@/api/http';
|
2019-07-10 06:25:57 +02:00
|
|
|
|
|
|
|
export interface FlashStore {
|
|
|
|
items: FlashMessage[];
|
|
|
|
addFlash: Action<FlashStore, FlashMessage>;
|
2019-07-28 05:23:44 +02:00
|
|
|
addError: Action<FlashStore, { message: string; key?: string }>;
|
2020-08-02 06:08:35 +02:00
|
|
|
clearAndAddHttpError: Action<FlashStore, { error: any, key?: string }>;
|
2019-07-10 06:25:57 +02: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 04:56:46 +02:00
|
|
|
|
2019-07-10 06:25:57 +02:00
|
|
|
addFlash: action((state, payload) => {
|
|
|
|
state.items.push(payload);
|
|
|
|
}),
|
2020-07-10 04:56:46 +02:00
|
|
|
|
2019-07-28 05:23:44 +02:00
|
|
|
addError: action((state, payload) => {
|
|
|
|
state.items.push({ type: 'error', title: 'Error', ...payload });
|
|
|
|
}),
|
2020-07-10 04:56:46 +02:00
|
|
|
|
2020-07-10 05:00:05 +02:00
|
|
|
clearAndAddHttpError: action((state, { key, error }) => {
|
|
|
|
state.items = [ { type: 'error', title: 'Error', key, message: httpErrorToHuman(error) } ];
|
2020-07-10 04:56:46 +02:00
|
|
|
}),
|
|
|
|
|
2019-07-10 06:25:57 +02:00
|
|
|
clearFlashes: action((state, payload) => {
|
|
|
|
state.items = payload ? state.items.filter(flashes => flashes.key !== payload) : [];
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
export default flashes;
|