-
Notifications
You must be signed in to change notification settings - Fork 0
/
timelineSlice.ts
93 lines (87 loc) · 1.77 KB
/
timelineSlice.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
export type TimelineItem = {
id: number;
days: string;
display: string;
active: boolean;
};
type TimelineState = {
currentTimeline: TimelineItem;
timeline: TimelineItem[];
};
const initialState: TimelineState = {
currentTimeline: {
id: 1,
days: "1",
display: "1D",
active: true,
},
timeline: [
{
id: 1,
days: "1",
display: "1D",
active: true,
},
{ id: 2, days: "7", display: "7D", active: false },
{
id: 3,
days: "14",
display: "14D",
active: false,
},
{
id: 4,
days: "30",
display: "1M",
active: false,
},
{
id: 5,
days: "90",
display: "3M",
active: false,
},
{
id: 6,
days: "180",
display: "6M",
active: false,
},
{
id: 7,
days: "365",
display: "1Y",
active: false,
},
{
id: 8,
days: "max",
display: "MAX",
active: false,
},
],
};
export const timelineSlice = createSlice({
name: "timeline",
initialState,
reducers: {
activeTimeline: (state, action: PayloadAction<number>) => {
const currentActiveIndex = state.timeline.findIndex(
(item) => item.active
);
if (currentActiveIndex !== -1) {
state.timeline[currentActiveIndex].active = false;
}
const newActiveIndex = state.timeline.findIndex(
(item) => item.id === action.payload
);
if (newActiveIndex !== -1) {
state.timeline[newActiveIndex].active = true;
state.currentTimeline = { ...state.timeline[newActiveIndex] };
}
},
},
});
export const { activeTimeline } = timelineSlice.actions;
export default timelineSlice.reducer;