1- import { parseRequestUrl , showLoading , hideLoading } from '../utils' ;
2- import { getProduct } from '../api' ;
1+ import {
2+ parseRequestUrl ,
3+ showLoading ,
4+ hideLoading ,
5+ showMessage ,
6+ rerender ,
7+ } from '../utils' ;
8+ import { createReview , getProduct } from '../api' ;
39import Rating from '../components/Rating' ;
10+ import { getUserInfo } from '../localStorage' ;
411
512const ProductScreen = {
613 after_render : ( ) => {
714 const request = parseRequestUrl ( ) ;
815 document . getElementById ( 'add-button' ) . addEventListener ( 'click' , ( ) => {
916 document . location . hash = `/cart/${ request . id } ` ;
1017 } ) ;
18+
19+ if ( document . getElementById ( 'review-form' ) ) {
20+ document
21+ . getElementById ( 'review-form' )
22+ . addEventListener ( 'submit' , async ( e ) => {
23+ e . preventDefault ( ) ;
24+ showLoading ( ) ;
25+ const data = await createReview ( request . id , {
26+ comment : document . getElementById ( 'comment' ) . value ,
27+ rating : document . getElementById ( 'rating' ) . value ,
28+ } ) ;
29+ hideLoading ( ) ;
30+ if ( data . error ) {
31+ showMessage ( data . error ) ;
32+ } else {
33+ showMessage ( 'Review Added Successfully' , ( ) => {
34+ rerender ( ProductScreen ) ;
35+ } ) ;
36+ }
37+ } ) ;
38+ }
1139 } ,
1240 render : async ( ) => {
1341 const request = parseRequestUrl ( ) ;
@@ -17,6 +45,7 @@ const ProductScreen = {
1745 return `<div>${ product . error } </div>` ;
1846 }
1947 hideLoading ( ) ;
48+ const userInfo = getUserInfo ( ) ;
2049 return `
2150 <div class="content">
2251 <div class="back-to-result">
@@ -66,6 +95,68 @@ const ProductScreen = {
6695 </ul>
6796 </div>
6897 </div>
98+ <div class="content">
99+ <h2>Reviews</h2>
100+ ${ product . reviews . length === 0 ? `<div>There is no review.</div>` : '' }
101+ <ul class="review">
102+ ${ product . reviews
103+ . map (
104+ ( review ) =>
105+ `<li>
106+ <div><b>${ review . name } </b></div>
107+ <div class="rating-container">
108+ ${ Rating . render ( {
109+ value : review . rating ,
110+ } ) }
111+ <div>
112+ ${ review . createdAt . substring ( 0 , 10 ) }
113+ </div>
114+ </div>
115+ <div>
116+ ${ review . comment }
117+ </div>
118+ </li>`
119+ )
120+ . join ( '\n' ) }
121+
122+ <li>
123+
124+ ${
125+ userInfo . name
126+ ? `
127+ <div class="form-container">
128+ <form id="review-form">
129+ <ul class="form-items">
130+ <li> <h3>Write a customer reviews</h3></li>
131+ <li>
132+ <label for="rating">Rating</label>
133+ <select required name="rating" id="rating">
134+ <option value="">Select</option>
135+ <option value="1">1 = Poor</option>
136+ <option value="2">2 = Fair</option>
137+ <option value="3">3 = Good</option>
138+ <option value="4">4 = Very Good</option>
139+ <option value="5">5 = Excellent</option>
140+ </select>
141+ </li>
142+ <li>
143+ <label for="comment">Comment</label>
144+ <textarea required name="comment" id="comment" ></textarea>
145+ </li>
146+ <li>
147+ <button type="submit" class="primary">Submit</button>
148+ </li>
149+ </ul>
150+ </form>
151+ </div>`
152+ : ` <div>
153+ Please <a href="/#/signin">Signin</a> to write a review.
154+ </div>`
155+ }
156+ </li>
157+ </ul>
158+
159+ </div>
69160 </div>` ;
70161 } ,
71162} ;
0 commit comments