From eb76f7457d316b9d797e0dc3948eba35114a3927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=B8=D0=BE=D1=81=D0=B8=D1=84=20=D0=B1=D1=80=D1=8B=D0=BA?= =?UTF-8?q?=D0=BE=D0=B2?= Date: Tue, 19 Mar 2024 14:15:18 +0500 Subject: [PATCH] finaly version --- src/constants/host.ts => config.ts | 0 src/app/admin/auth/page.tsx | 65 +------- src/app/admin/page.tsx | 9 +- src/components/layout/AdminSideBar.tsx | 34 +++- src/components/screens/admin/auth/Auth.tsx | 146 ++++++++++++++++++ .../screens/admin/service/Services.tsx | 30 ++-- .../admin/service/blocks/AddServiceForm.tsx | 26 ++-- .../admin/service/blocks/ServiceItem.tsx | 3 +- .../screens/home/blocks/services/Services.tsx | 28 ++-- src/components/screens/services/Services.tsx | 6 +- src/services/order/ordersServices.ts | 2 +- src/services/product/productServeces.ts | 2 +- src/services/user/userServices.ts | 28 ++++ 13 files changed, 269 insertions(+), 110 deletions(-) rename src/constants/host.ts => config.ts (100%) create mode 100644 src/services/user/userServices.ts diff --git a/src/constants/host.ts b/config.ts similarity index 100% rename from src/constants/host.ts rename to config.ts diff --git a/src/app/admin/auth/page.tsx b/src/app/admin/auth/page.tsx index b7d225e..8ddd6d3 100644 --- a/src/app/admin/auth/page.tsx +++ b/src/app/admin/auth/page.tsx @@ -1,67 +1,10 @@ -'use client' - -import MainButton from '@/components/UI/button/MainButton' -import FormInput from '@/components/UI/input/FormInput' -import { ChangeEvent, useState } from 'react' -import styled from 'styled-components' +import Auth from '@/components/screens/admin/auth/Auth' +import React from 'react' type Props = {} -const Container = styled.div` - width: 100%; - height: 100vh; - background-image: url('/image 12.png'); - background-position: center; - background-size: cover; - - display: flex; - align-items: center; - justify-content: center; - position: absolute; - top: 0; - left: 0; - - .form { - width: 440px; - padding: 20px; - text-align: center; - display: flex; - justify-content: center; - flex-direction: column; - gap: 20px; - backdrop-filter: blur(4px); - background: rgba(129, 129, 129, 0.4); - color: ${(props) => props.theme.colors.white}; - border-radius: 12px; - } -` - export default function page({}: Props) { - const [admin, setAdmin] = useState({ - login: '', - password: '' - }) - return ( - -
-

Админ панель

- ) => - setAdmin({ ...admin, login: e.target.value }) - } - placeholder='Логин' - /> - ) => - setAdmin({ ...admin, password: e.target.value }) - } - placeholder='Пароль' - /> - Войти -
-
+ ) -} +} \ No newline at end of file diff --git a/src/app/admin/page.tsx b/src/app/admin/page.tsx index 783df4c..5db4ca6 100644 --- a/src/app/admin/page.tsx +++ b/src/app/admin/page.tsx @@ -9,7 +9,14 @@ export default function page({}: Props) { const router = useRouter() useEffect(() => { - router.push('/admin/applications') + const hasCookie = document.cookie.includes('_identid') + if (!hasCookie) { + router.push('/admin/auth') + return + } + else { + router.push('/admin/applications') + } }, []) return <> diff --git a/src/components/layout/AdminSideBar.tsx b/src/components/layout/AdminSideBar.tsx index 9bcd6a4..1d5b023 100644 --- a/src/components/layout/AdminSideBar.tsx +++ b/src/components/layout/AdminSideBar.tsx @@ -1,7 +1,9 @@ 'use client' +import UserService from '@/services/user/userServices' import Link from 'next/link' -import { usePathname } from 'next/navigation' +import { usePathname, useRouter } from 'next/navigation' +import { useEffect } from 'react' import styled from 'styled-components' type Props = {} @@ -36,8 +38,32 @@ const Container = styled.div` export default function AdminSideBar({}: Props) { const pathname = usePathname() + const router = useRouter() - return pathname.startsWith('/admin') && pathname !== '/admin/auth' ? ( + useEffect(() => { + const hasCookie = document.cookie.includes('_identid') + if (!hasCookie && pathname.startsWith('/admin')) { + router.push('/admin/auth') + return + } + }, []) + + const logout = async () => { + try { + const response = await UserService.exit() + if (response.status === 200) { + router.push('/admin/auth') + document.cookie = + '_identid=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;' + } + } catch (error) { + console.log(error); + } + } + + return pathname.startsWith('/admin') && + pathname !== '/admin/auth' && + pathname !== '/admin' ? (
@@ -59,12 +85,10 @@ export default function AdminSideBar({}: Props) {

Вечная память

*/} - -
+

Выйти

-
) : null diff --git a/src/components/screens/admin/auth/Auth.tsx b/src/components/screens/admin/auth/Auth.tsx index e69de29..8832556 100644 --- a/src/components/screens/admin/auth/Auth.tsx +++ b/src/components/screens/admin/auth/Auth.tsx @@ -0,0 +1,146 @@ +'use client' + +import MainButton from '@/components/UI/button/MainButton' +import FormInput from '@/components/UI/input/FormInput' +import Loader from '@/components/UI/loader/Loader' +import UserService from '@/services/user/userServices' +import { useRouter } from 'next/navigation' +import { ChangeEvent, FormEvent, useState } from 'react' +import styled from 'styled-components' + +type Props = {} + +const Container = styled.div` + width: 100%; + height: 100vh; + background-image: url('/image 12.png'); + background-position: center; + background-size: cover; + + display: flex; + align-items: center; + justify-content: center; + position: absolute; + top: 0; + left: 0; + + .form { + width: 440px; + padding: 20px; + text-align: center; + display: flex; + justify-content: center; + flex-direction: column; + gap: 20px; + backdrop-filter: blur(4px); + background: rgba(129, 129, 129, 0.4); + color: ${(props) => props.theme.colors.white}; + border-radius: 12px; + } +` + +const NotificationContainer = styled.div` + position: absolute; + width: 100%; + height: 100%; + top: 0; + left: 0; + background: rgba(255, 255, 255, 0.5); + border-radius: 12px; + display: flex; + align-items: center; + justify-content: center; + + .container { + background: ${(props) => props.theme.colors.white}; + min-height: 100px; + min-width: 100px; + padding: 15px; + border-radius: 12px; + ${(props) => props.theme.shadow} + display: flex; + align-items: center; + justify-content: center; + flex-direction: column; + gap: 15px; + color: ${(props) => props.theme.colors.black}; + } +` + +export default function Auth({}: Props) { + const router = useRouter() + + const [admin, setAdmin] = useState({ + login: '', + password: '' + }) + + const [errorMessage, setErrorMessage] = useState('') + const [errorState, setErrorState] = useState(false) + const [loading, setLoading] = useState(false) + + const authFunc = async (e: FormEvent) => { + e.preventDefault() + setLoading(true) + try { + const response = await UserService.auth(admin) + if (response.status === 200) { + router.push('/admin/applications') + } else { + setErrorMessage(response.data) + setErrorState(true) + } + } catch (error) { + setErrorMessage('Сервер недоступен') + setErrorState(true) + } + setLoading(false) + } + + return ( + <> + +
+

Админ панель

+ ) => + setAdmin({ ...admin, login: e.target.value }) + } + placeholder='Логин' + /> + ) => + setAdmin({ ...admin, password: e.target.value }) + } + placeholder='Пароль' + type='password' + /> + Войти + {loading ? ( + +
+ +
+
+ ) : ( + errorState && ( + +
+

{errorMessage}

+ setErrorState(false)} + > + Закрыть + +
+
+ ) + )} + +
+ + ) +} diff --git a/src/components/screens/admin/service/Services.tsx b/src/components/screens/admin/service/Services.tsx index 9c3ace3..4eaeb36 100644 --- a/src/components/screens/admin/service/Services.tsx +++ b/src/components/screens/admin/service/Services.tsx @@ -137,29 +137,23 @@ export default function Services({}: Props) { {loadingState ? ( - <> - ) : services.length !== 0 ? ( - <> -
- {services.map((item: T_Product, index) => ( - - ))} -
- +
+ {services.map((item: T_Product, index) => ( + + ))} +
) : ( - <> - -

Ничего не найдено

-
- + +

Ничего не найдено

+
)} ) : ( diff --git a/src/components/screens/admin/service/blocks/AddServiceForm.tsx b/src/components/screens/admin/service/blocks/AddServiceForm.tsx index 0f154e4..97e772d 100644 --- a/src/components/screens/admin/service/blocks/AddServiceForm.tsx +++ b/src/components/screens/admin/service/blocks/AddServiceForm.tsx @@ -127,6 +127,9 @@ export default function AddServiceForm({ OldPrice: '' }) setSelectedImage(null) + if (iconInputRef.current) { + iconInputRef.current.value = '' + } }, [modal]) const [errorState, setErrorState] = useState(false) @@ -161,16 +164,21 @@ export default function AddServiceForm({ setLoader(true) const response = await ProductService.create(newProduct) if (response.status === 200 && selectedImage) { - const imageResponse = await ProductService.addImage( - response.data.Id, - selectedImage - ) - if (imageResponse.status === 200) { - getServices() - setModal(false) - } else { + try { + const imageResponse = await ProductService.addImage( + response.data.Id, + selectedImage + ) + if (imageResponse.status === 200) { + getServices() + setModal(false) + } else { + setErrorMessage(response.data) + setErrorState(true) + } + } catch (error) { + setErrorMessage('Нет папки images') setErrorState(true) - setErrorMessage(response.data) } } else { setErrorState(true) diff --git a/src/components/screens/admin/service/blocks/ServiceItem.tsx b/src/components/screens/admin/service/blocks/ServiceItem.tsx index 53fa3a2..83d88f0 100644 --- a/src/components/screens/admin/service/blocks/ServiceItem.tsx +++ b/src/components/screens/admin/service/blocks/ServiceItem.tsx @@ -4,6 +4,7 @@ import { useState } from 'react' import styled from 'styled-components' import { T_Product } from '../../../../../services/product/type' import UpdateServiceForm from './UpdateServiceForm' +import { hostName } from '../../../../../../config' const Cart = styled.div` display: flex; @@ -64,7 +65,7 @@ export default function ServiceItem({ item, getServices }: Props) { <>
- +

{item.Name}

{item.FullDesc}

diff --git a/src/components/screens/home/blocks/services/Services.tsx b/src/components/screens/home/blocks/services/Services.tsx index 81f9c7c..023e940 100644 --- a/src/components/screens/home/blocks/services/Services.tsx +++ b/src/components/screens/home/blocks/services/Services.tsx @@ -116,16 +116,16 @@ export default function Services({}: Props) { const getServices = async () => { try { - const response = await ProductService.get() + const response = await ProductService.get('') setServices(response.data.Data) } catch (error) { setErrorState(true) } - setLoadingState(false) } useEffect(() => { getServices() + setLoadingState(false) }, []) const [sliderHeight, setSliderHeight] = useState(0) @@ -153,15 +153,17 @@ export default function Services({}: Props) {

Наши услуги

{loadingState ? ( - <> - - - - + + + ) : errorState ? (

Ошибка, не удалось получить услуги, сервер не доступен.

+ ) : services.length === 0 ? ( + +

Услуг пока нет.

+
) : ( <>
@@ -184,15 +186,17 @@ export default function Services({}: Props) {

Наши услуги

{loadingState ? ( - <> - - - - + + + ) : errorState ? (

Ошибка, не удалось получить услуги, сервер не доступен.

+ ) : services.length === 0 ? ( + +

Услуг пока нет.

+
) : ( <>
diff --git a/src/components/screens/services/Services.tsx b/src/components/screens/services/Services.tsx index 6c5891c..57a4e78 100644 --- a/src/components/screens/services/Services.tsx +++ b/src/components/screens/services/Services.tsx @@ -59,7 +59,7 @@ export default function Services({}: Props) { const getServices = async () => { try { - const response = await ProductService.get() + const response = await ProductService.get('') setServices(response.data.Data) } catch (error) { setErrorState(true) @@ -85,6 +85,10 @@ export default function Services({}: Props) {

Ошибка, не удалось получить услуги, сервер не доступен.

+ ) : services.length === 0 ? ( + +

Услуг пока нет

+
) : ( {services.map((item, index) => ( diff --git a/src/services/order/ordersServices.ts b/src/services/order/ordersServices.ts index 0c5a0b7..2c3f93b 100644 --- a/src/services/order/ordersServices.ts +++ b/src/services/order/ordersServices.ts @@ -1,6 +1,6 @@ -import { hostName } from '@/constants/host' import axios, { AxiosResponse } from 'axios' import { T_NewOrder } from './type' +import { hostName } from '../../../config' export default class OrdersService { static async get(): Promise { diff --git a/src/services/product/productServeces.ts b/src/services/product/productServeces.ts index 5197f59..8796657 100644 --- a/src/services/product/productServeces.ts +++ b/src/services/product/productServeces.ts @@ -1,6 +1,6 @@ -import { hostName } from '@/constants/host' import axios, { AxiosResponse } from 'axios' import { T_NewProduct, T_Product } from './type' +import { hostName } from '../../../config' export default class ProductService { static async get(searchQuery: string): Promise { diff --git a/src/services/user/userServices.ts b/src/services/user/userServices.ts new file mode 100644 index 0000000..99cdbb6 --- /dev/null +++ b/src/services/user/userServices.ts @@ -0,0 +1,28 @@ +import axios, { AxiosResponse } from 'axios' +import { hostName } from '../../../config' + +type T_user = { + login: string + password: string +} + +export default class UserService { + static async auth(user: T_user): Promise { + try { + const response = await axios.get( + `${hostName}/auth?a123=${user.login}&a321=${user.password}` + ) + return response + } catch (error: any) { + return error.response + } + } + static async exit(): Promise { + try { + const response = await axios.get(`${hostName}/exit`) + return response + } catch (error: any) { + return error.response + } + } +}