forked from codeforboston/home-energy-analysis-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnergyUseHistoryChart.tsx
177 lines (154 loc) · 5.56 KB
/
EnergyUseHistoryChart.tsx
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
176
177
import { useState, useEffect } from 'react'
import { type z } from 'zod'
import { type UsageDataSchema, type BillingRecordsSchema } from '#/types/types.ts'
import { Checkbox } from '../../../../components/ui/checkbox.tsx'
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '../../../../components/ui/table.tsx'
import HeatingUsage from './assets/HeatingUsage.svg'
import HelpCircle from './assets/help-circle.svg'
import NonHeatingUsage from './assets/NonHeatingUsage.svg'
import NotAllowedInCalculations from './assets/NotAllowedInCalculations.svg'
import { tr } from '@faker-js/faker'
// type NaturalGasBillRecord = z.infer<typeof NaturalGasBillRecordZod>
// const naturalGasBillRecord01: NaturalGasBillRecord = {
// periodStartDate: new Date('12/08/2017'),
// periodEndDate: new Date('01/07/2018'),
// usageTherms: 197,
// inclusionOverride: 'Include',
// }
// const naturalGasBillRecord02: NaturalGasBillRecord = {
// periodStartDate: new Date('01/08/2018'),
// periodEndDate: new Date('02/07/2018'),
// usageTherms: 205,
// inclusionOverride: 'Include',
// }
// const naturalGasBillRecord03: NaturalGasBillRecord = {
// periodStartDate: new Date('02/08/2018'),
// periodEndDate: new Date('03/07/2018'),
// usageTherms: 220,
// inclusionOverride: 'Include',
// }
// const naturalGasBillRecord04: NaturalGasBillRecord = {
// periodStartDate: new Date('03/08/2018'),
// periodEndDate: new Date('04/07/2018'),
// usageTherms: 196,
// inclusionOverride: 'Include',
// }
// const naturalGasBill = [
// naturalGasBillRecord01,
// naturalGasBillRecord02,
// naturalGasBillRecord03,
// naturalGasBillRecord04,
// ]
export function EnergyUseHistoryChart({ usage_data }: { usage_data: UsageDataSchema }) {
const [billingRecords, setBillingRecords] = useState<BillingRecordsSchema>([])
useEffect(() => {
if (usage_data?.billing_records) {
// Process the billing records directly without converting from Map
setBillingRecords(usage_data.billing_records)
}
}, [usage_data])
const handleOverrideCheckboxChange = (index: number) => {
setBillingRecords((prevRecords) => {
const newRecords = structuredClone(prevRecords)
const period = newRecords[index]
if (period) {
const currentOverride = period.inclusion_override
// Toggle 'inclusion_override'
period.inclusion_override = !currentOverride
newRecords[index] = { ...period }
}
return newRecords
})
}
return (
<Table id="EnergyUseHistoryChart" className='text-center border rounded-md border-neutral-300'>
<TableHeader>
<TableRow className='text-xs text-muted-foreground bg-neutral-50'>
<TableHead className="text-center">#</TableHead>
<TableHead className='text-center'>
<div className="flex flex-row">
<div className='text-right'>Allowed Usage</div>
{/* TODO: add help text */}
{/* <img src={HelpCircle} alt='help text' className='pl-2'/> */}
</div>
</TableHead>
<TableHead className='text-center'>Start Date</TableHead>
<TableHead className='text-center'>End Date</TableHead>
<TableHead className='text-center'>Days in Period</TableHead>
<TableHead className='text-center'>Gas Usage (therms)</TableHead>
<TableHead className='text-center'>Whole-home UA (BTU/h-°F)</TableHead>
<TableHead className='text-center'>
<div className="flex flex-row">
<div className='text-right'>Override Default</div>
{/* TODO: add help text */}
{/* <img src={HelpCircle} alt='help text' className='pl-2'/> */}
</div>
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{billingRecords.map((period, index) => {
const startDate = new Date(period.period_start_date)
const endDate = new Date(period.period_end_date)
// Calculate days in period
const timeInPeriod = endDate.getTime() - startDate.getTime()
const daysInPeriod = Math.round(timeInPeriod / (1000 * 3600 * 24))
// Set Analysis Type image and checkbox setting
const analysisType = period.analysis_type
let analysisType_Image = undefined
let overrideCheckboxDisabled = false
/* switch case for 1, -1, 0 */
switch (analysisType) {
case 1:
analysisType_Image = HeatingUsage
break
case -1:
analysisType_Image = NonHeatingUsage
break
case 0:
analysisType_Image = NotAllowedInCalculations
overrideCheckboxDisabled = true
break
}
// Adjust inclusion for user input
let calculatedInclusion = period.default_inclusion_by_calculation
if (period.inclusion_override) {
calculatedInclusion = !calculatedInclusion
}
const variant = calculatedInclusion ? 'included' : 'excluded'
return (
<TableRow key={index} variant={variant}>
<TableCell className="font-medium">{index + 1}</TableCell>
<TableCell className='justify-items-center'>
<img src={analysisType_Image} alt="Analysis Type" />
</TableCell>
<TableCell>{startDate.toLocaleDateString()}</TableCell>
<TableCell>{endDate.toLocaleDateString()}</TableCell>
<TableCell>{daysInPeriod}</TableCell>
<TableCell>{period.usage}</TableCell>
<TableCell>
{period.whole_home_heat_loss_rate
? period.whole_home_heat_loss_rate.toFixed(0)
: '-'}
</TableCell>
<TableCell>
<Checkbox
checked={period.inclusion_override}
disabled={overrideCheckboxDisabled}
onClick={() => handleOverrideCheckboxChange(index)}
/>
</TableCell>
</TableRow>
)
})}
</TableBody>
</Table>
)
}