From fc24bd4c9c19530c66e228e10e71f37c156a934d Mon Sep 17 00:00:00 2001 From: VividTruthKeeper Date: Thu, 23 Feb 2023 12:03:45 +0500 Subject: [PATCH] All posts --- src/components/global/CustomNewsScroll.tsx | 27 ++++++---- src/components/global/NewsScroll.tsx | 2 +- src/pages/AllPosts.tsx | 60 ++++++++++++++++++---- src/styles/_all-posts.scss | 6 +++ src/styles/style.scss | 37 ++++++------- 5 files changed, 93 insertions(+), 39 deletions(-) create mode 100644 src/styles/_all-posts.scss diff --git a/src/components/global/CustomNewsScroll.tsx b/src/components/global/CustomNewsScroll.tsx index 07c1a62..402c296 100644 --- a/src/components/global/CustomNewsScroll.tsx +++ b/src/components/global/CustomNewsScroll.tsx @@ -1,14 +1,14 @@ // Modules -import { useMemo, useState } from 'react'; -import { v4 as uuidv4 } from 'uuid'; +import { useMemo, useState } from "react"; +import { v4 as uuidv4 } from "uuid"; // Components -import News from '../news/News'; -import Loader from './Loader'; -import Pagination from './Pagination'; +import News from "../news/News"; +import Loader from "./Loader"; +import Pagination from "./Pagination"; // Types -import { IPostsData } from '../../types/data.types'; +import { IPostsData } from "../../types/data.types"; interface IProps { data: IPostsData[]; @@ -16,9 +16,12 @@ interface IProps { pagination: boolean; } -const CustomNewsScroll = ({ data, word, pagination = true }: IProps) => { +const CustomNewsScroll = ({ data, word, pagination = false }: IProps) => { const [activePage, setActivePage] = useState(1); - const pageMemo = useMemo(() => ({ activePage, setActivePage }), [activePage, setActivePage]); + const pageMemo = useMemo( + () => ({ activePage, setActivePage }), + [activePage, setActivePage] + ); return (
@@ -34,7 +37,11 @@ const CustomNewsScroll = ({ data, word, pagination = true }: IProps) => { text={dataEl.excerpt} date={dataEl.published_at} categories={dataEl.categories} - img={dataEl.featured_images[0] ? dataEl.featured_images[0].path : ''} + img={ + dataEl.featured_images[0] + ? dataEl.featured_images[0].path + : "" + } /> ); }) @@ -42,7 +49,7 @@ const CustomNewsScroll = ({ data, word, pagination = true }: IProps) => { ) ) : ( -

Нет новостей для "{word || ''}"

+

Нет новостей для "{word || ""}"

)}
{pagination ? ( diff --git a/src/components/global/NewsScroll.tsx b/src/components/global/NewsScroll.tsx index b8c96d0..8d5179e 100644 --- a/src/components/global/NewsScroll.tsx +++ b/src/components/global/NewsScroll.tsx @@ -74,7 +74,7 @@ const NewsScroll = ({ title, category }: Props) => { {title === true ? ( ) : null}
diff --git a/src/pages/AllPosts.tsx b/src/pages/AllPosts.tsx index cade926..e90fe12 100644 --- a/src/pages/AllPosts.tsx +++ b/src/pages/AllPosts.tsx @@ -8,32 +8,72 @@ import { url } from "../url"; // Components import CustomNewsScroll from "../components/global/CustomNewsScroll"; +import Loader from "../components/global/Loader"; + +// Types import { IurlParamAdder } from "../types/api.types"; +import { IPostsData } from "../types/data.types"; const AllPosts = () => { const { category } = useParams(); - const api = new Api(url + category); + const categoryParam = + category !== ("null" || undefined) ? (category as string) : ""; + + const [params, setParams] = useState( + category !== ("null" || undefined) + ? [ + { + name: "count", + value: 10, + }, + { + name: "page", + value: 1, + }, + { + name: "category", + value: categoryParam, + }, + ] + : [ + { + name: "count", + value: 10, + }, + { + name: "page", + value: 1, + }, + ] + ); + const api = new Api(url + "/posts", params); + const language = api.language; - const [params, setParams] = useState([ - { - name: "count", - value: 10, - }, - ]); - const [lastLanguage, setLastLanguage] = useState(language); + const [data, setData] = useState(); + + useEffect(() => { + api.get(data, setData); + }, [params]); useEffect(() => { if (language !== lastLanguage) { - // api.get() + api.get(data, setData); + setLastLanguage(language); } }, [language, lastLanguage]); return (
-
+
+ {data ? ( + + ) : ( + + )} +
); diff --git a/src/styles/_all-posts.scss b/src/styles/_all-posts.scss new file mode 100644 index 0000000..7b2d3d5 --- /dev/null +++ b/src/styles/_all-posts.scss @@ -0,0 +1,6 @@ +.all-inner { + padding: 5.6rem 0; + .loader { + margin: 4rem 0; + } +} diff --git a/src/styles/style.scss b/src/styles/style.scss index 06cffaa..292e23d 100644 --- a/src/styles/style.scss +++ b/src/styles/style.scss @@ -1,18 +1,19 @@ -@import './variables'; -@import './mixins'; -@import './general'; -@import './nav'; -@import './main'; -@import './videos'; -@import './section-title'; -@import './footer'; -@import './news'; -@import './asideNews'; -@import './aside'; -@import './news-section'; -@import './news-article'; -@import './category'; -@import './calendar'; -@import './loader'; -@import './search-result'; -@import './pagination'; +@import "./variables"; +@import "./mixins"; +@import "./general"; +@import "./nav"; +@import "./main"; +@import "./videos"; +@import "./section-title"; +@import "./footer"; +@import "./news"; +@import "./asideNews"; +@import "./aside"; +@import "./news-section"; +@import "./news-article"; +@import "./category"; +@import "./calendar"; +@import "./loader"; +@import "./search-result"; +@import "./pagination"; +@import "./all-posts";