11import { 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'
33import { FolderAddFilled } from '@ant-design/icons'
44import type { TYearlyDataType } from '@/pages/Check/ScopeII/CheckScopeIITable/Table/types'
55import { 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