Moved stickers state to redux. Removed unneded type definitions. Refactored components to accomdate changes in types and interfaces.
This commit is contained in:
63
src/features/calender/stickers.ts
Normal file
63
src/features/calender/stickers.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
||||
import { format, getDate, isSameDay } from "date-fns";
|
||||
import stickersSeeder from "../../../data/stickerSeeder";
|
||||
|
||||
interface StickersSlice {
|
||||
stickersMonth: StickerDays;
|
||||
}
|
||||
|
||||
interface UpdateStickerSlicePayload {
|
||||
date: string;
|
||||
sticker: StickerVal;
|
||||
}
|
||||
|
||||
const initialState: StickersSlice = {
|
||||
stickersMonth: stickersSeeder()
|
||||
};
|
||||
|
||||
const stickersSlice = createSlice({
|
||||
name: "Stickers",
|
||||
initialState,
|
||||
reducers: {
|
||||
addEditSticker(
|
||||
state: StickersSlice,
|
||||
actions: PayloadAction<UpdateStickerSlicePayload>
|
||||
) {
|
||||
const { date, sticker } = actions.payload;
|
||||
|
||||
const dateObj = new Date(date);
|
||||
|
||||
// Getting index for the stickers array, sticker from the stickers array, and the date from the sticker.
|
||||
const index: number = getDate(dateObj) - 1;
|
||||
const currSticker: Sticker = state.stickersMonth[index];
|
||||
const { date: stickerDate } = currSticker;
|
||||
|
||||
// Updating the edited status by checking if the sticker date is today's date.
|
||||
const edited = currSticker.edited
|
||||
? true
|
||||
: isSameDay(new Date(stickerDate), new Date())
|
||||
? false
|
||||
: true;
|
||||
currSticker.edited = edited;
|
||||
|
||||
// TODO: Add manually added here.
|
||||
|
||||
// Updating the id of the sticker.
|
||||
const id = format(dateObj, "yyyyddLL") + sticker;
|
||||
|
||||
// Updating the information of the sticker.
|
||||
const newSticker: Sticker = {
|
||||
id: id,
|
||||
date: date,
|
||||
sticker: sticker,
|
||||
edited: edited,
|
||||
manual: false
|
||||
};
|
||||
|
||||
state.stickersMonth[index] = newSticker;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
export const { addEditSticker } = stickersSlice.actions;
|
||||
export default stickersSlice.reducer;
|
||||
Reference in New Issue
Block a user