| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- import { createSlice, createAsyncThunk } from "@reduxjs/toolkit";
- import roomService from "../services/roomService";
- import notificationService from "../services/notificationService";
-
- const initialState = {
- status: "idle",
- rooms: [],
- activeRoom: null,
- error: null,
- connection: null,
- messages: [],
- connections: [],
- notifications: [],
- typings: [],
- };
-
- export const fetchChatRoomsAsync = createAsyncThunk(
- "chat/fetchChatRoomsAsync",
- async (payload, thunkAPI) => {
- try {
- return await roomService.getRooms(payload);
- } catch (error) {
- return thunkAPI.rejectWithValue({ error });
- }
- }
- );
-
- export const fetchSupportRoomsAsync = createAsyncThunk(
- "chat/fetchSupportRooms",
- async (payload, thunkAPI) => {
- try {
- return await roomService.getSupportRooms(payload);
- } catch (error) {
- return thunkAPI.rejectWithValue({ error });
- }
- }
- );
-
- export const createChatRoomAsync = createAsyncThunk(
- "chat/createChatRoomAsync",
- async (payload, thunkAPI) => {
- try {
- return await roomService.createRoom(payload);
- } catch (error) {
- return thunkAPI.rejectWithValue({ error });
- }
- }
- );
-
- export const loadNotifications = createAsyncThunk(
- "chat/loadNotifications",
- async (payload, thunkAPI) => {
- try {
- return await notificationService.loadNotifications(payload);
- } catch (error) {
- return thunkAPI.rejectWithValue({ error });
- }
- }
- );
-
- export const readNotificationsAsync = createAsyncThunk(
- "chat/deleteNotificationsAsync",
- async (payload, thunkAPI) => {
- try {
- return await notificationService.readNotifications(payload);
- } catch (error) {
- return thunkAPI.rejectWithValue({ error });
- }
- }
- );
-
- const chatSlice = createSlice({
- name: "chat",
- initialState,
- reducers: {
- setAllChats: (state, action) => {
- state.rooms = action.payload;
- },
- setRoom: (state, action) => {
- state.activeRoom = action.payload;
- },
- deleteActiveRoom: (state, action) => {
- state.activeRoom = null;
- },
- // Set hub connection
- setConnection: (state, action) => {
- state.connection = action.payload;
- },
- // New message sent from user
- newMessage: (state, action) => {
- if (action.payload.changedRoom) {
- state.messages = [];
- } else {
- state.messages = [...state.messages, action.payload];
- }
- },
- saveContextId: (state, action) => {
- // Check is empty array
- // If array is not empty, check is connection for specific room and specific user already added
- if (state.connections.length > 0) {
- if (
- !state.connections.some(
- (e) => e.connId === action.payload && e.roomId === state.activeRoom
- )
- ) {
- state.connections.push({
- connId: action.payload.connId,
- roomId: state.activeRoom.id,
- userId: action.payload.userId,
- });
- }
- } else {
- // If array is empty, add connection
- state.connections.push({
- connId: action.payload.connId,
- roomId: state.activeRoom.id,
- userId: action.payload.userId,
- });
- }
- },
- // Set messages fetched from backend for specific room
- setMessages: (state, action) => {
- state.messages = action.payload;
- },
- addNotification: (state, action) => {
- console.log(1, action.payload);
- if (state.notifications.length !== 0) {
- console.log(2);
- const room = state.notifications.find(
- (notification) => notification.roomId === action.payload
- );
-
- if (room) {
- console.log(3);
- room.notificationCount++;
- } else {
- state.notifications.push({
- roomId: action.payload,
- notificationCount: 1,
- });
- }
- } else {
- console.log(4);
- state.notifications.push({
- roomId: action.payload,
- notificationCount: 1,
- });
- }
- },
- readNotifications: (state, action) => {
- state.notifications = state.notifications.filter(
- (notification) => notification.roomId !== action.payload
- );
- },
- leaveRoom: (state, action) => {
- state.activeRoom = null;
-
- const room = state.rooms.find(
- (room) => room.id === action.payload.roomId
- );
-
- room.customers = room.customers.filter(
- (customer) => customer.customerId !== action.payload.customerId
- );
- },
- addTyping: (state, action) => {
- if (
- !state.typings.some(
- (n) =>
- n.message === action.payload.message &&
- n.roomId === action.payload.roomId
- )
- )
- state.typings = [...state.typings, action.payload];
- else console.log("ima vec");
- },
- removeTyping: (state, action) => {
- if (state.typings.length >= 1) {
- let f = state.typings[0];
- state.typings = state.typings.filter((n) => n.message !== f.message);
- }
- },
- },
- extraReducers: (builder) => {
- // Fetch chat rooms
- builder.addCase(fetchChatRoomsAsync.pending, (state) => {
- state.status = "pendingFetchRooms";
- state.error = null;
- });
- builder.addCase(fetchChatRoomsAsync.fulfilled, (state, action) => {
- state.rooms = action.payload;
- state.status = "idle";
- state.error = null;
- });
- builder.addCase(fetchChatRoomsAsync.rejected, (state, action) => {
- state.status = "idle";
- state.error = "Fetch error" + action.payload;
- });
-
- // fetch rooms that support created
- builder.addCase(fetchSupportRoomsAsync.pending, (state) => {
- state.status = "pendingFetchRooms";
- state.error = null;
- });
- builder.addCase(fetchSupportRoomsAsync.fulfilled, (state, action) => {
- state.rooms = action.payload;
- state.status = "idle";
- state.error = null;
- });
- builder.addCase(fetchSupportRoomsAsync.rejected, (state, action) => {
- state.status = "idle";
- state.error = "Fetch error" + action.payload;
- });
-
- // Add chat room
- builder.addCase(createChatRoomAsync.pending, (state) => {
- state.status = "pendingAddRoom";
- state.error = null;
- });
- builder.addCase(createChatRoomAsync.fulfilled, (state, action) => {
- state.status = "idle";
- state.rooms = [...state.rooms, action.payload];
- state.error = null;
- });
- builder.addCase(createChatRoomAsync.rejected, (state, action) => {
- state.status = "idle";
- state.error = action.payload;
- });
-
- builder.addCase(loadNotifications.pending, (state) => {
- state.status = "pendingLoadingNotification";
- state.error = null;
- });
- builder.addCase(loadNotifications.fulfilled, (state, action) => {
- state.status = "idle";
- if (action.payload.length === 0) {
- state.notifications = [];
- } else {
- state.notifications = action.payload.map((n) => {
- return { roomId: n.roomId, notificationCount: n.count };
- });
- }
- state.error = null;
- });
- builder.addCase(loadNotifications.rejected, (state, action) => {
- state.status = "idle";
- state.error = action.payload;
- });
-
- // Read notifications
- builder.addCase(readNotificationsAsync.pending, (state) => {
- state.status = "pendingReadingNotifications";
- state.error = null;
- });
- builder.addCase(readNotificationsAsync.fulfilled, (state) => {
- state.status = "idle";
- state.error = null;
- });
- builder.addCase(readNotificationsAsync.rejected, (state, action) => {
- state.status = "idle";
- state.error = action.payload;
- });
- },
- });
-
- export const chatActions = chatSlice.actions;
- export const chatReducers = chatSlice.reducer;
|