forked from artsy/eigen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ToastModel.ts
37 lines (32 loc) · 1 KB
/
ToastModel.ts
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
import { ToastDetails, ToastOptions, ToastPlacement } from "app/Components/Toast/types"
import { action, Action } from "easy-peasy"
export interface ToastModel {
sessionState: {
nextId: number
toasts: Array<Omit<ToastDetails, "positionIndex">>
}
add: Action<this, { message: string; placement: ToastPlacement; options?: ToastOptions }>
remove: Action<this, this["sessionState"]["nextId"]>
removeOldest: Action<this>
}
export const getToastModel = (): ToastModel => ({
sessionState: {
nextId: 0,
toasts: [],
},
add: action((state, newToast) => {
state.sessionState.toasts.push({
id: state.sessionState.nextId,
message: newToast.message,
placement: newToast.placement,
...newToast.options,
})
state.sessionState.nextId += 1
}),
remove: action((state, toastId) => {
state.sessionState.toasts = state.sessionState.toasts.filter((toast) => toast.id !== toastId)
}),
removeOldest: action((state) => {
state.sessionState.toasts.shift()
}),
})