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 ( -