lbry-desktop/ui/component/publishReleaseDate/view.jsx
infinite-persistence 698bd5eef1
Continuation of "Add release date field #5514" (#6049)
* Release date -- initial attempt (squashed and rebased)

- Use white color for calendar buttons in release date input
- Update icon color for release date input
- Allow to set time in release date and display it under additional options
- Add release date field

* Upgrade to latest react-datetime-picker.

I believe this also picks up the y18n security fix.

* Handle dark theme and general style fixes.

* [+redux] Change how release_time is edited.

- `releaseTime` is now a number instead of a string, matching `release_time`. It was getting confusing what the variable units were.

- `releaseTime` will always match `release_time` for an edit. It will be used in the GUI to reset just the date to the original, instead of having to reset the entire form.

- `releaseTimeEdited` will be used by `updatePublishForm` in the GUI to represent the desired new release time. Set to `undefined` if we don't want to change the date.

* Add 'Reset|Now|Default' buttons instead of overloading the date-picker's "X" button.

Before this, the "X" button resets to the original (previous) publish date for Edits, and resets to current time for New Claims. This is very confusing, so added explicit text-based buttons -- hopefully this is more intuitive.

* bump redux to master

Co-authored-by: Franco Montenegro <franco.montenegro.ruke@gmail.com>
Co-authored-by: Thomas Zarebczan <thomas.zarebczan@gmail.com>
2021-05-15 00:10:28 -04:00

132 lines
3.6 KiB
JavaScript

// @flow
import React from 'react';
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';
type Props = {
releaseTime: ?number,
releaseTimeEdited: ?number,
updatePublishForm: ({}) => void,
};
const PublishReleaseDate = (props: Props) => {
const { releaseTime, releaseTimeEdited, updatePublishForm } = props;
const maxDate = new Date();
const [date, setDate] = React.useState(releaseTime ? linuxTimestampToDate(releaseTime) : new Date());
const isNew = releaseTime === undefined;
const isEdit = !isNew;
const showEditBtn = isNew && releaseTimeEdited === undefined;
const showDefaultBtn = isNew && releaseTimeEdited !== undefined;
const showDatePicker = isEdit || releaseTimeEdited !== undefined;
const onDateTimePickerChanged = (value) => {
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) {
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;
}
}
return (
<div className="form-field-date-picker">
<label>Release date</label>
<div className="controls">
{showDatePicker && (
<DateTimePicker
className="date-picker-input"
calendarClassName="form-field-calendar"
onChange={onDateTimePickerChanged}
value={date}
maxDate={maxDate}
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)}
/>
)}
{showDatePicker && (
<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)}
/>
)}
</div>
</div>
);
};
export default PublishReleaseDate;