aboutsummaryrefslogtreecommitdiffstats
path: root/src/views/History/HistoryCard.jsx
blob: 5e71f521af4f869340ca1451f1ff8f9d7e86e09c (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
import { CalendarViewDay, Check, Timelapse } from '@mui/icons-material'
import {
  Avatar,
  Box,
  Chip,
  ListDivider,
  ListItem,
  ListItemContent,
  ListItemDecorator,
  Typography,
} from '@mui/joy'
import moment from 'moment'

const HistoryCard = ({ allHistory, performers, historyEntry, index }) => {
  function 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' : ''}`
  }

  const getCompletedChip = historyEntry => {
    var text = 'No Due Date'
    var color = 'info'
    var icon = <CalendarViewDay />
    // if completed few hours +-6 hours
    if (
      historyEntry.dueDate &&
      historyEntry.completedAt > historyEntry.dueDate - 1000 * 60 * 60 * 6 &&
      historyEntry.completedAt < historyEntry.dueDate + 1000 * 60 * 60 * 6
    ) {
      text = 'On Time'
      color = 'success'
      icon = <Check />
    } else if (
      historyEntry.dueDate &&
      historyEntry.completedAt < historyEntry.dueDate
    ) {
      text = 'On Time'
      color = 'success'
      icon = <Check />
    }

    // if completed after due date then it's late
    else if (
      historyEntry.dueDate &&
      historyEntry.completedAt > historyEntry.dueDate
    ) {
      text = 'Late'
      color = 'warning'
      icon = <Timelapse />
    } else {
      text = 'No Due Date'
      color = 'info'
      icon = <CalendarViewDay />
    }

    return (
      <Chip startDecorator={icon} color={color}>
        {text}
      </Chip>
    )
  }

  return (
    <>
      <ListItem sx={{ gap: 1.5, alignItems: 'flex-start' }}>
        {' '}
        {/* Adjusted spacing and alignment */}
        <ListItemDecorator>
          <Avatar sx={{ mr: 1 }}>
            {performers
              .find(p => p.userId === historyEntry.completedBy)
              ?.displayName?.charAt(0) || '?'}
          </Avatar>
        </ListItemDecorator>
        <ListItemContent sx={{ my: 0 }}>
          {' '}
          {/* Removed vertical margin */}
          <Box
            sx={{
              display: 'flex',
              justifyContent: 'space-between',
              alignItems: 'center',
            }}
          >
            <Typography level='body1' sx={{ fontWeight: 'md' }}>
              {moment(historyEntry.completedAt).format('ddd MM/DD/yyyy HH:mm')}
            </Typography>
            {getCompletedChip(historyEntry)}
          </Box>
          <Typography level='body2' color='text.tertiary'>
            <Chip>
              {
                performers.find(p => p.userId === historyEntry.completedBy)
                  ?.displayName
              }
            </Chip>{' '}
            completed
            {historyEntry.completedBy !== historyEntry.assignedTo && (
              <>
                {', '}
                assigned to{' '}
                <Chip>
                  {
                    performers.find(p => p.userId === historyEntry.assignedTo)
                      ?.displayName
                  }
                </Chip>
              </>
            )}
          </Typography>
          {historyEntry.dueDate && (
            <Typography level='body2' color='text.tertiary'>
              Due: {moment(historyEntry.dueDate).format('ddd MM/DD/yyyy')}
            </Typography>
          )}
          {historyEntry.notes && (
            <Typography level='body2' color='text.tertiary'>
              Note: {historyEntry.notes}
            </Typography>
          )}
        </ListItemContent>
      </ListItem>
      {index < allHistory.length - 1 && (
        <>
          <ListDivider component='li'>
            {/* time between two completion: */}
            {index < allHistory.length - 1 &&
              allHistory[index + 1].completedAt && (
                <Typography level='body3' color='text.tertiary'>
                  {formatTimeDifference(
                    historyEntry.completedAt,
                    allHistory[index + 1].completedAt,
                  )}{' '}
                  before
                </Typography>
              )}
          </ListDivider>
        </>
      )}
    </>
  )
}

export default HistoryCard