Skip to content

Commit d8788f0

Browse files
marvelmflovilmart
authored andcommitted
Support push time, expiration time, and expiration interval (#773)
* Enable scheduled push fields if feature is enabled on the server * Revert changes to package.json * Add expiration_time and push_time to push payload * Separate test into hours and days * Fix imports * Add payload * Use expiration_interval and expiration_time * Convert to interval if the the expiration_time is in local time * parse-server supports audiences
1 parent cf6ed6f commit d8788f0

File tree

3 files changed

+431
-9
lines changed

3 files changed

+431
-9
lines changed

src/dashboard/Push/PushNew.react.js

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ import Toggle from 'components/Toggle/Toggle.react';
3737
import Toolbar from 'components/Toolbar/Toolbar.react';
3838
import { Directions } from 'lib/Constants';
3939
import { Promise } from 'parse';
40+
import { extractExpiration, extractPushTime } from 'lib/extractTime';
4041

4142
const PARSE_SERVER_SUPPORTS_AB_TESTING = false;
42-
const PARSE_SERVER_SUPPORTS_SCHEDULE_PUSH = false;
4343

4444
let formatErrorMessage = (emptyInputMessages, key) => {
4545
let boldMessages = emptyInputMessages.map((message) => {
@@ -202,10 +202,14 @@ export default class PushNew extends DashboardView {
202202
payload.badge = "Increment";
203203
}
204204

205+
const push_time = extractPushTime(changes);
205206
let body = {
207+
data: payload,
206208
where: changes.target || new Parse.Query(Parse.Installation),
207-
data: payload
208-
}
209+
push_time,
210+
};
211+
Object.assign(body, extractExpiration(changes));
212+
209213
let audience_id = changes.audience_id;
210214
// Only set the audience ID if it is a saved audience.
211215
if (audience_id != PushConstants.NEW_SEGMENT_ID && audience_id != "everyone") {
@@ -631,7 +635,7 @@ export default class PushNew extends DashboardView {
631635
legend='Choose your recipients.'
632636
description='Send to everyone, or use an audience to target the right users.'>
633637
<PushAudiencesData
634-
loaded={true /* Parse Server doesn't support push audiences yet. once it does, pass: this.state.pushAudiencesFetched */}
638+
loaded={this.state.pushAudiencesFetched}
635639
schema={schema}
636640
pushAudiencesStore={this.props.pushaudiences}
637641
current={fields.audience_id}
@@ -677,18 +681,21 @@ export default class PushNew extends DashboardView {
677681
{this.renderExperimentContent(fields, setField)}
678682
</Fieldset> : null;
679683

680-
const timeFieldsLegend = PARSE_SERVER_SUPPORTS_SCHEDULE_PUSH ?
684+
const {push} = this.context.currentApp.serverInfo.features;
685+
const hasScheduledPushSupport = push && push.scheduledPush;
686+
687+
const timeFieldsLegend = hasScheduledPushSupport ?
681688
'Choose a delivery time' :
682-
'Choose exiry';
689+
'Choose expiry';
683690

684-
const timeFieldsDescription = PARSE_SERVER_SUPPORTS_SCHEDULE_PUSH ?
691+
const timeFieldsDescription = hasScheduledPushSupport ?
685692
'We can send the campaign immediately, or any time in the next 2 weeks.' :
686693
"If your push hasn't been send by this time, it won't get sent.";
687694

688-
const deliveryTimeFields = PARSE_SERVER_SUPPORTS_SCHEDULE_PUSH ? <Fieldset
695+
const deliveryTimeFields = hasScheduledPushSupport ? <Fieldset
689696
legend={timeFieldsLegend}
690697
description={timeFieldsDescription}>
691-
{PARSE_SERVER_SUPPORTS_SCHEDULE_PUSH ? this.renderDeliveryContent(fields, setField) : null}
698+
{hasScheduledPushSupport ? this.renderDeliveryContent(fields, setField) : null}
692699
<Field
693700
label={<Label text='Should this notification expire?' />}
694701
input={<Toggle value={fields.push_expires} onChange={setField.bind(null, 'push_expires')} />} />

src/lib/extractTime.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
export function extractPushTime(changes) {
2+
const {
3+
local_time: isLocalTime,
4+
push_time_type,
5+
push_time,
6+
push_time_iso,
7+
} = changes;
8+
9+
if (push_time_type === 'time') {
10+
if (isLocalTime) {
11+
return push_time;
12+
}
13+
return push_time_iso;
14+
}
15+
}
16+
17+
export function extractExpiration(changes) {
18+
const {
19+
local_time: isLocalTime,
20+
push_time,
21+
push_expires,
22+
expiration_time_type,
23+
expiration_interval_unit,
24+
expiration_interval_num,
25+
expiration_time
26+
} = changes;
27+
28+
if (push_expires) {
29+
if (expiration_time_type === 'interval') {
30+
let time = parseInt(expiration_interval_num, 10);
31+
if (expiration_interval_unit === 'hours') {
32+
time *= 60 * 60;
33+
} else if (expiration_interval_unit === 'days') {
34+
time *= 60 * 60 * 24;
35+
}
36+
37+
return { expiration_interval: time };
38+
} else if (expiration_time_type === 'time') {
39+
if (isLocalTime) {
40+
const pushTime = new Date(push_time);
41+
const expirationTime = new Date(expiration_time);
42+
const diffSeconds = Math.floor((expirationTime - pushTime) / 1000);
43+
return { expiration_interval: diffSeconds };
44+
}
45+
46+
return { expiration_time };
47+
}
48+
}
49+
50+
return {};
51+
}

0 commit comments

Comments
 (0)