import { EventBusy } from '@mui/icons-material' import { Box, Button, Chip, Container, List, ListDivider, ListItem, ListItemContent, Typography, } from '@mui/joy' import moment from 'moment' import { useEffect, useState } from 'react' import { Link, useParams } from 'react-router-dom' import { GetThingHistory } from '../../utils/Fetcher' const ThingsHistory = () => { const { id } = useParams() const [thingsHistory, setThingsHistory] = useState([]) const [noMoreHistory, setNoMoreHistory] = useState(false) const [errLoading, setErrLoading] = useState(false) useEffect(() => { GetThingHistory(id, 0, 10).then(resp => { if (resp.ok) { resp.json().then(data => { setThingsHistory(data.res) if (data.res.length < 10) { setNoMoreHistory(true) } }) } else { setErrLoading(true) } }) }, []) const handleLoadMore = () => { GetThingHistory(id, thingsHistory.length).then(resp => { if (resp.ok) { resp.json().then(data => { setThingsHistory([...thingsHistory, ...data.res]) if (data.res.length < 10) { setNoMoreHistory(true) } }) } }) } const formatTimeDifference = (startDate, endDate) => { const diffInMinutes = moment(startDate).diff(endDate, 'minutes') let timeValue = diffInMinutes let unit = 'minute' if (diffInMinutes >= 60) { const diffInHours = moment(startDate).diff(endDate, 'hours') timeValue = diffInHours unit = 'hour' if (diffInHours >= 24) { const diffInDays = moment(startDate).diff(endDate, 'days') timeValue = diffInDays unit = 'day' } } return `${timeValue} ${unit}${timeValue !== 1 ? 's' : ''}` } if (errLoading || !thingsHistory) { return ( No history found It's look like there is no history for this thing yet. ) } return ( History: {thingsHistory.map((history, index) => ( <> {moment(history.updatedAt).format( 'ddd MM/DD/yyyy HH:mm:ss', )} {history.state === '1' ? 'Active' : 'Inactive'} {index < thingsHistory.length - 1 && ( <> {/* time between two completion: */} {index < thingsHistory.length - 1 && thingsHistory[index + 1].createdAt && ( {formatTimeDifference( history.createdAt, thingsHistory[index + 1].createdAt, )}{' '} before )} )} ))} {/* Load more Button */} ) } export default ThingsHistory