-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
63 lines (51 loc) · 1.55 KB
/
api.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
import moment from 'moment';
import * as Expo from 'expo';
import uuid from 'uuid-random';
import Constants from 'expo-constants';
const api = Constants.manifest.packagerOpts.dev
? Constants.manifest.debuggerHost.split(':').shift().concat(':3000')
: 'productionurl.com'
const url = `http://${api}/events`;
export function getEvents() {
return fetch(url)
.then(response => response.json())
.then(events => events.map(e => ({ ...e, date: new Date(e.date)})));
}
export function saveEvent({ title, date }) {
return fetch(url, {
method: 'POST',
body: JSON.stringify({
title,
date,
id: uuid(),
}),
headers: new Headers({
'Content-Type': 'application/json',
})
})
.then(res => res.json())
.catch(err => console.error(err));
}
export function formatDate(dateString) {
const parsed = moment(new Date(dateString));
if (!parsed.isValid()) {
return dateString
}
return parsed.format('D MMM YYYY');
}
export function formatDateTime(dateString) {
const parsed = moment(new Date(dateString));
if (!parsed.isValid()) {
return dateString
}
return parsed.format('H A on D MMM YYYY');
}
export function getCountDownParts(eventDate) {
const duration = moment.duration(moment(new Date(eventDate)).diff(new Date()));
return {
days: parseInt(duration.as('days')),
hours: duration.get('hours'),
minutes: duration.get('minutes'),
seconds: duration.get('seconds'),
};
}