import { createSlice , PayloadAction } from "@reduxjs/toolkit" ; import { QnaPost } from "../lib/api/qna" ; export interface QnaState { post : QnaPost | null ; replies : QnaPost [ ] | null ; } const initialState = { post : null , replies : null , } ; const qna = createSlice ( { name : "qna" , initialState , reducers : { setPost ( state , action ) { state . post = action . payload ; } , setReplies ( state , action ) { state . replies = action . payload ; } , acceptReply ( state , action ) { const reply = state . replies ?. find ( ( reply ) => reply . id === action . payload ) ; if ( reply ) { reply . accepted = true ; } } , deleteReply ( state , action ) { if ( ! state . replies ) { return ; } const idx = state . replies ?. findIndex ( ( reply ) => reply . id === action . payload ) ; if ( idx !== - 1 ) { state . replies ?. splice ( idx , 1 ) ; } } , writeReply ( state , action ) { state . replies ?. push ( action . payload ) ; } , voteReply ( state , action ) { const reply = state . replies ?. find ( ( reply ) => reply . id === action . payload . id ) ; if ( reply ) { reply . voted = action . payload . voteType === "upvote" ? 1 : - 1 ; reply . recommend = 1 ; } } , } , } ) ; export default qna ;