-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
341 lines (241 loc) · 9.58 KB
/
script.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Promisifing the XMLHttpRequest so we can use .then and .catch.
let Request = (() => {
//for a GET request
let get = (url) => {
return new Promise((resolve, reject) => {
let request = new XMLHttpRequest();
request.open("GET", url);
request.addEventListener('load', function () {
if (this.status === 200) {
//if the status is 200 just parse everything as JSON
// and return the notes
resolve(JSON.parse(this.responseText))
} else {
reject(this.responseText)
}
});
request.send();
})
};
//for a POST request
let post = (url, data) => {
return new Promise((resolve, reject) => {
let request = new XMLHttpRequest();
request.open("POST", url);
request.setRequestHeader("Content-type", "application/json");
request.addEventListener('load', function () {
if (this.status === 200) {
//if the status is 200 just parse everything as JSON
// and return the notes
resolve(JSON.parse(this.responseText))
} else {
console.log(this)
reject(this.responseText)
}
});
request.send(JSON.stringify(data));
})
};
return {
get, post
}
})();
let UIController = (() => {
let _DOM = {
title : "title",
content : "content",
postBtn : "postBtn",
noteList: "noteList",
footer : "footer",
location: ".location"
};
return {
DOM : _DOM,
getNote : () => {
//get the content from the inputs.
let title = document.getElementById(_DOM.title).value;
let content = document.getElementById(_DOM.content).value;
return {
title, content
}
},
clearNote : () => {
let title = document.getElementById(_DOM.title);
let content = document.getElementById(_DOM.content);
title.value = "";
content.value = "";
title.focus()
},
insertNoteInList: (noteHtml) => {
let noteList = document.getElementById(_DOM.noteList);
noteList.insertAdjacentHTML('afterbegin', noteHtml)
},
setLocation : (location) => {
let locationDOM = document.querySelector(_DOM.location);
let template = '<img src="$flag_url$" width="35" height="20" alt="flag"><p>You are posting from $location$</p>';
let locationHtml = template.replace('$flag_url$', location.flag_url);
locationHtml = locationHtml.replace('$location$', location.location);
locationDOM.innerHTML = locationHtml;
}
}
})();
let Store = (() => {
let _store = {
notes : [],
instance: {},
location: {}
};
function Note(content, title, country) {
this.content = content;
this.title = title;
this.country_code = country;
}
function Location(ip, location, country_code, flag_url) {
this.ip = ip;
this.location = location;
this.country_code = country_code;
this.flag_url = flag_url;
}
Note.prototype.addNote = function () {
_store.notes.push(this);
};
return {
// rehydration functions
rehydrateNotesState : (notes) => {
return _store.notes = notes.map((n) => {
return new Note(n.content, n.title, n.country_code)
})
},
rehydrateInstanceState: (instance) => {
return _store.instance = instance;
},
// CRUD
addNote: (content, title, country) => {
let n = new Note(content, title, country);
n.addNote();
return n;
},
setLocation: (ip, location, country_code, flag_url) => {
return _store.location = new Location(ip, location, country_code, flag_url)
},
getLocation: () => {
return _store.location;
},
//get information from store
getNotes : () => {
return _store.notes;
},
getInstanceDetails: () => {
return _store.instance;
}
}
})();
let AppController = ((store, ui) => {
function init() {
//1. focus the client on the title input
document.getElementById(ui.DOM.title).focus();
//2. Get existing notes from a server
Request.get('http://notes-lb-1614696031.eu-west-1.elb.amazonaws.com/note')
.then(req => {
//rehydrate the notes
rehydrateNotes(req.data);
//rehydrate the instance data
rehydrateInstance(req.instance)
})
.catch(err => {
console.log("error when retrieving data - ", err);
rehydrateNotes([])
});
Request.get('http://notes-lb-1614696031.eu-west-1.elb.amazonaws.com/location')
.then(req => {
let location = store.setLocation(req.ip, req.location, req.country_code, req.flag_url);
ui.setLocation(location);
})
.catch(err => {
console.log("error when retrieving location - ", err);
});
}
//function used to get the state received from the store and
//add the elements inside the app.
function rehydrateNotes(from_server) {
//add notes to state
let state = store.rehydrateNotesState(from_server);
//create container element that will hold all notes
let ul = document.createElement("ul");
ul.id = ui.DOM.noteList;
//run through all notes in array and generate a DOM node.
state.forEach((note) => {
let li = document.createElement('template');
li.innerHTML = generateNoteHtml(note);
ul.appendChild(li.content.firstChild);
});
//find the content and insert the newly created list in DOM list
//right before the footer.
let content = document.querySelector('.content');
content.insertBefore(ul, document.getElementById('footer'));
}
//function to rehydrate the instance information received from server.
function rehydrateInstance(from_server) {
//add notes to state
let state = store.rehydrateInstanceState(from_server);
//add the information to the footer element.
document.getElementById(ui.DOM.footer).innerHTML = generateInstanceHtml(state);
}
//generate the template based on the note object.
function generateNoteHtml(note) {
const template = '<li><h1>$title$</h1><p>$content$</p><div><img src="http://www.geognos.com/api/en/countries/flag/$country$.png" alt=""><p>Posted from Romania | At 20:06 PM 10/12/2017</p></div></li>';
let noteHtml = template.replace('$title$', note.title);
noteHtml = noteHtml.replace('$content$', note.content);
noteHtml = noteHtml.replace('$country$', note.country_code);
return noteHtml;
}
//generate the template based on the instance object.
function generateInstanceHtml(instanceDetails) {
const template = '<p>App version 1.0. Last API call came from the instance <span>$instanceId$</span> found in region <span>$region$</span>, Availability Zone <span>$availabilityZone$</span>, with private IP <span>$privateIp$</span></p>';
let instanceHtml = template.replace('$instanceId$', instanceDetails.instanceId);
instanceHtml = instanceHtml.replace('$region$', instanceDetails.region);
instanceHtml = instanceHtml.replace('$availabilityZone$', instanceDetails.availabilityZone);
instanceHtml = instanceHtml.replace('$privateIp$', instanceDetails.privateIp);
return instanceHtml;
}
//get elements
//TODO refactor to get an element directly
let btn = document.getElementById(ui.DOM.postBtn);
// event listeners functions.
let addNote = () => {
//get note content from UI.
let note = ui.getNote();
//If i don't have the content or title chug an error.
if (note.content && note.title) {
//1. Add note in store
let noteObj = store.addNote(note.content, note.title, store.getLocation().country_code);
//2. add to server
Request.post('http://notes-lb-1614696031.eu-west-1.elb.amazonaws.com/note', {
title : noteObj.title,
content : noteObj.content,
location: store.getLocation()
}).then(req => {
//3. Add note to DOM
ui.insertNoteInList(generateNoteHtml(noteObj));
//4. clear is separate as i might have to retry server call
ui.clearNote();
}).catch(err => {
console.log("error when posting note to server - ", err);
});
//clear is separate as i might have to retry server call
ui.clearNote();
} else {
//TODO work with errors...
console.log("not all fields boys...")
}
};
// add event listeners
btn.addEventListener('click', addNote);
document.addEventListener('keypress', (e) => {
if (e.code === "Enter") {
addNote()
}
});
//initialize
init()
})(Store, UIController);