import { Container, Row, Col, Alert } from 'react-bootstrap'; import { useState,…
- User
import { Container, Row, Col, Alert } from 'react-bootstrap'; import { useState, useEffect } from 'react'; import { PageComponent, AddButton } from './BackOfficeComponents'; import ErrorRoute from './ErrorRoute'; import { BackButton, LeftSideBar } from './NavigationComponents'; import API from '../API';
function BackOfficeRoute(props) { //BackOfficeRoute displayed if Route matches "/backoffice"
const [pageList, setPageList] = useState([]); // Array of empty objects const [dirtyPageList, setDirtyPageList] = useState(true); const [isLoading, setIsLoading] = useState(true); useEffect(() => { if (dirtyPageList) { setIsLoading(true); //if dependency of useEffect changes API.getPages() .then((x) => { setPageList(x); setDirtyPageList(false); setIsLoading(false); }) .catch((err) => props.handleError(err)); } }, [dirtyPageList]); const deletePage = (id) => { setPageList((oldList) => oldList.map( e => e.id !== id ? e : Object.assign({}, e, { status: 'deleted' }) // Assign to the correct object a new prop called status to manage the color change int he table )); API.deletePageById(id) .then(() => setDirtyPageList(true)) .catch((err) => props.handleError(err)); } return ( props.loggedIn ? ( <Container fluid> <p></p> <Row> <Col xs={2}> <BackButton /> <LeftSideBar admin={props.admin} loggedIn={props.loggedIn} /> </Col> <Col> <h2 style={{ textAlign: "center" }}>Back Office</h2> {isLoading ? <props.LoadingBody /> : <PageComponent user={props.user} admin={props.admin} deletePage={deletePage} pageList={pageList} /> } </Col> <Col xs={2}> <Alert variant="info" style={{ textAlign: "center" }}> Date format is YYYY-MM-DD </Alert> <div style={{ position: "fixed", bottom: "70px", padding: "25px" }}> <AddButton /> </div> </Col> </Row> </Container> ) : <ErrorRoute /> );}
export default BackOfficeRoute;
it is needed the setIsLoading(true);?
- Assistant
The
setIsLoading(true)line is used to show a loading state while fetching data. When thedirtyPageListchanges and triggers theuseEffecthook, settingisLoadingto true will indicate that new data is being fetched, which is useful to show a loading state (like a spinner) to your users while they wait for the update. If you don't want to show a loading state, you can remove it, but it's generally a good user experience to provide some visual feedback while data is being fetched.