Skip to content

Commit 27293ab

Browse files
author
JerryLiu
committed
feat: scopeII finish
1 parent e2b32da commit 27293ab

File tree

11 files changed

+193
-276
lines changed

11 files changed

+193
-276
lines changed

js/src/components/HiddenInput/index.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { CloseCircleFilled } from '@ant-design/icons'
33

44
const HiddenInput: React.FC<{
55
name: string | number | (string | number)[]
6-
initialValue?: string | number
76
required?: boolean
8-
}> = ({ name, initialValue = '○○○', required = false }) => {
7+
}> = ({ name, required = false }) => {
98
const form = Form.useFormInstance()
109
const value = Form.useWatch(name, form)
1110

@@ -14,7 +13,6 @@ const HiddenInput: React.FC<{
1413
<Form.Item
1514
name={name}
1615
hidden
17-
initialValue={initialValue}
1816
rules={[
1917
{
2018
required,

js/src/hooks/useEditableTitle.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,7 @@ const useEditableTitle = ({
4747
>
4848
{editableStr}
4949
</Typography.Title>
50-
{!printMode ? (
51-
<HiddenInput
52-
name={name}
53-
required={required}
54-
initialValue={initialValue}
55-
/>
56-
) : null}
50+
{!printMode ? <HiddenInput name={name} required={required} /> : null}
5751
</>
5852
)
5953

js/src/pages/Check/ScopeII/CheckScopeIITable/Table/components/AddRecordButton/index.tsx

Lines changed: 28 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { createContext, useContext, useState, useEffect } from 'react'
2-
import { Button, Modal, InputNumber, Form, Row, Col } from 'antd'
2+
import { Button, Modal, Row, Col, Form, InputNumber } from 'antd'
33
import { FolderAddFilled } from '@ant-design/icons'
44
import type { TYearlyDataType } from '@/pages/Check/ScopeII/CheckScopeIITable/Table/types'
55
import { nanoid } from 'nanoid'
@@ -16,16 +16,17 @@ const AddRecordButton = () => {
1616
const scopeIIGroups = scopes?.scopeII || []
1717
const group = scopeIIGroups.find((theGroup) => theGroup.groupKey === groupKey)
1818
const dataSource = group?.dataSource || []
19-
2019
const watchYearlyAmount = Form.useWatch(
2120
[
21+
'scopeII',
2222
groupIndex,
2323
'yearlyAmount',
2424
],
2525
form,
2626
)
2727
const watchCo2Kwh = Form.useWatch(
2828
[
29+
'scopeII',
2930
groupIndex,
3031
'co2Kwh',
3132
],
@@ -45,34 +46,42 @@ const AddRecordButton = () => {
4546
const resetFormData = () => {
4647
form.resetFields([
4748
[
49+
'scopeII',
4850
groupIndex,
49-
'electricSource',
51+
'equipment',
5052
],
5153
[
54+
'scopeII',
5255
groupIndex,
5356
'period',
5457
],
5558
[
59+
'scopeII',
5660
groupIndex,
5761
'yearlyAmount',
5862
],
5963
[
64+
'scopeII',
6065
groupIndex,
6166
'monthlyAmount',
6267
],
6368
[
69+
'scopeII',
6470
groupIndex,
6571
'hourlyAmount',
6672
],
6773
[
74+
'scopeII',
6875
groupIndex,
6976
'hours',
7077
],
7178
[
79+
'scopeII',
7280
groupIndex,
7381
'gwp',
7482
],
7583
[
84+
'scopeII',
7685
groupIndex,
7786
'unit',
7887
],
@@ -85,52 +94,18 @@ const AddRecordButton = () => {
8594
}
8695

8796
const handleData = () => {
88-
const formData = form.getFieldsValue()[groupIndex]
89-
console.log('formData', formData)
90-
91-
const getYearlyAmount = (theFormData: any) => {
92-
switch (theFormData?.period) {
93-
case 'yearly':
94-
return convertUnitToTons({
95-
value: theFormData.yearlyAmount ?? 0,
96-
unit: theFormData.unit,
97-
})
98-
case 'monthly':
99-
return convertUnitToTons({
100-
value: (theFormData?.monthlyAmount ?? []).reduce(
101-
(acc: number, cur: number) => acc + cur,
102-
0,
103-
),
104-
unit: theFormData.unit,
105-
})
106-
case 'hourly':
107-
return convertUnitToTons({
108-
value: (theFormData.hourlyAmount ?? 0) * (theFormData.hours ?? 0),
109-
unit: theFormData.unit,
110-
})
111-
default:
112-
return 0
113-
}
114-
}
115-
const yearlyAmount = getYearlyAmount(formData)
116-
117-
const ar5 = gwpMapping.find((gwp) => gwp?.value === formData?.gwp)?.ar5 || 0
97+
const formData = form.getFieldsValue().scopeII[groupIndex]
11898

119-
const carbonTonsPerYear = yearlyAmount * ar5
99+
const yearlyAmount = formData?.yearlyAmount || 0
100+
const co2Kwh = formData.co2Kwh || 1
101+
const carbonTonsPerYear = yearlyAmount * co2Kwh
120102

121103
const theFormatRecord: TYearlyDataType = {
122104
key: nanoid(),
123105
electricSource: formData?.electricSource,
124-
gwp: formData.gwp,
106+
co2Kwh: formData.co2Kwh || 1,
125107
yearlyAmount,
126-
ar5,
127-
co2e: carbonTonsPerYear,
128108
carbonTonsPerYear,
129-
period: formData?.period,
130-
monthlyAmount:
131-
formData?.period === 'monthly' ? formData.monthlyAmount : [],
132-
hourlyAmount: formData?.period === 'hourly' ? formData.hourlyAmount : 0,
133-
unit: formData.unit,
134109
}
135110

136111
return [
@@ -163,23 +138,27 @@ const AddRecordButton = () => {
163138
}
164139

165140
const handleESSelect = (value: any) => {
166-
const isES = Object.keys(value[groupIndex]).includes('electricSource')
141+
const isES = Object.keys(value.scopeII[groupIndex]).includes(
142+
'electricSource',
143+
)
167144

168145
const values = {
169146
...form.getFieldsValue(),
170147
}
148+
149+
console.log('@@@values', values)
171150
if (isES) {
172151
const source = electricSources.find(
173-
(s) => s.value === value[groupIndex].electricSource,
152+
(s) => s.value === value.scopeII[groupIndex].electricSource,
174153
) || {
175154
yearlyAmount: 0,
176-
conversionRate: 1,
155+
co2Kwh: 1,
177156
}
178157

179158
console.log('source', source)
180159

181-
values[groupIndex].yearlyAmount = source.yearlyAmount
182-
values[groupIndex].co2Kwh = source.conversionRate
160+
values.scopeII[groupIndex].yearlyAmount = source.yearlyAmount
161+
values.scopeII[groupIndex].co2Kwh = source.co2Kwh
183162

184163
form.setFieldsValue(values)
185164
}
@@ -220,6 +199,7 @@ const AddRecordButton = () => {
220199
<Form.Item
221200
label="使用度數(年)"
222201
name={[
202+
'scopeII',
223203
groupIndex,
224204
'yearlyAmount',
225205
]}
@@ -245,6 +225,7 @@ const AddRecordButton = () => {
245225
</>
246226
}
247227
name={[
228+
'scopeII',
248229
groupIndex,
249230
'co2Kwh',
250231
]}

0 commit comments

Comments
 (0)