feat(frontend): complete pagination + device search per id

This commit is contained in:
leandrofars 2024-01-11 19:20:18 -03:00
parent 5fd6afc8fa
commit 638abfe355

View File

@ -9,7 +9,8 @@ import {
InputAdornment, InputAdornment,
SvgIcon, SvgIcon,
Stack, Stack,
Pagination Pagination,
CircularProgress
} from '@mui/material'; } from '@mui/material';
import MagnifyingGlassIcon from '@heroicons/react/24/solid/MagnifyingGlassIcon'; import MagnifyingGlassIcon from '@heroicons/react/24/solid/MagnifyingGlassIcon';
import { Layout as DashboardLayout } from 'src/layouts/dashboard/layout'; import { Layout as DashboardLayout } from 'src/layouts/dashboard/layout';
@ -24,10 +25,11 @@ const Page = () => {
const [deviceFound, setDeviceFound] = useState(false) const [deviceFound, setDeviceFound] = useState(false)
const [pages, setPages] = useState(0); const [pages, setPages] = useState(0);
const [page, setPage] = useState(null); const [page, setPage] = useState(null);
const [Loading, setLoading] = useState(true);
useEffect(() => { useEffect(() => {
setLoading(true)
if (auth.user.token) { if (auth.user.token) {
console.log("auth.user.token =", auth.user.token) console.log("auth.user.token =", auth.user.token)
}else{ }else{
@ -54,6 +56,7 @@ const Page = () => {
setPages(json.pages + 1) setPages(json.pages + 1)
setPage(json.page) setPage(json.page)
setDevices(json.devices) setDevices(json.devices)
setLoading(false)
return setDeviceFound(true) return setDeviceFound(true)
}) })
.catch(error => { .catch(error => {
@ -61,12 +64,14 @@ const Page = () => {
}); });
}, [auth.user]); }, [auth.user]);
const handleChangePage = (e) => { const handleChangePage = (event, value) => {
console.log("new page: ", e.target.value) console.log("new page: ", value)
//TODO: Handle page change setPage(value)
fetchDevicePerPage(value)
} }
const fetchDevicePerId = async (id) => { const fetchDevicePerPage = async (p) => {
setLoading(true)
var myHeaders = new Headers(); var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json"); myHeaders.append("Content-Type", "application/json");
@ -78,8 +83,39 @@ const Page = () => {
redirect: 'follow' redirect: 'follow'
} }
p = p - 1
p = p.toString()
fetch(process.env.NEXT_PUBLIC_REST_ENPOINT+'/device?page_number='+p, requestOptions)
.then(response => {
if (response.status === 401)
router.push("/auth/login")
return response.json()
})
.then(json => {
setDevices(json.devices)
setLoading(false)
return
})
.catch(error => {
return console.error('Error:', error)
});
}
const fetchDevicePerId = async (id) => {
setLoading(true)
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Authorization", auth.user.token);
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
}
if (id == ""){ if (id == ""){
fetch(process.env.NEXT_PUBLIC_REST_ENPOINT+'/device', requestOptions) return fetch(process.env.NEXT_PUBLIC_REST_ENPOINT+'/device', requestOptions)
.then(response => { .then(response => {
if (response.status === 401) if (response.status === 401)
router.push("/auth/login") router.push("/auth/login")
@ -89,6 +125,7 @@ const Page = () => {
setPages(json.pages + 1) setPages(json.pages + 1)
setPage(json.page) setPage(json.page)
setDevices(json.devices) setDevices(json.devices)
setLoading(false)
return setDeviceFound(true) return setDeviceFound(true)
}) })
.catch(error => { .catch(error => {
@ -100,10 +137,10 @@ const Page = () => {
if (response.status === 401) if (response.status === 401)
router.push("/auth/login") router.push("/auth/login")
let json = await response.json() let json = await response.json()
if (json.device != undefined){ if (json.MTP != undefined){
setDevices({"devices":[ setDevices([json])
json.device setDeviceFound(true)
]}) setLoading(false)
setPages(1) setPages(1)
setPage(1) setPage(1)
}else{ }else{
@ -111,6 +148,7 @@ const Page = () => {
setDevices([]) setDevices([])
setPages(1) setPages(1)
setPage(1) setPage(1)
setLoading(false)
} }
} }
@ -162,10 +200,12 @@ const Page = () => {
/> />
</Grid> </Grid>
{deviceFound ? {deviceFound ?
( !Loading ?
<OverviewLatestOrders <OverviewLatestOrders
orders={devices} orders={devices}
sx={{ height: '100%' }} sx={{ height: '100%' }}
/> /> : <CircularProgress></CircularProgress>
)
: :
<Box <Box
sx={{ sx={{