-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
289 lines (247 loc) · 7.83 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
// Config Section
var config = {
PRODUCT_URL: 'https://product-list-js.herokuapp.com/products',
REEVIEW_URL: 'https://product-list-js.herokuapp.com/review/',
MAX_STARS: 5,
MAX_SHOW_REVIEW: 10,
MAX_CHAR_PER_LINE: 100,
}
var inMemory = {
SELECTED_PRODUCT: 1,
products: [],
selectedStar: 0,
reviewDiscription: '',
reviewTitle: '',
reviews: [],
noReviews: config.MAX_SHOW_REVIEW,
};
// Request Section
var request = (function () {
var xhr = new XMLHttpRequest();
function getProdRequest() {
if (xhr.readyState == 4 && xhr.status == 200) {
inMemory.products = JSON.parse(xhr.responseText);
renderPage(inMemory.products);
}
};
function getProdreviews() {
if (xhr.readyState == 4 && xhr.status == 200) {
inMemory.reviews = JSON.parse(xhr.responseText);
mergeReview();
reviewItems();
}
};
return {
getProducts: function () {
xhr.open('GET', config.PRODUCT_URL, true);
xhr.send();
xhr.onreadystatechange = getProdRequest;
},
getReviews: function (productId) {
xhr.open('GET', config.REEVIEW_URL + productId, true);
xhr.send();
xhr.onreadystatechange = getProdreviews;
}
}
})();
// Main Section
function onLoad() {
genarateProductTiles();
renderReviewStars();
}
function renderPage(products, id) {
if (products && products.length) {
id = id || products[0].id;
genarateTile(products);
genarateReviewSection(id)
} else {
//show message
}
}
function genarateTile(products) {
var tiles = '';
for (var index = 0; index < products.length; index++) {
tiles += productTile(products[index], index);
};
document.getElementById("tiles").innerHTML = tiles;
};
function getProductstar(rating) {
return '<span class="product_rating">' +
'<div class="rating_star">' + rating.average + ' ★ </div>' +
'</span>';
}
function getProductReviewInfo(rating) {
return '<span class="prd_rating_review">' +
'<span>' + rating.ratingCount + ' Ratings & ' + rating.reviewCount + ' Reviews </span>' +
'</span>';
}
function getStarName() {
var rating = getRatingText(inMemory.selectedStar) || '';
document.getElementById('startText').innerHTML = '<span class="' + rating.toLowerCase() + '">' + rating + ' </span>';
return 'defaultStar ' + rating.toLowerCase();
}
function getRatingText(selectedStar) {
if (selectedStar < 3) {
return 'Average'
}
if (selectedStar >= 3 && selectedStar < 4) {
return 'Good';
}
if (selectedStar >= 4) {
return 'Excellent';
}
}
function stars(index, selected) {
var cssClass = selected ? getStarName(index) : 'defaultStar';
return '<span data-star=' + index + ' class="' + cssClass + '">★</span>';
}
function renderReviewStars() {
var startList = '';
for (let index = 1; index <= config.MAX_STARS; index++) {
startList += stars(index, index <= inMemory.selectedStar);
}
var starsElt = '<div class="starts" onclick="starSelection()">' +
startList +
'</div>';
document.getElementById("star").innerHTML = starsElt;
}
function productTile(product, index) {
var rating = product.rating;
var reviews = getProductReviewInfo(rating);
var star = getProductstar(rating);
var productTileClass = inMemory.SELECTED_PRODUCT === product.id ? "product_tile selected" : "product_tile";
return '<div onclick="productSelection(' + product.id + ')"class="' + productTileClass + '">' +
'<img class="product_image" src="' + product.productImage + '"/>' +
'<div class="productInfo">' +
'<strong>' + product.title + '</strong>' +
'<div class="ratingWrapper">' +
star +
reviews +
'</div>' +
'<div class="product_price"> र ' + product.pricing.value + '</div>' +
'</div>' +
'</div>';
};
function getReviewItem(item) {
var rating = {
average: item.rating
}
var star = getProductstar(rating);
var textElt = reviewDiscription(item.text);
return '<div class="ratingWrapper">' +
star +
'<span class="prd_rating_review">' +
'<span>' + item.title + '</span>' +
'</span>' +
'<ul class="reviewList">' +
textElt +
'</ul>' +
'<span class="reviewCreator">' +
'<span> <pre class="clr">' + item.userName + ' ' + formatDate(item.created) + '</pre></span> ' +
'</span>'+
'<hr class="hr">' +
'</div>';
}
function reviewDiscription(text) {
var formatedtext = (text || '').split('.');
var row = '';
for (let index = 0; index < formatedtext.length; index++) {
row += '<li>' + formatedtext[index] + '</li>';
}
return row;
}
function reviewItems() {
var tiles = '';
var reviews = inMemory.reviews;
// limit max number of items to show MAX_SHOW_REVIEW
var max = reviews.length;
for (var index = 0; index < max; index++) {
tiles = getReviewItem(reviews[index]) + tiles;
};
document.getElementById("col").innerHTML = tiles;
}
function genarateProductTiles() {
request.getProducts();
}
function genarateReviewSection(productId) {
request.getReviews(productId);
}
function productSelection(id) {
inMemory.SELECTED_PRODUCT = id;
mergeReview();
renderReviewStars();
renderPage(inMemory.products, id);
document.getElementById('error').innerHTML =''
}
function starSelection() {
inMemory.selectedStar = parseInt(event.target.getAttribute('data-star'));
setlocalStorage(inMemory.SELECTED_PRODUCT, {
star: inMemory.selectedStar
});
renderReviewStars();
}
function formatDate(date) {
var newDate = new Date(date);
var monthNames = [
"Jan", "Feb", "Mar",
"Apr", "May", "Jun", "Jul",
"Aug", "Sep", "Oct",
"Nov", "Dec"
];
var day = newDate.getDate();
var monthIndex = newDate.getMonth();
var year = newDate.getFullYear();
return day + ' ' + monthNames[monthIndex] + ', ' + year;
}
function setlocalStorage(key, value) {
if (typeof (Storage) !== "undefined") {
var oldData = getFromLocalStorage(key);
var newValue = {
...oldData,
...value
};
localStorage.setItem(key, JSON.stringify(newValue));
} else {
//Sorry, your browser does not support localStorage
}
}
function getFromLocalStorage(key) {
if (typeof (Storage) !== "undefined") {
return JSON.parse(localStorage.getItem(key));
} else {
//Sorry, your browser does not support localStorage
}
}
function mergeReview() {
var localData = getFromLocalStorage(inMemory.SELECTED_PRODUCT);
if (localData) {
var newReview = {
created: new Date(),
id: "5b1a8a5e14bad453a7bc8430",
rating: parseInt(localData.star),
text: localData.reviewDiscription,
title: localData.reviewTitle || '',
userName: "Arun",
}
inMemory.reviews.push(newReview);
}
}
function saveReviews() {
document.getElementById('error').innerHTML= '';
var reviewDiscription = (document.getElementById('discriptionData').value || '');
if(!reviewDiscription.length){
document.getElementById('error').innerHTML = '<span> Review discription is required </span>';
return;
}
reviewDiscription = reviewDiscription.match(/.{1,100}/g).join('.');
var reviewTitle = document.getElementById('titleData').value;
document.getElementById('discriptionData').value = ''
document.getElementById('titleData').value = '';
var reviewDetails = {
reviewDiscription,
reviewTitle
};
setlocalStorage(inMemory.SELECTED_PRODUCT, reviewDetails);
mergeReview();
reviewItems();
}