From ac3baf77a14516707effb79cfc9a64cfe8b56104 Mon Sep 17 00:00:00 2001 From: Kakabay Date: Sat, 11 Feb 2023 14:59:12 +0500 Subject: [PATCH] opt --- src/api/Api.ts | 26 ++++++--------- src/components/global/NewsScroll.tsx | 37 ++++++++++------------ src/components/news/News.tsx | 47 ++++++++++------------------ 3 files changed, 43 insertions(+), 67 deletions(-) diff --git a/src/api/Api.ts b/src/api/Api.ts index a53cbe4..92027cb 100644 --- a/src/api/Api.ts +++ b/src/api/Api.ts @@ -1,20 +1,20 @@ // Modules -import axios from "axios"; -import { Dispatch, SetStateAction } from "react"; -import { DispatchProp, useSelector } from "react-redux"; +import axios from 'axios'; +import { Dispatch, SetStateAction } from 'react'; +import { DispatchProp, useSelector } from 'react-redux'; // Types -import { IurlParamAdder } from "../types/api.types"; -import { RootState } from "../types/store.types"; +import { IurlParamAdder } from '../types/api.types'; +import { RootState } from '../types/store.types'; // Helpers -import { urlParamAdder } from "../helpers/urlParamAdder"; +import { urlParamAdder } from '../helpers/urlParamAdder'; export class Api { - private url: string = ""; + private url: string = ''; private params?: IurlParamAdder[]; - public language = useSelector( - (state) => state.language.title + public language = useSelector( + (state) => state.language.title, ); constructor(url: string, params?: IurlParamAdder[]) { @@ -23,13 +23,7 @@ export class Api { } get(state: any, setState: Dispatch>) { - const locale = { name: "locale", value: this.language }; - if ( - this.url.match( - "^(http(s)://.)[-a-zA-Z0-9@:%._+~#=]{2,256}.[a-z]{2,6}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)$" - ) - ) - throw new Error("Bad URL"); + const locale = { name: 'locale', value: this.language }; axios .get(urlParamAdder(locale, this.url, this.params)) .then((res) => { diff --git a/src/components/global/NewsScroll.tsx b/src/components/global/NewsScroll.tsx index 3625b48..6291f7b 100644 --- a/src/components/global/NewsScroll.tsx +++ b/src/components/global/NewsScroll.tsx @@ -1,37 +1,37 @@ // Modules -import { useEffect, useState } from "react"; -import { v4 as uuidv4 } from "uuid"; -import { useSelector, useDispatch } from "react-redux"; +import { useEffect, useState } from 'react'; +import { v4 as uuidv4 } from 'uuid'; +import { useSelector, useDispatch } from 'react-redux'; // Components -import News from "../news/News"; -import SectionTitle from "./SectionTitle"; -import Loader from "./Loader"; +import News from '../news/News'; +import SectionTitle from './SectionTitle'; +import Loader from './Loader'; // Api -import { url } from "../../url"; -import { Api } from "../../api/Api"; -import { newsScrollParams } from "../../api/params"; +import { url } from '../../url'; +import { Api } from '../../api/Api'; +import { newsScrollParams } from '../../api/params'; // Types -import { IPostsData } from "../../types/data.types"; -import { RootState } from "../../types/store.types"; +import { IPostsData } from '../../types/data.types'; +import { RootState } from '../../types/store.types'; // Actions -import { setNewsScroll } from "../../actions/setData"; +import { setNewsScroll } from '../../actions/setData'; interface Props { title: boolean; } const NewsScroll = ({ title }: Props) => { - const api = new Api(url + "/posts", newsScrollParams); + const api = new Api(url + '/posts', newsScrollParams); const language = api.language; const [lastLanguage, setLastLanguage] = useState(language); // redux - const data = useSelector( - (state) => state.newsScroll.data + const data = useSelector( + (state) => state.newsScroll.data, ); const dispatch = useDispatch(); @@ -47,13 +47,10 @@ const NewsScroll = ({ title }: Props) => {
{title === true ? ( - + ) : null}
- {data ? ( + {(data as IPostsData[])[0].id > -1 ? ( (data as IPostsData[]).map((dataEl) => { return ( { @@ -23,12 +22,7 @@ const News = ({ id, title, text, categories, date, img }: Props) => {
- +
@@ -38,23 +32,14 @@ const News = ({ id, title, text, categories, date, img }: Props) => {
{categories.map((category) => { - return ( - - ); + return ; })}
-
+