Initial commit
This commit is contained in:
39
store/eventsSlice.ts
Normal file
39
store/eventsSlice.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
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;
|
||||
11
store/store.ts
Normal file
11
store/store.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { configureStore } from '@reduxjs/toolkit';
|
||||
import eventsReducer from './eventsSlice';
|
||||
|
||||
export const store = configureStore({
|
||||
reducer: {
|
||||
events: eventsReducer,
|
||||
},
|
||||
});
|
||||
|
||||
export type RootState = ReturnType<typeof store.getState>;
|
||||
export type AppDispatch = typeof store.dispatch;
|
||||
Reference in New Issue
Block a user