blob: e13ded6b489f87cc3dac59d3f56b60a46de41df7 [file] [log] [blame]
marinationc8423052021-04-07 23:49:04 +05301$(() => {
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() {
marinationc8423052021-04-07 23:49:04 +053029 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) => {
marination3d5f0292021-04-08 15:25:43 +053041 if (!r.exc) {
marinationc8423052021-04-07 23:49:04 +053042 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) => {
marination3d5f0292021-04-08 15:25:43 +053076 if (result.message) {
marinationc8423052021-04-07 23:49:04 +053077 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 }
marination3d5f0292021-04-08 15:25:43 +053087 });
marinationc8423052021-04-07 23:49:04 +053088 });
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">
marination6b2b9dc2021-08-25 13:09:35 +053099 <div class="d-flex">
100 <p class="mr-4 user-review-title">
101 <span>${__(review.review_title)}</span>
102 </p>
marinationc8423052021-04-07 23:49:04 +0530103 <div class="rating">
104 ${me.get_review_stars(review.rating)}
105 </div>
marinationc8423052021-04-07 23:49:04 +0530106 </div>
marination6b2b9dc2021-08-25 13:09:35 +0530107
108 <div class="product-description mb-4">
marinationc8423052021-04-07 23:49:04 +0530109 <p>
110 ${__(review.comment)}
111 </p>
112 </div>
marination6b2b9dc2021-08-25 13:09:35 +0530113 <div class="review-signature mb-2">
114 <span class="reviewer">${__(review.customer)}</span>
115 <span class="indicator grey" style="--text-on-gray: var(--gray-300);"></span>
116 <span class="reviewer">${__(review.published_on)}</span>
117 </div>
marinationc8423052021-04-07 23:49:04 +0530118 </div>
119 `);
120 });
121 }
122
123 get_review_stars(rating) {
124 let stars = ``;
marination3d5f0292021-04-08 15:25:43 +0530125 for (let i = 1; i < 6; i++) {
marinationc8423052021-04-07 23:49:04 +0530126 let fill_class = i <= rating ? 'star-click' : '';
marination6b2b9dc2021-08-25 13:09:35 +0530127 stars += `
128 <svg class="icon icon-sm ${fill_class}">
marinationc8423052021-04-07 23:49:04 +0530129 <use href="#icon-star"></use>
marination6b2b9dc2021-08-25 13:09:35 +0530130 </svg>
131 `;
marinationc8423052021-04-07 23:49:04 +0530132 }
133 return stars;
134 }
135 }
136
137 new CustomerReviews();
138});