40 lines
859 B
TypeScript
40 lines
859 B
TypeScript
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
|
|
|
|
interface Event {
|
|
id: string;
|
|
name: string;
|
|
description: string;
|
|
date: string;
|
|
}
|
|
|
|
interface EventsState {
|
|
events: Event[];
|
|
favorites: string[];
|
|
}
|
|
|
|
const initialState: EventsState = {
|
|
events: [],
|
|
favorites: [],
|
|
};
|
|
|
|
const eventsSlice = createSlice({
|
|
name: 'events',
|
|
initialState,
|
|
reducers: {
|
|
setEvents: (state, action: PayloadAction<Event[]>) => {
|
|
state.events = action.payload;
|
|
},
|
|
toggleFavorite: (state, action: PayloadAction<string>) => {
|
|
const index = state.favorites.indexOf(action.payload);
|
|
if (index > -1) {
|
|
state.favorites.splice(index, 1);
|
|
} else {
|
|
state.favorites.push(action.payload);
|
|
}
|
|
},
|
|
},
|
|
});
|
|
|
|
export const { setEvents, toggleFavorite } = eventsSlice.actions;
|
|
export default eventsSlice.reducer;
|