-
Notifications
You must be signed in to change notification settings - Fork 0
/
useFromToDateTime.js
179 lines (160 loc) · 5.7 KB
/
useFromToDateTime.js
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
178
179
import { EndDateField } from 'app/model/field/EndDateField';
import { EndTimeField } from 'app/model/field/EndTimeField';
import { StartDateField } from 'app/model/field/StartDateField';
import { StartTimeField } from 'app/model/field/StartTimeField';
import { DateUtils } from 'core/utils/DateUtils';
import {
addDays,
addHours,
addMinutes,
differenceInCalendarDays,
differenceInMinutes,
isValid,
} from 'date-fns';
import { useState } from 'react';
export default function useFromToDateTime(baseline = new Date(Date.now())) {
const [dateTimeFields, setDateTimeFields] = useState(() => {
const startDate = new StartDateField(initStartDate(baseline), '指定は現在日時以降のみ');
const startTime = new StartTimeField(initStartTime(baseline));
const endDate = new EndDateField(initEndDate(startDate.value));
const endTime = new EndTimeField(initEndTime(startTime.value));
return {
startDate,
startTime,
endDate,
endTime,
old: {
startDate: { value: startDate.value },
startTime: { value: startTime.value },
endDate: { value: endDate.value },
endTime: { value: endTime.value },
},
};
});
const changeDateTime = (date, fieldName) => {
dateTimeFields[fieldName].value = date;
dateTimeFields[fieldName].validate(dateTimeFields);
setDateTimeFields({ ...dateTimeFields });
};
const getStartDateTime = () => {
return DateUtils.concatDateTime(dateTimeFields.startDate.value, dateTimeFields.startTime.value);
};
const getEndDateTime = () => {
return DateUtils.concatDateTime(dateTimeFields.endDate.value, dateTimeFields.endTime.value);
};
const checkMultiFields = (changeField) => {
if (!dateTimeFields[changeField].depended) {
return;
}
const dependedFieldNames = dateTimeFields[changeField].depended;
[...dependedFieldNames].forEach((dep) => {
dateTimeFields[dep].validate(dateTimeFields);
});
setDateTimeFields({ ...dateTimeFields });
};
// onBlurかPickerのonCloseが入力の確定タイミングとなるのでそのタイミングでこのメソッドを呼び出し調整することを想定
// 入力途中の開始日との比較とならないように入力確定タイミングで前回入力確定時の値をoldに持っておく
const adjustByChangeOfStartDate = () => {
if (dateTimeFields.startDate.error) {
return;
}
if (dateTimeFields.endDate.error) {
// 調整は行わないがstartDateは確定しているのでoldに保存しておく
dateTimeFields.old.startDate.value = dateTimeFields.startDate.value;
setDateTimeFields({ ...dateTimeFields });
return;
}
const diff = differenceInCalendarDays(
dateTimeFields.endDate.value,
dateTimeFields.old.startDate.value
);
const adjustedEndDate = addDays(dateTimeFields.startDate.value, diff);
if (!isValid(adjustedEndDate)) {
console.log('adjustByChangeOfStartDateでadjustedEndDateの導出に失敗しました');
return;
}
dateTimeFields.endDate.value = adjustedEndDate;
dateTimeFields.old.startDate.value = dateTimeFields.startDate.value;
setDateTimeFields({ ...dateTimeFields });
};
// 考え方はadjustByChangeOfStartDateと同じ
const adjustByChangeOfStartTime = () => {
if (dateTimeFields.startTime.error) {
return;
}
if (
dateTimeFields.startDate.error ||
dateTimeFields.endDate.error ||
dateTimeFields.endTime.error
) {
dateTimeFields.old.startTime.value = dateTimeFields.startTime.value;
setDateTimeFields({ ...dateTimeFields });
return;
}
const compEndDateTime = getEndDateTime();
const compStartDateTime = DateUtils.concatDateTime(
dateTimeFields.startDate.value,
dateTimeFields.old.startTime.value // 開始時間は変更確定前の値
);
const diff = differenceInMinutes(compEndDateTime, compStartDateTime);
const adjustedEndDateTime = addMinutes(getStartDateTime(), diff);
if (!isValid(adjustedEndDateTime)) {
console.log('adjustByChangeOfStartTimeでadjustedEndDateTimeの導出に失敗しました');
return;
}
dateTimeFields.endDate.value = DateUtils.trancateHours(new Date(adjustedEndDateTime.getTime()));
dateTimeFields.endTime.value = DateUtils.trancateSeconds(
new Date(adjustedEndDateTime.getTime())
);
dateTimeFields.old.startTime.value = dateTimeFields.startTime.value;
setDateTimeFields({ ...dateTimeFields });
};
const adjust = {
startDate: adjustByChangeOfStartDate,
startTime: adjustByChangeOfStartTime,
};
const checkStartDateTimePastNow = () => {
const result = dateTimeFields.startTime.validate(dateTimeFields);
setDateTimeFields({ ...dateTimeFields });
return result;
};
const processOnInputFixTiming = (fieldName) => {
if (adjust[fieldName]) {
adjust[fieldName]();
}
checkMultiFields(fieldName);
};
const allOk = () => {
return (
dateTimeFields.startDate.ok() &&
dateTimeFields.startTime.ok() &&
dateTimeFields.endDate.ok() &&
dateTimeFields.endTime.ok()
);
};
return {
...dateTimeFields,
changeDateTime,
getStartDateTime,
getEndDateTime,
adjust,
checkStartDateTimePastNow,
processOnInputFixTiming,
allOk,
};
}
function initStartDate(baseline) {
let date = addHours(baseline.getTime(), 1);
return DateUtils.trancateHours(date);
}
function initStartTime(baseline) {
let date = addHours(baseline.getTime(), 1);
return DateUtils.trancateMinutes(date);
}
function initEndDate(baseline) {
let date = addDays(baseline.getTime(), 1);
return DateUtils.trancateHours(date);
}
function initEndTime(baseline) {
return new Date(baseline.getTime());
}