From 9540f355f54a69e45c46c2bcbf982792dc2bcf11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A2=D0=B0=D1=82=D1=8C=D1=8F=D0=BD=D0=B0=20=D0=90=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=B5=D0=B5=D0=B2=D0=B0?= Date: Sun, 5 Jan 2025 20:00:43 +0300 Subject: [PATCH] add channelsApi --- frontend/src/api/channelsApi.js | 25 +++++++ "frontend/src/components/\320\241hannels.jsx" | 68 ++++++++++--------- frontend/src/context/AuthProvider.jsx | 5 +- frontend/src/pages/LoginPage.jsx | 3 - frontend/src/store/activeChannelSlice.js | 19 ++++++ frontend/src/store/channelsSlice.js | 17 ----- frontend/src/store/index.js | 23 +++++-- frontend/vite.config.js | 2 +- 8 files changed, 100 insertions(+), 62 deletions(-) create mode 100644 frontend/src/api/channelsApi.js create mode 100644 frontend/src/store/activeChannelSlice.js delete mode 100644 frontend/src/store/channelsSlice.js diff --git a/frontend/src/api/channelsApi.js b/frontend/src/api/channelsApi.js new file mode 100644 index 0000000..9948af3 --- /dev/null +++ b/frontend/src/api/channelsApi.js @@ -0,0 +1,25 @@ +import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'; + + +export const channelsApi = createApi({ + reducerPath: 'channels', + baseQuery: fetchBaseQuery({ + baseUrl: '/api/v1', + prepareHeaders: (headers) => { + const token = localStorage.getItem('token'); + if (token) { + headers.set('Authorization', `Bearer ${token}`); + } + return headers; + } + }), + endpoints: (builder) => ({ + getChannels: builder.query({ + query: () => 'channels', + }), + }), +}); + +export const { + useGetChannelsQuery, +} = channelsApi; \ No newline at end of file diff --git "a/frontend/src/components/\320\241hannels.jsx" "b/frontend/src/components/\320\241hannels.jsx" index a187c1e..f5f9e66 100644 --- "a/frontend/src/components/\320\241hannels.jsx" +++ "b/frontend/src/components/\320\241hannels.jsx" @@ -1,42 +1,44 @@ -import { Nav } from 'react-bootstrap'; -import axios from 'axios'; -import { useEffect, useState } from 'react'; -import router from '../utils/routes.js'; - -const getAuthHeader = () => { - const token = localStorage.getItem('token'); - if (token) { - return { Authorization: `Bearer ${token}`}; - } - return {}; -}; +import { Nav, Button } from 'react-bootstrap'; +import { useGetChannelsQuery } from '../api/channelsApi.js'; +import { useRef, useEffect } from 'react'; +import { useSelector, useDispatch } from 'react-redux'; +import { selectActiveTab, activeChannelSelector } from '../store/activeChannelSlice.js'; const Channels = () => { - const [channels, setChannels] = useState([]); + const { data, error, isLoading, refetch } = useGetChannelsQuery(); + const dispatch = useDispatch(); + const channels = data; + const channelsRef = useRef(null); + const activeChannel = useSelector(activeChannelSelector); useEffect(() => { - const fetchContent = async () => { - const { data } = await axios.get(router.channelsPath(), { headers: getAuthHeader() }); - console.log(data); - setChannels(data); - }; - fetchContent(); -}, []); + }, []); + console.log(channels); +console.log(activeChannel); return ( -