aboutsummaryrefslogtreecommitdiffstats
path: root/src/views/Things/ThingsHistory.jsx
blob: c043a552d7338486e070b5c587b90f5eb83237ae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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 (
      <Container
        maxWidth='md'
        sx={{
          textAlign: 'center',
          display: 'flex',
          // make sure the content is centered vertically:
          alignItems: 'center',
          justifyContent: 'center',
          flexDirection: 'column',
          height: '50vh',
        }}
      >
        <EventBusy
          sx={{
            fontSize: '6rem',
            // color: 'text.disabled',
            mb: 1,
          }}
        />

        <Typography level='h3' gutterBottom>
          No history found
        </Typography>
        <Typography level='body1'>
          It's look like there is no history for this thing yet.
        </Typography>
        <Button variant='soft' sx={{ mt: 2 }}>
          <Link to='/things'>Go back to things</Link>
        </Button>
      </Container>
    )
  }

  return (
    <Container maxWidth='md'>
      <Typography level='h3' mb={1.5}>
        History:
      </Typography>
      <Box sx={{ borderRadius: 'sm', p: 2, boxShadow: 'md' }}>
        <List sx={{ p: 0 }}>
          {thingsHistory.map((history, index) => (
            <>
              <ListItem sx={{ gap: 1.5, alignItems: 'flex-start' }}>
                <ListItemContent sx={{ my: 0 }}>
                  <Box
                    sx={{
                      display: 'flex',
                      justifyContent: 'space-between',
                      alignItems: 'center',
                    }}
                  >
                    <Typography level='body1' sx={{ fontWeight: 'md' }}>
                      {moment(history.updatedAt).format(
                        'ddd MM/DD/yyyy HH:mm:ss',
                      )}
                    </Typography>
                    <Chip>{history.state === '1' ? 'Active' : 'Inactive'}</Chip>
                  </Box>
                </ListItemContent>
              </ListItem>
              {index < thingsHistory.length - 1 && (
                <>
                  <ListDivider component='li'>
                    {/* time between two completion: */}
                    {index < thingsHistory.length - 1 &&
                      thingsHistory[index + 1].createdAt && (
                        <Typography level='body3' color='text.tertiary'>
                          {formatTimeDifference(
                            history.createdAt,
                            thingsHistory[index + 1].createdAt,
                          )}{' '}
                          before
                        </Typography>
                      )}
                  </ListDivider>
                </>
              )}
            </>
          ))}
        </List>
      </Box>
      {/* Load more Button  */}
      <Box
        sx={{
          display: 'flex',
          justifyContent: 'center',
          mt: 2,
        }}
      >
        <Button
          variant='plain'
          fullWidth
          color='primary'
          onClick={handleLoadMore}
          disabled={noMoreHistory}
        >
          {noMoreHistory ? 'No more history' : 'Load more'}
        </Button>
      </Box>
    </Container>
  )
}

export default ThingsHistory