marination | c842305 | 2021-04-07 23:49:04 +0530 | [diff] [blame] | 1 | $(() => { |
| 2 | class CustomerReviews { |
| 3 | constructor() { |
| 4 | this.bind_button_actions(); |
| 5 | this.start = 0; |
| 6 | this.page_length = 10; |
| 7 | } |
| 8 | |
| 9 | bind_button_actions() { |
| 10 | this.write_review(); |
| 11 | this.view_more(); |
| 12 | } |
| 13 | |
| 14 | write_review() { |
| 15 | //TODO: make dialog popup on stray page |
| 16 | $('.page_content').on('click', '.btn-write-review', (e) => { |
| 17 | // Bind action on write a review button |
| 18 | const $btn = $(e.currentTarget); |
| 19 | |
| 20 | let d = new frappe.ui.Dialog({ |
| 21 | title: __("Write a Review"), |
| 22 | fields: [ |
| 23 | {fieldname: "title", fieldtype: "Data", label: "Headline", reqd: 1}, |
| 24 | {fieldname: "rating", fieldtype: "Rating", label: "Overall Rating", reqd: 1}, |
| 25 | {fieldtype: "Section Break"}, |
| 26 | {fieldname: "comment", fieldtype: "Small Text", label: "Your Review"} |
| 27 | ], |
| 28 | primary_action: function() { |
marination | c842305 | 2021-04-07 23:49:04 +0530 | [diff] [blame] | 29 | let data = d.get_values(); |
| 30 | frappe.call({ |
| 31 | method: "erpnext.e_commerce.doctype.item_review.item_review.add_item_review", |
| 32 | args: { |
| 33 | web_item: $btn.attr('data-web-item'), |
| 34 | title: data.title, |
| 35 | rating: data.rating, |
| 36 | comment: data.comment |
| 37 | }, |
| 38 | freeze: true, |
| 39 | freeze_message: __("Submitting Review ..."), |
| 40 | callback: (r) => { |
marination | 3d5f029 | 2021-04-08 15:25:43 +0530 | [diff] [blame] | 41 | if (!r.exc) { |
marination | c842305 | 2021-04-07 23:49:04 +0530 | [diff] [blame] | 42 | frappe.msgprint({ |
| 43 | message: __("Thank you for submitting your review"), |
| 44 | title: __("Review Submitted"), |
| 45 | indicator: "green" |
| 46 | }); |
| 47 | d.hide(); |
| 48 | location.reload(); |
| 49 | } |
| 50 | } |
| 51 | }); |
| 52 | }, |
| 53 | primary_action_label: __('Submit') |
| 54 | }); |
| 55 | d.show(); |
| 56 | }); |
| 57 | } |
| 58 | |
| 59 | view_more() { |
| 60 | $('.page_content').on('click', '.btn-view-more', (e) => { |
| 61 | // Bind action on view more button |
| 62 | const $btn = $(e.currentTarget); |
| 63 | $btn.prop('disabled', true); |
| 64 | |
| 65 | this.start += this.page_length; |
| 66 | let me = this; |
| 67 | |
| 68 | frappe.call({ |
| 69 | method: "erpnext.e_commerce.doctype.item_review.item_review.get_item_reviews", |
| 70 | args: { |
| 71 | web_item: $btn.attr('data-web-item'), |
| 72 | start: me.start, |
| 73 | end: me.page_length |
| 74 | }, |
| 75 | callback: (result) => { |
marination | 3d5f029 | 2021-04-08 15:25:43 +0530 | [diff] [blame] | 76 | if (result.message) { |
marination | c842305 | 2021-04-07 23:49:04 +0530 | [diff] [blame] | 77 | let res = result.message; |
| 78 | me.get_user_review_html(res.reviews); |
| 79 | |
| 80 | $btn.prop('disabled', false); |
| 81 | if (res.total_reviews <= (me.start + me.page_length)) { |
| 82 | $btn.hide(); |
| 83 | } |
| 84 | |
| 85 | } |
| 86 | } |
marination | 3d5f029 | 2021-04-08 15:25:43 +0530 | [diff] [blame] | 87 | }); |
marination | c842305 | 2021-04-07 23:49:04 +0530 | [diff] [blame] | 88 | }); |
| 89 | |
| 90 | } |
| 91 | |
| 92 | get_user_review_html(reviews) { |
| 93 | let me = this; |
| 94 | let $content = $('.user-reviews'); |
| 95 | |
| 96 | reviews.forEach((review) => { |
| 97 | $content.append(` |
| 98 | <div class="mb-3 review"> |
| 99 | <div style="display: flex;"> |
| 100 | <div class="rating"> |
| 101 | ${me.get_review_stars(review.rating)} |
| 102 | </div> |
| 103 | <p class="ml-4 user-review-title"> |
| 104 | <span>${__(review.review_title)}</span> |
| 105 | </p> |
| 106 | </div> |
| 107 | <div class="review-signature"> |
| 108 | <span class="reviewer">${__(review.customer)}</span> |
| 109 | <span>${__(review.published_on)}</span> |
| 110 | </div> |
| 111 | <div class="product-description mb-4 mt-4"> |
| 112 | <p> |
| 113 | ${__(review.comment)} |
| 114 | </p> |
| 115 | </div> |
| 116 | </div> |
| 117 | `); |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | get_review_stars(rating) { |
| 122 | let stars = ``; |
marination | 3d5f029 | 2021-04-08 15:25:43 +0530 | [diff] [blame] | 123 | for (let i = 1; i < 6; i++) { |
marination | c842305 | 2021-04-07 23:49:04 +0530 | [diff] [blame] | 124 | let fill_class = i <= rating ? 'star-click' : ''; |
| 125 | stars += `<svg class="icon icon-md ${fill_class}"> |
| 126 | <use href="#icon-star"></use> |
| 127 | </svg>`; |
| 128 | } |
| 129 | return stars; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | new CustomerReviews(); |
| 134 | }); |