2021-05-15 06:10:28 +02:00
|
|
|
// @flow
|
2021-12-30 18:36:56 +01:00
|
|
|
import React, { useEffect } from 'react';
|
2021-05-15 06:10:28 +02:00
|
|
|
import Button from 'component/button';
|
|
|
|
import DateTimePicker from 'react-datetime-picker';
|
|
|
|
|
|
|
|
function linuxTimestampToDate(linuxTimestamp: number) {
|
|
|
|
return new Date(linuxTimestamp * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
function dateToLinuxTimestamp(date: Date) {
|
|
|
|
return Number(Math.round(date.getTime() / 1000));
|
|
|
|
}
|
|
|
|
|
|
|
|
const NOW = 'now';
|
|
|
|
const DEFAULT = 'default';
|
|
|
|
const RESET_TO_ORIGINAL = 'reset-to-original';
|
2022-06-23 13:27:08 +02:00
|
|
|
const FUTURE_DATE_ERROR = 'Cannot set to a future date.';
|
2021-05-15 06:10:28 +02:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
releaseTime: ?number,
|
|
|
|
releaseTimeEdited: ?number,
|
|
|
|
updatePublishForm: ({}) => void,
|
2021-12-16 22:59:13 +01:00
|
|
|
allowDefault: ?boolean,
|
|
|
|
showNowBtn: ?boolean,
|
|
|
|
useMaxDate: ?boolean,
|
2021-05-15 06:10:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const PublishReleaseDate = (props: Props) => {
|
2021-12-16 22:59:13 +01:00
|
|
|
const {
|
|
|
|
releaseTime,
|
|
|
|
releaseTimeEdited,
|
|
|
|
updatePublishForm,
|
|
|
|
allowDefault = true,
|
|
|
|
showNowBtn = true,
|
|
|
|
useMaxDate = true,
|
|
|
|
} = props;
|
|
|
|
const maxDate = useMaxDate ? new Date() : undefined;
|
2021-05-15 06:10:28 +02:00
|
|
|
const [date, setDate] = React.useState(releaseTime ? linuxTimestampToDate(releaseTime) : new Date());
|
2022-06-23 13:27:08 +02:00
|
|
|
const [error, setError] = React.useState([]);
|
2021-05-15 06:10:28 +02:00
|
|
|
|
|
|
|
const isNew = releaseTime === undefined;
|
2021-12-16 22:59:13 +01:00
|
|
|
const isEdit = !isNew || allowDefault === false;
|
2021-05-15 06:10:28 +02:00
|
|
|
|
2022-06-03 15:28:12 +02:00
|
|
|
// const showEditBtn = isNew && releaseTimeEdited === undefined && allowDefault !== false;
|
|
|
|
const showEditBtn = false;
|
2021-12-16 22:59:13 +01:00
|
|
|
const showDefaultBtn = isNew && releaseTimeEdited !== undefined && allowDefault !== false;
|
2022-06-03 15:28:12 +02:00
|
|
|
// const showDatePicker = isEdit || releaseTimeEdited !== undefined;
|
|
|
|
const showDatePicker = true;
|
2021-05-15 06:10:28 +02:00
|
|
|
|
2022-06-23 13:27:08 +02:00
|
|
|
const updateError = (action, error) => {
|
|
|
|
switch (action) {
|
|
|
|
case 'remove':
|
|
|
|
setError((prev) => prev.filter((x) => x !== error));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'clear':
|
|
|
|
setError([]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'add':
|
|
|
|
setError((prev) => {
|
|
|
|
const nextError = prev.slice();
|
|
|
|
if (!nextError.includes(error)) {
|
|
|
|
nextError.push(error);
|
|
|
|
return nextError;
|
|
|
|
}
|
|
|
|
return prev;
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-05-15 06:10:28 +02:00
|
|
|
const onDateTimePickerChanged = (value) => {
|
2022-06-23 13:27:08 +02:00
|
|
|
const isValueInFuture = maxDate && value && value.getTime() > maxDate.getTime();
|
|
|
|
if (isValueInFuture) {
|
|
|
|
updateError('add', FUTURE_DATE_ERROR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateError('remove', FUTURE_DATE_ERROR);
|
|
|
|
|
2021-05-15 06:10:28 +02:00
|
|
|
if (value) {
|
|
|
|
newDate(value);
|
|
|
|
} else {
|
|
|
|
// "!value" should never happen since we now hide the "clear" button,
|
|
|
|
// but retained the logic here anyway.
|
|
|
|
if (releaseTime) {
|
|
|
|
newDate(RESET_TO_ORIGINAL);
|
|
|
|
} else {
|
|
|
|
newDate(NOW);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function newDate(value: string | Date) {
|
2022-06-23 13:27:08 +02:00
|
|
|
updateError('clear', FUTURE_DATE_ERROR);
|
|
|
|
|
2021-05-15 06:10:28 +02:00
|
|
|
switch (value) {
|
|
|
|
case NOW:
|
|
|
|
const newDate = new Date();
|
|
|
|
setDate(newDate);
|
|
|
|
updatePublishForm({ releaseTimeEdited: dateToLinuxTimestamp(newDate) });
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DEFAULT:
|
|
|
|
setDate(undefined);
|
|
|
|
updatePublishForm({ releaseTimeEdited: undefined });
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RESET_TO_ORIGINAL:
|
|
|
|
if (releaseTime) {
|
|
|
|
setDate(linuxTimestampToDate(releaseTime));
|
|
|
|
updatePublishForm({ releaseTimeEdited: undefined });
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
if (value instanceof Date) {
|
|
|
|
setDate(value);
|
|
|
|
updatePublishForm({ releaseTimeEdited: dateToLinuxTimestamp(value) });
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-23 13:27:08 +02:00
|
|
|
function handleBlur(event) {
|
|
|
|
if (event.target.name === 'minute' || event.target.name === 'day') {
|
|
|
|
const validity = event?.target?.validity;
|
|
|
|
if (validity.rangeOverflow || validity.rangeUnderflow) {
|
|
|
|
updateError('add', event.target.name);
|
|
|
|
} else if (error.includes(event.target.name)) {
|
|
|
|
updateError('remove', event.target.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 18:36:56 +01:00
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
|
|
|
updatePublishForm({ releaseTimeEdited: undefined });
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
2022-06-23 13:27:08 +02:00
|
|
|
useEffect(() => {
|
|
|
|
updatePublishForm({ releaseTimeError: error.join(';') });
|
|
|
|
}, [error]);
|
|
|
|
|
2021-05-15 06:10:28 +02:00
|
|
|
return (
|
|
|
|
<div className="form-field-date-picker">
|
2021-06-13 09:12:28 +02:00
|
|
|
<label>{__('Release date')}</label>
|
2022-06-23 13:27:08 +02:00
|
|
|
<div className="form-field-date-picker__controls">
|
2021-05-15 06:10:28 +02:00
|
|
|
{showDatePicker && (
|
|
|
|
<DateTimePicker
|
|
|
|
className="date-picker-input"
|
|
|
|
calendarClassName="form-field-calendar"
|
2022-06-23 13:27:08 +02:00
|
|
|
onBlur={handleBlur}
|
2021-05-15 06:10:28 +02:00
|
|
|
onChange={onDateTimePickerChanged}
|
|
|
|
value={date}
|
|
|
|
format="y-MM-dd h:mm a"
|
|
|
|
disableClock
|
|
|
|
clearIcon={null}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{showEditBtn && (
|
|
|
|
<Button
|
|
|
|
button="link"
|
|
|
|
label={__('Edit')}
|
|
|
|
aria-label={__('Set custom release date')}
|
|
|
|
onClick={() => newDate(NOW)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{showDatePicker && isEdit && releaseTime && (
|
|
|
|
<Button
|
|
|
|
button="link"
|
|
|
|
label={__('Reset')}
|
|
|
|
aria-label={__('Reset to original (previous) publish date')}
|
|
|
|
onClick={() => newDate(RESET_TO_ORIGINAL)}
|
|
|
|
/>
|
|
|
|
)}
|
2021-12-16 22:59:13 +01:00
|
|
|
{showDatePicker && showNowBtn && (
|
2021-05-15 06:10:28 +02:00
|
|
|
<Button
|
|
|
|
button="link"
|
|
|
|
label={__('Now')}
|
|
|
|
aria-label={__('Set to current date and time')}
|
|
|
|
onClick={() => newDate(NOW)}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{showDefaultBtn && (
|
|
|
|
<Button
|
|
|
|
button="link"
|
|
|
|
label={__('Default')}
|
|
|
|
aria-label={__('Remove custom release date')}
|
|
|
|
onClick={() => newDate(DEFAULT)}
|
|
|
|
/>
|
|
|
|
)}
|
2022-06-23 13:27:08 +02:00
|
|
|
{error.length > 0 && (
|
|
|
|
<span className="form-field-date-picker__error">
|
|
|
|
{error.includes(FUTURE_DATE_ERROR) && <span>{__(FUTURE_DATE_ERROR)}</span>}
|
|
|
|
{(!error.includes(FUTURE_DATE_ERROR) || error.length > 1) && <span>{__('Invalid date/time.')}</span>}
|
|
|
|
</span>
|
|
|
|
)}
|
2021-05-15 06:10:28 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default PublishReleaseDate;
|