forked from superdesk/superdesk-client-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublishing-date-options.tsx
More file actions
206 lines (182 loc) · 7.33 KB
/
Copy pathpublishing-date-options.tsx
File metadata and controls
206 lines (182 loc) · 7.33 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import React from 'react';
import {IArticle} from 'superdesk-api';
import {gettext, toIsoStringWithoutTimezoneOffset} from 'core/utils';
import {appConfig} from 'appConfig';
import {DateTimePicker, ToggleBox} from 'superdesk-ui-framework/react';
import {TimeZonePicker} from 'core/ui/components/time-zone-picker';
import {generatePatch} from 'core/patch';
import {sdApi} from 'api';
import {isValid} from 'date-fns';
import {TZDate} from '@sourcefabric/date-fns-tz';
import {getLocaleForDatePicker} from 'core/helpers/ui-framework';
export interface IPublishingDateOptions {
embargo: Date | null;
publishSchedule: Date | null;
timeZone: string | null;
}
export function ignoreTimezone(
/**
* Server adds +0000 to embargo/publish_schedule no matter what timezone it is,
* so ignore that and treat it as local time for editing to avoid further conversion.
*/
date: string,
): TZDate {
return new TZDate(date.replace('+0000', ''));
}
export function getInitialPublishingDateOptions(items: Array<IArticle>): IPublishingDateOptions {
const timeZone = items.length === 1 ? items[0].schedule_settings?.time_zone ?? null : null;
return {
embargo: (() => {
if (items.length != 1) {
return null;
}
const embargo = items[0]?.embargo;
if (embargo == null) {
return null;
}
return ignoreTimezone(embargo);
})(),
publishSchedule: (() => {
if (items.length != 1) {
return null;
}
const publishSchedule = items[0]?.publish_schedule;
if (publishSchedule == null) {
return null;
}
return ignoreTimezone(publishSchedule);
})(),
timeZone: timeZone,
};
}
/**
* It's tricky with timezones here.
* UI widget to pick datetime doesn't support timezones,
* thus before displaying in the picker - we convert it to users' local time.
*
* In order to generate the patch - we need to convert it back to selected timezone.
*/
export function getPublishingDatePatch(item: IArticle, options: IPublishingDateOptions): Partial<IArticle> {
const {
embargo,
publishSchedule,
timeZone,
} = options;
const currentOptions: Partial<IArticle> = {
embargo: item.embargo,
publish_schedule: item.publish_schedule,
schedule_settings: item.schedule_settings,
};
const nextOptions: Partial<IArticle> = {
embargo: embargo == null
? null
: toIsoStringWithoutTimezoneOffset(new TZDate(embargo)),
publish_schedule: publishSchedule == null
? null
: toIsoStringWithoutTimezoneOffset(new TZDate(publishSchedule)),
schedule_settings: {
...item.schedule_settings,
time_zone: timeZone,
},
};
return generatePatch(currentOptions, nextOptions, {undefinedEqNull: true});
}
interface IProps {
items: Array<IArticle>;
value: IPublishingDateOptions;
onChange(value: IPublishingDateOptions): void;
allowSettingEmbargo: boolean;
allowSettingPublishSchedule?: boolean;
}
export class PublishingDateOptions extends React.PureComponent<IProps> {
render() {
const {items} = this.props;
const {
embargo,
publishSchedule,
timeZone,
} = this.props.value;
const canSetEmbargo = this.props.allowSettingEmbargo && sdApi.user.hasPrivilege('embargo');
const canSetPublishSchedule = this.props.allowSettingPublishSchedule;
const allowSeconds = appConfig.authoring?.panels?.publish?.allowSeconds ?? false;
if (items.length !== 1) {
return null;
}
return (
<div>
{canSetEmbargo && (
<ToggleBox variant="simple" title={gettext('Embargo')} initiallyOpen>
<DateTimePicker
value={embargo}
valueType="date"
allowSeconds={allowSeconds}
locale={{
type: 'full',
payload: getLocaleForDatePicker(),
}}
dateFormat={appConfig.view.dateformat}
onChange={(val) => {
const isValidDate = isValid(val);
const isDateBeingReset = val === null;
if (isValidDate || isDateBeingReset) {
this.props.onChange({
embargo: val,
timeZone: timeZone ?? appConfig.default_timezone,
publishSchedule: publishSchedule,
});
}
}}
data-test-id="embargo"
/>
</ToggleBox>
)}
{canSetPublishSchedule && (
<ToggleBox variant="simple" title={gettext('Publish schedule')} initiallyOpen>
<DateTimePicker
value={publishSchedule}
valueType="date"
allowSeconds={allowSeconds}
locale={{
type: 'full',
payload: getLocaleForDatePicker(),
}}
dateFormat={appConfig.view.dateformat}
onChange={(val) => {
const isValidDate = isValid(val);
const isDateBeingReset = val === null;
if (isValidDate || isDateBeingReset) {
this.props.onChange({
publishSchedule: val,
timeZone: timeZone ?? appConfig.default_timezone,
embargo: embargo,
});
}
}}
data-test-id="publish-schedule"
/>
</ToggleBox>
)}
{(embargo != null || publishSchedule != null) && (
<ToggleBox variant="simple" title={gettext('Time zone')} initiallyOpen>
<TimeZonePicker
value={timeZone}
onChange={(val) => {
this.props.onChange({
...this.props.value,
timeZone: val,
});
}}
/>
{
timeZone == null && (
<div style={{paddingBlockStart: 5}}>
{gettext('If not set, the UTC+0 time zone is assumed.')}
</div>
)
}
</ToggleBox>
)}
</div>
);
}
}