-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
63 lines (54 loc) · 1.45 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 Constants from 'expo-constants';
import uuid from 'uuid';
const { manifest } = Constants;
const api = manifest.packagerOpts.dev
? manifest.debuggerHost.split(':').shift().concat(':3000')
: 'productionurl.com';
const url = `http://${api}/events`;
const date = (date) => {
return new Date(date);
}
export function getEvents() {
return fetch(url)
.then(response => response.json())
.then(events => events.map(event => ({
...event,
date: new Date(event.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 formatDateTime(dateString) {
const parsed = moment(date(dateString));
if (!parsed.isValid()) dateString;
return parsed.format('H:MM on D MMM YYYY');
}
export function formatDate(dateString) {
const parsed = moment(date(dateString));
if (!parsed.isValid()) dateString;
return parsed.format('D MMM YYYY');
}
export function getCountdownParts(eventDate) {
const duration = moment.duration(moment(date(eventDate)).diff(new Date()));
return {
days: parseInt(duration.as('days')),
hours: duration.get('hours'),
minutes: duration.get('minutes'),
seconds: duration.get('seconds'),
};
}