All files / context/chat ChatState.js

61.53% Statements 8/13
100% Branches 0/0
28.57% Functions 2/7
61.53% Lines 8/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56                    1x 1x         1x   1x 1x             1x       1x                 1x                                
import React, { useReducer } from "react";
import { getUnrepliedCount } from "../../api/api";
import ChatContext from "./ChatContext";
import ChatReducer from "./ChatReducer";
import {
  MODIFY_UNREPLIEDCOUNT,
  SET_NEWMESSAGES,
  SET_UNREPLIEDCOUNT,
} from "./types/Types";
 
const ChatState = (props) => {
  const initialState = {
    unrepliedCount: {},
    newMessages: [],
  };
 
  const [state, dispatch] = useReducer(ChatReducer, initialState);
 
  const fetchUnrepliedCount = () => {
    getUnrepliedCount(props.backendApiUrl)
      .then((res) => {
        dispatch({ type: SET_UNREPLIEDCOUNT, payload: res?.data });
      })
      .catch((e) => console.log(e));
  };
 
  const storeNewMessages = (data) => {
    dispatch({ type: SET_NEWMESSAGES, payload: data });
  };
 
  const modifyCount = (data) => {
    data.forEach((element) => {
      dispatch({
        type: MODIFY_UNREPLIEDCOUNT,
        payload: { type: element?.type, threadId: element?.thread?.id },
      });
    });
  };
 
  return (
    <ChatContext.Provider
      value={{
        unrepliedCount: state.unrepliedCount,
        newMessages: state.newMessages,
        fetchUnrepliedCount,
        modifyCount,
        storeNewMessages,
      }}
    >
      {props.children}
    </ChatContext.Provider>
  );
};
 
export default ChatState;