-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
65 lines (54 loc) · 1.52 KB
/
Copy pathapi.js
File metadata and controls
65 lines (54 loc) · 1.52 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
import moment from 'moment';
import Constants from 'expo-constants';
import uuid from 'uuid';
// Get the url to the local machine run both
// the ios simulator and the json-server
const { manifest } = Expo.Constants;
const api = manifest.packagerOpts.dev
? manifest.debuggerHost.split(':').shift().concat(':3000')
: 'productionurl.com';
const url = `http://${api}/events`;
export function getEvents() {
console.log(url);
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(error => console.error(error));
}
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'),
};
}