File size: 1,166 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { createSlice } from '@reduxjs/toolkit';
import { getItemFromLocalStorage } from '../../helper/localStorage';

const initialState = {
   currentComments: [], // when giving like to multiple comment items quickly , it takes a time to transform comment item with already liked or not , so I stored transformed current comments in redux store and put them to server
   commentItem: getItemFromLocalStorage('commentItemToManage') || null, // to manage => edit || delete
   transformedComments: getItemFromLocalStorage('transformedComments') || [], // for deleting comment
};

const currentCommentsSlice = createSlice({
   name: 'currentComment',
   initialState,
   reducers: {
      setCurrentComments: (state, action) => {
         state.currentComments = action.payload;
      },

      setcommentItem: (state, action) => {
         state.commentItem = action.payload;
      },

      setTransformedComments: (state, action) => {
         state.transformedComments = action.payload;
      },
   },
});

export const { setCurrentComments, setcommentItem, setTransformedComments } =
   currentCommentsSlice.actions;

export default currentCommentsSlice.reducer;