Advertisement
rickyc81

Untitled

Jan 26th, 2022
764
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import { useEffect } from 'react';
  3. import { useStorePageData } from 'hooks/useStorePageData';
  4. import { useState } from 'react';
  5. import Card from '@mui/material/Card';
  6. import CardHeader from '@mui/material/CardHeader';
  7. import Pagination from '@mui/material/Pagination';
  8. import Box from '@mui/system/Box';
  9. import useStoreDelete from 'hooks/useStoreDelete';
  10. import Button from '@mui/material/Button';
  11.  
  12. function AdminHome() {
  13.   const [numberOfPagesOnServer, setNumberOfPagesOnServer] = useState();
  14.   const [dataFetched, setDataFetched] = useState([]);
  15.   const [currentPage, setCurrentPage] = useState(1);
  16.   const [numberOfItems, setNumberOfItems] = useState();
  17.   const { isLoading, data } = useStorePageData(currentPage);
  18.   const { deleteStoreMutate } = useStoreDelete();
  19.  
  20.   useEffect(() => {
  21.     console.log('effect data');
  22.     if (data) {
  23.       setNumberOfItems(data.data.data.length);
  24.       setDataFetched(data.data.data);
  25.       setNumberOfPagesOnServer(data.data.numberOfPages);
  26.     }
  27.   }, [data, currentPage, setCurrentPage]);
  28.  
  29.   const handleChange = (_, value) => {
  30.     console.log(
  31.       `Dentro l'onChange della paginazione: ${numberOfPagesOnServer}`
  32.    );
  33.    setCurrentPage(value);
  34.  };
  35.  
  36.  const deleteStoreHandler = (_id) => {
  37.    deleteStoreMutate(_id);
  38.    if (numberOfItems === 1) {
  39.      setCurrentPage(currentPage - 1);
  40.    }
  41.  };
  42.  
  43.  return (
  44.    <>
  45.      {isLoading && '...loading'}
  46.      {dataFetched.map((data, index) => (
  47.        <Card key={index} sx={{ m: 2 }}>
  48.          <CardHeader
  49.            title={data.nome}
  50.            subheader={`deviceId: ${data.deviceId}`}
  51.          ></CardHeader>
  52.          <Button
  53.            onClick={() => {
  54.              deleteStoreHandler(data._id);
  55.            }}
  56.          >
  57.            Cancella
  58.          </Button>
  59.        </Card>
  60.      ))}
  61.      <Box sx={{ width: '100%', display: 'flex', justifyContent: 'center' }}>
  62.        Variabile numero di pagine {numberOfPagesOnServer}
  63.        <Pagination
  64.          count={numberOfPagesOnServer}
  65.          color='primary'
  66.          page={currentPage}
  67.          onChange={handleChange}
  68.        />
  69.      </Box>
  70.    </>
  71.  );
  72. }
  73.  
  74. export default AdminHome;
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement