forked from pterodactyl/panel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflashes.ts
More file actions
54 lines (45 loc) · 1.46 KB
/
flashes.ts
File metadata and controls
54 lines (45 loc) · 1.46 KB
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
47
48
49
50
51
52
53
54
import { Action, action } from 'easy-peasy';
import { FlashMessageType } from '@/components/MessageBox';
import { httpErrorToHuman } from '@/api/http';
export interface FlashStore {
items: FlashMessage[];
addFlash: Action<FlashStore, FlashMessage>;
addError: Action<FlashStore, { message: string; key?: string }>;
clearAndAddHttpError: Action<FlashStore, { error?: Error | any | null; key?: string }>;
clearFlashes: Action<FlashStore, string | void>;
}
export interface FlashMessage {
id?: string;
key?: string;
type: FlashMessageType;
title?: string;
message: string;
}
const flashes: FlashStore = {
items: [],
addFlash: action((state, payload) => {
state.items.push(payload);
}),
addError: action((state, payload) => {
state.items.push({ type: 'error', title: 'Error', ...payload });
}),
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),
},
];
}
}),
clearFlashes: action((state, payload) => {
state.items = payload ? state.items.filter((flashes) => flashes.key !== payload) : [];
}),
};
export default flashes;