· 4 years ago · Mar 17, 2021, 02:48 PM
1/*
2 Add code licence et
3*/
4var MAIN = (function(){
5 function doGetAjax(type_str, product_id_str){
6 if(window,G_API_GW_URL_STR === null){
7 showProblem("I have no GET API to call for " + type_str);
8 return;
9 }
10 showWorking();
11 // var product_id_str = $("select").val();
12
13 //use AJAX to get error handlng
14 $.get(window.G_API_GW_URL_STR + "/" + type_str + "?product_id=" + product_id_str, successGetHandler);
15 }
16 function doPostAjax(){
17 if(window,G_API_GW_URL_STR === null){
18 showProblem("I have no POST API to call!");
19 return;
20 }
21 //have to be logge din
22 var token_str_or_null = localStorage.getItem("bearer_str");
23
24 if(token_str_or_null === null){
25 return showProblem("You need to be logged in to create a report");
26 }
27
28 showWorking();
29 //construct bearer token in the headerTODO
30 //use $.ajax to get error handling too
31 $.ajax({
32 url: window.G_API_GW_URL_STR + "/" + "create_report",
33 method: "POST",
34 data: {},
35 headers: {
36 "Authorization": "Bearer " + token_str_or_null
37 },
38 error: handleReportCreationError,
39 success: handleReportCreationSuccess
40 });
41 }
42 function handleReportCreationSuccess(response){
43 console.info(response);
44 //shoudl have message str (ok)
45 if(response.message_str){
46 showProblem(response.message_str);
47 }else{
48 showProblem(JSON.parse(response).message_str);
49 }
50 }
51 function handleReportCreationError(response){
52 console.error(response);
53 showProblem("Something went wrong");
54 }
55 function handleKeypress(kpe){
56 console.log(kpe.which);
57 if(kpe.which === 13){
58 showProblem("Choose either get reviews or get average rating");
59 return false;
60 }
61 }
62 function hideProblem(){
63 $("[data-role='problem_message']")
64 .attr("data-showing", "not_showing")
65 .text("");
66 }
67 function requestReport(){
68 console.log("attempting a report creation");
69 doPostAjax();
70 }
71 function getAverageRating(){
72 //if reulst card for this item is showong already
73 //return;
74
75 var product_id_str = $(this).attr("data-product_id");
76 console.log("AJAX GET average rating for " + product_id_str);
77 doGetAjax("get_av_star_rating", product_id_str);
78 }
79 function getReviews(){
80 var product_id_str = $(this).attr("data-product_id");
81 console.log("AJAX GET reviews for " + product_id_str);
82 doGetAjax("get_reviews", product_id_str);
83 }
84 function setUpHandlers(){
85 $(document).on("keypress", "[data-role='product_name']", handleKeypress);
86 $(document).on("click", "[data-action='request_report']", requestReport);
87 $(document).on("click", "[data-action='get_reviews']", getReviews);
88 $(document).on("click", "[data-action='get_average_rating']", getAverageRating);
89 }
90 function showProblem(problem_str){
91 console.warn(problem_str);
92 $("[data-role='problem_message']")
93 .attr("data-showing", "showing")
94 .text(problem_str);
95 setTimeout(function(){
96 hideProblem();
97 }, 2.2 * 1000);
98 }
99 function showWorking(){
100 $("[data-role='problem_message']")
101 .text("")
102 .attr("data-showing", "not_showing");
103 }
104 function successGetHandler(response){
105 //clear any existing reviews and rating section
106
107
108 if(response.average_star_review_float){
109 showUIForRatings(response.product_id_str, response.average_star_review_float);
110 } else if(response.product_id_str && response.reviews_arr) {
111 showUIForReviews(response.product_id_str, response.reviews_arr);
112 } else if (response.body) { //FOR JAVA that forced a body key
113 var jsonResponse = JSON.parse(response.body);
114 if(jsonResponse.reviews_arr) {
115 showUIForReviewsJava(jsonResponse.product_id_str, jsonResponse.reviews_arr);
116 } else {
117 showUIForRatingsJava(jsonResponse.product_id_str, jsonResponse.average_star_review_float);
118 }
119 }
120 }
121
122 function showUIForReviews(product_id_str, reviews_arr){
123 var $row = $("[data-role='result_card'][data-product_id='" + product_id_str + "']");
124 var html_str = '';
125 html_str += '<div data-role="reviews_wrapper">'
126 html_str += '<p>This product has <span>' + reviews_arr.length.toString() + '</span> reviews</p>';
127 html_str += '<ol>';
128 for(var i_int = 0; i_int < reviews_arr.length; i_int += 1){
129 html_str += '<li>';
130 html_str += '<span data-role="review_headline">';
131 html_str += reviews_arr[i_int].review_headline_str;
132 html_str += '</span>';
133 html_str += '<span>';
134 html_str += reviews_arr[i_int].review_body_str;
135 html_str += '</span>';
136 html_str += '</li>';
137 }
138 html_str += '</ol>';
139 html_str += '</div>';
140 $row.append(html_str);
141 }
142
143 function showUIForReviewsJava(product_id_str, reviews_arr){
144
145 var $row = $("[data-role='result_card'][data-product_id='" + product_id_str + "']");
146
147 try {
148
149 var html_str = '';
150 html_str += '<div data-role="reviews_wrapper">'
151 html_str += '<p>This product has <span>' + reviews_arr.length.toString() + '</span> reviews</p>';
152 html_str += '<ol>';
153 for(var i_int = 0; i_int < reviews_arr.length; i_int += 1){
154 html_str += '<li>';
155 html_str += '<span data-role="review_headline">';
156 html_str += JSON.parse(JSON.parse(reviews_arr[i_int])).review_headline_str;
157 html_str += '</span>';
158 html_str += '<span>';
159 html_str += JSON.parse(JSON.parse(reviews_arr[i_int])).review_body_str;
160 html_str += '</span>';
161 html_str += '</li>';
162 }
163 html_str += '</ol>';
164 html_str += '</div>';
165 $row.append(html_str);
166 } catch(err) {
167 console.log("Error displaying reviews")
168 }
169
170 }
171
172 function showUIForReviews(product_id_str, reviews_arr){
173 var $row = $("[data-role='result_card'][data-product_id='" + product_id_str + "']");
174 var html_str = '';
175 html_str += '<div data-role="reviews_wrapper">'
176 html_str += '<p>This product has <span>' + reviews_arr.length.toString() + '</span> reviews</p>';
177 html_str += '<ol>';
178 for(var i_int = 0; i_int < reviews_arr.length; i_int += 1){
179 html_str += '<li>';
180 html_str += '<span data-role="review_headline">';
181 html_str += reviews_arr[i_int].review_headline_str;
182 html_str += '</span>';
183 html_str += '<span>';
184 html_str += reviews_arr[i_int].review_body_str;
185 html_str += '</span>';
186 html_str += '</li>';
187 }
188 html_str += '</ol>';
189 html_str += '</div>';
190 $row.append(html_str);
191 }
192
193 function showUIForRatings(product_id_str, average_star_review_float){
194 var $row = $("[data-role='result_card'][data-product_id='" + product_id_str + "']");
195
196 var html_str = '';
197 html_str += '<div data-role="rating_wrapper">'
198 html_str += '<p>This product has an average rating of <span>' + average_star_review_float.toString() +'</span></p>';
199 html_str += '</div>';
200 $row.append(html_str);
201 }
202 function showUIForRatingsJava(product_id_str, average_star_review_float){
203 console.warn("once I get a sample payload from Morgan I can fix this");
204 showUIForRatings(product_id_str, average_star_review_float);
205
206 // var $row = $("[data-role='result_card'][data-product_id='" + product_id_str + "']");
207
208 // var html_str = '';
209 // html_str += '<div data-role="rating_wrapper">'
210 // html_str += '<p>This product has an average rating of <span>' + average_star_review_float.toString() +'</span></p>';
211 // html_str += '</div>';
212 // $row.append(html_str);
213 }
214
215 function successPostHandler(response){
216 alert(response);
217 }
218 function showHostedUILink(){
219 $("[data-role='request_report_wrapper'] > a")
220 .attr("href", G_COGNITO_HOSTED_URL_STR || "javascript:void(0);");
221
222 }
223 (function init(){
224 showHostedUILink();
225 setUpHandlers();
226 })();
227})();