Skip to content

Commit

Permalink
add chatApi
Browse files Browse the repository at this point in the history
  • Loading branch information
Tatyana-js committed Jan 8, 2025
1 parent 9540f35 commit a459eb2
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 20 deletions.
11 changes: 7 additions & 4 deletions frontend/src/api/channelsApi.js → frontend/src/api/chatApi.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';


export const channelsApi = createApi({
reducerPath: 'channels',
export const chatApi = createApi({
reducerPath: 'chatApi',
baseQuery: fetchBaseQuery({
baseUrl: '/api/v1',
prepareHeaders: (headers) => {
Expand All @@ -17,9 +16,13 @@ export const channelsApi = createApi({
getChannels: builder.query({
query: () => 'channels',
}),
getMessages: builder.query({
query: () => 'messages',
})
}),
});

export const {
useGetChannelsQuery,
} = channelsApi;
useGetMessagesQuery,
} = chatApi;
21 changes: 17 additions & 4 deletions frontend/src/components/MessageField.jsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
import { Container, Row, Col, Nav } from 'react-bootstrap';
import { useSelector, useDispatch } from 'react-redux';
import { useTranslation } from 'react-i18next';
import { activeChannelSelector } from '../store/activeChannelSlice.js';
import MessageForm from './MessageForm.jsx';
import { useGetMessagesQuery } from '../api/chatApi.js';

const MessageField = () => {
const { t } = useTranslation();
const { data, error, isLoading, refetch } = useGetMessagesQuery();
const activeChannel = useSelector(activeChannelSelector);
const dispatch = useDispatch();
const messages = data;
const countMessages = messages?.length || 0;
console.log(messages)

return (
<div className="d-flex flex-column h-100">
<div className="bg-light mb-4 p-3 shadow-sm small">
<p className="m-0">
<b># general</b>
<b># {activeChannel.name}</b>
</p>
<span className="text-muted">0 сообщений</span>
<span className="text-muted">
{t('countMessages.messages', { count: countMessages })}
</span>
</div>
<div id="messages-box" className="chat-messages overflow-auto px-5 ">{}</div>
<div id="messages-box" className="chat-messages overflow-auto px-5 ">{messages}</div>
<MessageForm />
</div>
);
Expand Down
16 changes: 10 additions & 6 deletions frontend/src/components/Сhannels.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Nav, Button } from 'react-bootstrap';
import { useGetChannelsQuery } from '../api/channelsApi.js';
import { useGetChannelsQuery } from '../api/chatApi.js';
import { useRef, useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { selectActiveTab, activeChannelSelector } from '../store/activeChannelSlice.js';
Expand All @@ -11,10 +11,14 @@ const Channels = () => {
const channelsRef = useRef(null);
const activeChannel = useSelector(activeChannelSelector);
useEffect(() => {
try {
if (isLoading) {

}
} catch (err) {
console.log(err);
}
}, []);
console.log(channels);
console.log(activeChannel);

return (
<Nav
Expand All @@ -26,11 +30,11 @@ return (
{channels?.map((channel) => {
return (
<Nav.Item as="li" key={channel.id} className="w-100">
<Button
<Button
type="button"
className="w-100 rounded-0 text-start"
variant={channel.id === activeChannel ? "secondary" : ""}
onClick={() => dispatch(selectActiveTab({ id: channel.id }))}
variant={channel.id === activeChannel.id ? "secondary" : ""}
onClick={() => dispatch(selectActiveTab(channel))}
>
<span className="me-1"># </span>
{channel.name}
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/context/AuthProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import useAuth from '../hooks/index.jsx';
// eslint-disable-next-line react/prop-types
const AuthProvider = ({ children }) => {
const [loggedIn, setLoggedIn] = useState(!!localStorage.getItem('token'));

const token = localStorage.getItem('token');

const logIn = (token, username) => {
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/locales/ru.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export default {
},
channels: {
title: 'Каналы',
},
countMessages: {
messages_one: '{{count}} сообщение',
messages_fwe: '{{count}} сообщения',
messages_many: '{{count}} сообщений',
}
// singUpForm: {
// username: ''
Expand Down
11 changes: 9 additions & 2 deletions frontend/src/store/activeChannelSlice.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
import { createSlice } from '@reduxjs/toolkit';

const defaultChannel = {
id: '1',
name: 'general',
removable: false,
};

const initialState = {
currentChannel: 1,
currentChannel: defaultChannel,
};

const activeChannelSlice = createSlice({
name: 'activeChannel',
initialState,
reducers: {
selectActiveTab: (state, { payload }) => {
state.activeChannel = payload;
state.currentChannel = payload;
// console.log(payload);
},
},
});
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { combineReducers, configureStore } from '@reduxjs/toolkit';
import { channelsApi } from '../api/channelsApi.js';
import { chatApi } from '../api/chatApi.js';
import activeChannelReducer from './activeChannelSlice.js';

const rootReducer = combineReducers({
[channelsApi.reducerPath]: channelsApi.reducer,
[chatApi.reducerPath]: chatApi.reducer,
activeChannel: activeChannelReducer,
// messages: mesagesReducer,
});

const store = configureStore ({
reducer: rootReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(channelsApi.middleware),
getDefaultMiddleware().concat(chatApi.middleware),
});

export default store;

0 comments on commit a459eb2

Please sign in to comment.