From 3057ec532ef5758502732e4bfa7f69d246076fc0 Mon Sep 17 00:00:00 2001 From: VividTruthKeeper Date: Fri, 3 Mar 2023 14:53:31 +0500 Subject: [PATCH] category reducer --- src/actions/setData.ts | 38 ++++-- src/components/header/SubNav.tsx | 25 ++-- src/reducers/dataReducer.ts | 193 +++++++++++++++++++------------ src/store/functionality.ts | 12 +- src/types/store.types.ts | 45 ++++--- 5 files changed, 197 insertions(+), 116 deletions(-) diff --git a/src/actions/setData.ts b/src/actions/setData.ts index 85de568..182ad1e 100644 --- a/src/actions/setData.ts +++ b/src/actions/setData.ts @@ -1,5 +1,8 @@ // Types +import { ICategoriesData } from "../types/data.types"; import { + ICategoryData, + ICategoryDataAction, IFeaturedData, IFeaturedDataAction, INewsScroll, @@ -10,33 +13,46 @@ import { ISearchResultAction, IVideoData, IVideoDataAction, -} from '../types/store.types'; +} from "../types/store.types"; // NewsScroll -export const setNewsScroll = (data: INewsScroll['data']): INewsScrollAction => ({ - type: 'SET_NEWS_SCROLL', +export const setNewsScroll = ( + data: INewsScroll["data"] +): INewsScrollAction => ({ + type: "SET_NEWS_SCROLL", payload: data, }); -export const setSearchResult = (data: ISearchResult['data']): ISearchResultAction => ({ - type: 'SET_SEARCH_DATA', +export const setSearchResult = ( + data: ISearchResult["data"] +): ISearchResultAction => ({ + type: "SET_SEARCH_DATA", payload: data, }); // Post -export const setPost = (data: IPostData['data']): IPostDataAction => ({ - type: 'SET_POST', +export const setPost = (data: IPostData["data"]): IPostDataAction => ({ + type: "SET_POST", payload: data, }); -export const setVideo = (data: IVideoData['data']): IVideoDataAction => ({ - type: 'SET_VIDEO', +export const setVideo = (data: IVideoData["data"]): IVideoDataAction => ({ + type: "SET_VIDEO", payload: data, }); -export const setFeatured = (data: IFeaturedData['data']): IFeaturedDataAction => ({ - type: 'SET_FEATURED', +export const setFeatured = ( + data: IFeaturedData["data"] +): IFeaturedDataAction => ({ + type: "SET_FEATURED", + payload: data, +}); + +export const setCategories = ( + data: ICategoryData["data"] +): ICategoryDataAction => ({ + type: "SET_CATEGORIES", payload: data, }); diff --git a/src/components/header/SubNav.tsx b/src/components/header/SubNav.tsx index e6dd79a..26cc497 100644 --- a/src/components/header/SubNav.tsx +++ b/src/components/header/SubNav.tsx @@ -5,7 +5,7 @@ import { useDispatch, useSelector } from "react-redux"; import { useEffect, useState } from "react"; // Types -import { RootState } from "../../types/store.types"; +import { ICategoryData, RootState } from "../../types/store.types"; import { ICategoriesData } from "../../types/data.types"; // Actions @@ -20,6 +20,7 @@ import { categoriesParams } from "../../api/params"; import Loader from "../global/Loader"; import SubNavLi from "./SubNavLi"; import SubNavLiMain from "./SubNavLiMain"; +import { setCategories } from "../../actions/setData"; const SubNav = () => { const activeLink = useSelector( @@ -29,19 +30,23 @@ const SubNav = () => { (state) => state.language.title ); + const categories = useSelector( + (state) => state.categories.data + ); + const dispatch = useDispatch(); const onClickLink = (active: number) => { dispatch(setActiveLink(active)); }; - const [data, setData] = useState(); - // Api const api = new Api(url + "/categories", categoriesParams); useEffect(() => { - api.get(data, setData); + api.get(categories, (data: ICategoryData["data"]) => + dispatch(setCategories(data)) + ); }, [language]); const location = useLocation(); @@ -58,15 +63,19 @@ const SubNav = () => { return (