0

i use useReducer + useContext to change global state, please tell me how to remove comments using useReducer

interfaces

interface image

data array

export const listsData:IData[] = [
  {
    id: 'list-1',
    listTitle: 'TODO',
    cards: [
      {
        id: 'card-1-1',
        cardTitle: 'card title 1',
        cardDescription: 'description 1',
        cardComment: [
          {
            id:'card-1-comment-1',
            commentText:'commentTitle'
          }
        ],
        modalOpen: false
      }
    ]
  }
]

actions & useReducer

type DataAction =
  | { type: 'REMOVE_COMMENT', payload: { comment: IComment, list: IData, card: ICards, } }
case 'REMOVE_COMMENT':
      return {
        ...state, datass: state.datass.map(el=>{
          if (el.id === action.payload.list.id) {
            el.cards.map(el=>{
              if (el.id === action.payload.card.id) {
                el.cardComment.filter(el => {
                  return el.id !== action.payload.comment.id
                })
              }
            })
          }
          return el
        })
      }
Rafael
  • 1

1 Answers1

0

maybe it will be useful for someone

case 'REMOVE_COMMENT':
      return {
        ...state,
        datass: state.datass.map((el:IData) => ({
          ...el,
          cards: el.cards.map((el:ICards) => ({
            ...el,
            cardComment: el.cardComment.filter((el:IComment) => {
                return el.id !== action.payload.comment.id
              }
            )
          }))
        }))
      }
Rafael
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 23 '22 at 11:51