Skip to content

Commit

Permalink
add .
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatyana-js committed Jan 17, 2025
1 parent 601260e commit 441bf95
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
15 changes: 0 additions & 15 deletions frontend/src/api/chatApi.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import { io } from 'socket.io-client';

const socket = io();

export const chatApi = createApi({
reducerPath: 'chatApi',
Expand Down Expand Up @@ -46,18 +43,6 @@ export const chatApi = createApi({
}),
getMessages: builder.query({ // Сообщения
query: () => '/messages',
onCacheEntryAdded: async ( event, { updateCachedData, cacheDataLoaded, cacheEntryRemoved }) => {
await cacheDataLoaded;
console.log(cacheDataLoaded);
const listener = () => {
if (event === 'addMessage') {
updateCachedData((draft) => draft.push(data));
}
};
socket.on('addMessage', listener);
await cacheEntryRemoved;
socket.close();
},
providesTags: ['Messages'],
}),
addMessage: builder.mutation ({
Expand Down
9 changes: 9 additions & 0 deletions frontend/src/init.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import i18next from 'i18next';
import { io } from 'socket.io-client';
import { initReactI18next } from 'react-i18next';
import { Provider } from 'react-redux';
import store from './store/index.js';
import App from './App.jsx';
import resources from './locales/index.js';
import { useGetMessagesQuery } from './api/chatApi.js';

const init = async () => {
const i18n = i18next.createInstance();
Expand All @@ -18,6 +20,13 @@ const init = async () => {
}
});

const socket = io();
const { data: messages = []} = useGetMessagesQuery();
socket.on('newMessage', (payload) => {
console.log(payload)
messages.push(payload);
});

return (
<Provider store={store}>
<App />
Expand Down

0 comments on commit 441bf95

Please sign in to comment.