import AdminRows from '../AdminRows/AdminRows';
import { deleteQuery, getQueries } from '@/api/cache/query';
import type { ResponseQueries } from '@/api/cache/types';
import { useEffect, useState } from 'react';

export default function AdminTable() {
  const [data, setData] = useState<ResponseQueries>({});

  const handleClick = async (name: string) => {
    await deleteQuery(name);
    const queryList = await getQueries();
    setData(queryList);
  };

  useEffect(() => {
    const getRows = async () => {
      const queryList = await getQueries();
      // debugger;
      setData(queryList);
    };

    getRows();
  }, []);

  return (
    <>
      {Object.keys(data).map((key, index) => (
        <AdminRows
          key={key}
          queries={data[key].queries}
          name={key}
          index={index}
          onClick={handleClick}
        />
      ))}
    </>
  );
}
