11import { useCallback } from 'react' ;
2- import { isDefined , isNotDefined } from '@togglecorp/fujs' ;
2+ import { isDefined } from '@togglecorp/fujs' ;
33import {
44 type Error ,
55 type EntriesAsList ,
@@ -12,35 +12,13 @@ import TextInput from '#components/TextInput';
1212import DateInput from '#components/DateInput' ;
1313import NumberInput from '#components/NumberInput' ;
1414import useTranslation from '#hooks/useTranslation' ;
15- import { formatDate } from '#utils/common' ;
15+ import { addNumMonthsToDate , encodeDate } from '#utils/common' ;
1616
1717import { type PartialFinalReport } from '../schema' ;
1818
1919import i18n from './i18n.json' ;
2020import styles from './styles.module.css' ;
2121
22- function calculateEndDate (
23- startDate : string | undefined ,
24- duration : number | undefined ,
25- ) {
26- if ( isNotDefined ( startDate ) || isNotDefined ( duration ) ) {
27- return undefined ;
28- }
29- const approvalDate = new Date ( startDate ) ;
30- if ( Number . isNaN ( approvalDate ) ) {
31- return undefined ;
32- }
33- // FIXME: use a separate utility
34- approvalDate . setMonth (
35- approvalDate . getMonth ( ) + duration + 1 ,
36- ) ;
37- approvalDate . setDate ( 0 ) ;
38- approvalDate . setHours ( 0 , 0 , 0 , 0 ) ;
39-
40- // NOTE: need to change the data type
41- return formatDate ( approvalDate , 'yyyy-MM-dd' ) ;
42- }
43-
4422type Value = PartialFinalReport ;
4523
4624interface Props {
@@ -65,12 +43,12 @@ function Submission(props: Props) {
6543 const handleTotalOperationTimeframeChange = useCallback (
6644 ( val : number | undefined , name : 'total_operation_timeframe' ) => {
6745 setFieldValue ( val , name ) ;
68- const endDate = calculateEndDate (
46+ const endDate = addNumMonthsToDate (
6947 value . operation_start_date ,
7048 val ,
7149 ) ;
7250 if ( isDefined ( endDate ) ) {
73- setFieldValue ( endDate , 'operation_end_date' ) ;
51+ setFieldValue ( encodeDate ( endDate ) , 'operation_end_date' ) ;
7452 }
7553 } ,
7654 [ setFieldValue , value . operation_start_date ] ,
@@ -79,12 +57,12 @@ function Submission(props: Props) {
7957 const handleOperationStartDateChange = useCallback (
8058 ( val : string | undefined , name : 'operation_start_date' ) => {
8159 setFieldValue ( val , name ) ;
82- const endDate = calculateEndDate (
60+ const endDate = addNumMonthsToDate (
8361 val ,
8462 value . total_operation_timeframe ,
8563 ) ;
8664 if ( isDefined ( endDate ) ) {
87- setFieldValue ( endDate , 'operation_end_date' ) ;
65+ setFieldValue ( encodeDate ( endDate ) , 'operation_end_date' ) ;
8866 }
8967 } ,
9068 [ setFieldValue , value . total_operation_timeframe ] ,
0 commit comments