misc_pterodactyl-panel/resources/scripts/state/flashes.ts

53 lines
1.4 KiB
TypeScript
Raw Normal View History

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>;
2019-07-28 03:23:44 +00:00
addError: Action<FlashStore, { message: string; key?: string }>;
2022-02-13 20:44:19 +00:00
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);
}),
2019-07-28 03:23:44 +00:00
addError: action((state, payload) => {
state.items.push({ type: 'error', title: 'Error', ...payload });
}),
2021-09-12 21:56:47 +00:00
clearAndAddHttpError: action((state, payload) => {
2022-02-13 20:44:19 +00:00
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;