-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
116 lines (97 loc) · 2.6 KB
/
scripts.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
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
var dayjs = require('dayjs');
import './css/base.scss';
import Traveler from './Traveler';
import Trip from './Trip';
import Destination from './Destination';
import {
showFetchErrorMessage,
showPostErrorMessage,
dropdown,
tripTravelers,
tripDuration,
calenderDate,
submitTrip,
backPage,
renderCards,
renderCardBack,
showCards,
currentTraveler,
userTrips,
userDestinations,
} from './domUpdates';
import {
allData,
postTrip
} from './apiCalls';
// Gloabal Varibles
export let destinationData;
export let trips;
export let destinations;
export let traveler;
export let travelerData;
export let tripData;
export let newTrip;
//Functions
const invokeFetch = () => {
allData
.then(response => parseValues(response))
.catch(err => showFetchErrorMessage())
}
const parseValues = (data) => {
destinationData = data[0].destinations;
tripData = data[1].trips;
travelerData = data[2].travelers;
instantiation(destinationData, tripData, travelerData)
}
export const instantiation = (destinationInfo, tripInfo, travelerInfo) => {
traveler = new Traveler(travelerInfo)
trips = new Trip(tripInfo);
destinations = new Destination(destinationInfo)
}
const bookTrip = (event) => {
event.preventDefault();
event.stopImmediatePropagation();
newTrip = {
id: Date.now(),
userID: currentTraveler.id,
destinationID: destinations.findId(dropdown.value),
travelers: Number(tripTravelers.value),
date: dayjs(calenderDate.value).format('YYYY/MM/DD'),
duration: Number(tripDuration.value),
status: 'pending',
suggestedActivities: []
}
renderCardBack(newTrip)
event.target.reset()
}
const publishTrip = (event) => {
event.preventDefault()
if (event.target.className === "button button-back book") {
validatePost(newTrip)
} else if (event.target.className === "button button-back no-thanks") {
let userTrips = trips.findTrips(currentTraveler.id)
renderCards(userTrips)
showCards()
}
}
const validatePost = (obj) => {
postTrip(obj)
.then(response => {
return fetch("http://localhost:3001/api/v1/trips")
})
.then(response => response.json())
.then(data => {
trips = new Trip(data.trips)
let userTrips = trips.findTrips(currentTraveler.id)
let userDestinations = destinations.findByTrips(userTrips)
renderCards(userTrips)
showCards();
return trips
})
.catch(err => showPostErrorMessage())
return
}
// Event Listeners
submitTrip.addEventListener('submit', () => bookTrip(event))
backPage.addEventListener('click', () => publishTrip(event))
window.addEventListener('load', invokeFetch)