From d3150dba6eff0c2f7373803d01ef1f4b58613b8e Mon Sep 17 00:00:00 2001 From: obarykina Date: Wed, 27 Mar 2024 15:22:52 +0300 Subject: [PATCH] apps/web:/Components added AdminRow and AdminRows components --- .../Components/Admin/AdminRow/AdminRow.tsx | 40 ++++++++++--- .../Components/Admin/AdminRows/AdminRows.tsx | 22 ++++--- .../Admin/AdminTable/AdminTable.tsx | 57 ++++++++++++++----- 3 files changed, 89 insertions(+), 30 deletions(-) diff --git a/apps/web/Components/Admin/AdminRow/AdminRow.tsx b/apps/web/Components/Admin/AdminRow/AdminRow.tsx index af9b85c..021d0ee 100644 --- a/apps/web/Components/Admin/AdminRow/AdminRow.tsx +++ b/apps/web/Components/Admin/AdminRow/AdminRow.tsx @@ -1,13 +1,16 @@ import style from '../AdminTable/Admin.module.css'; +import { getQueryValue } from '@/api/cache/query'; +import type { ResponseQueries } from '@/api/cache/types'; import type { CollapseProps } from 'antd'; import { Button, Collapse } from 'antd'; +import { useState } from 'react'; import styled from 'styled-components'; type IProps = { readonly data: string; readonly index: number; readonly name: string; - readonly onClick: (name: string) => void; + readonly onClick: (name: string, key: string) => void; }; const Wrapper = styled.div` @@ -17,12 +20,7 @@ const Wrapper = styled.div` export default function AdminRow(props: IProps) { const { data, index, name, onClick } = props; - - const handleClick = () => { - onClick(name); - }; - - const items: CollapseProps['items'] = [ + const defaultItems: CollapseProps['items'] = [ { children:

{data}

, key: index.toString(), @@ -30,10 +28,36 @@ export default function AdminRow(props: IProps) { }, ]; + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const [queryText, setQueryText] = useState({}); + const [items, setItems] = useState(defaultItems); + // eslint-disable-next-line @typescript-eslint/no-unused-vars + const [key, setKey] = useState(data); + + const handleClick = () => { + onClick(name, key); + }; + + const handleChange = async () => { + const query = await getQueryValue(key); + setQueryText(query); + setItems( + [...items].map((obj) => ({ + ...obj, + children:

lolo

, + })) + ); + }; + return ( - + ); } + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +function addSlashToString(key: string) { + return key.replace(`"`, '"'); +} diff --git a/apps/web/Components/Admin/AdminRows/AdminRows.tsx b/apps/web/Components/Admin/AdminRows/AdminRows.tsx index a6b5787..ade5f78 100644 --- a/apps/web/Components/Admin/AdminRows/AdminRows.tsx +++ b/apps/web/Components/Admin/AdminRows/AdminRows.tsx @@ -1,6 +1,6 @@ import AdminRow from '../AdminRow/AdminRow'; import type { CollapseProps } from 'antd'; -import { Collapse } from 'antd'; +import { Collapse } from 'ui/elements'; export default function AdminRows({ index, @@ -10,11 +10,11 @@ export default function AdminRows({ }: { readonly index: number; readonly name: string; - readonly onClick: (name: string) => void; + readonly onClick: (name: string, key: string) => void; readonly queries: string[]; }) { if (queries.length === 0) { - const items2: CollapseProps['items'] = [ + const items: CollapseProps['items'] = [ { children: null, key: index.toString(), @@ -22,14 +22,18 @@ export default function AdminRows({ }, ]; - return ; + return ; } - const rows: CollapseProps['items'] = queries.map((x, ind) => ({ - children: , - key: index.toString(), - label: name, - })); + const rows: CollapseProps['items'] = [ + { + children: queries.map((x, ind) => ( + + )), + key: index.toString(), + label: name, + }, + ]; return ; } diff --git a/apps/web/Components/Admin/AdminTable/AdminTable.tsx b/apps/web/Components/Admin/AdminTable/AdminTable.tsx index 5f6bff7..38cde9d 100644 --- a/apps/web/Components/Admin/AdminTable/AdminTable.tsx +++ b/apps/web/Components/Admin/AdminTable/AdminTable.tsx @@ -1,21 +1,49 @@ import AdminRows from '../AdminRows/AdminRows'; -import { deleteQuery, getQueries } from '@/api/cache/query'; +import { deleleteQueriesByKey, deleteQuery, getQueries } from '@/api/cache/query'; import type { ResponseQueries } from '@/api/cache/types'; import { useEffect, useState } from 'react'; +import type { QueryItem } from 'shared/types/cache'; +import styled from 'styled-components'; +import { Button } from 'ui/elements'; + +const Wrapper = styled.div` + display: flex; + justify-content: space-between; + > * { + &:first-child { + flex-grow: 1; + } + } +`; export default function AdminTable() { const [data, setData] = useState({}); - const handleClick = async (name: string) => { - await deleteQuery(name); - const queryList = await getQueries(); - setData(queryList); + const handleClick = async (name: string, key: string) => { + await deleteQuery(key); + + const index = data[name].queries.indexOf(key); + const updatedQuery: QueryItem = { ...data[name] }; + + if (index >= 0 && updatedQuery.queries.length > 1) { + setData({ ...data, [name]: { ...data[name], queries: data[name].queries.splice(index, 1) } }); + } else if (index === 0) { + setData({ ...data, [name]: { ...data[name], queries: [] } }); + } + }; + + const handleClickQueriesGroup = async (name: string) => { + await deleleteQueriesByKey(name); + if (data[name].queries.length > 0) { + const updatedQuery = { ...data[name], queries: [] }; + setData({ ...data, [name]: updatedQuery }); + } }; useEffect(() => { const getRows = async () => { const queryList = await getQueries(); - // debugger; + setData(queryList); }; @@ -25,13 +53,16 @@ export default function AdminTable() { return ( <> {Object.keys(data).map((key, index) => ( - + + + + ))} );