import SubPage from './base_page'; import { get_rating_html } from '../helpers'; erpnext.hub.Item = class Item extends SubPage { make_wrapper() { super.make_wrapper(); this.setup_events(); } refresh() { this.show_skeleton(); this.hub_item_code = frappe.get_route()[2]; this.own_item = false; this.get_item(this.hub_item_code) .then(item => { this.own_item = item.hub_seller === hub.settings.company_email; this.item = item; this.render(item); }); } show_skeleton() { const skeleton = `

Name

Details

Ratings


Desc

Desc

`; this.$wrapper.html(skeleton); } setup_events() { this.$wrapper.on('click', '.btn-contact-seller', () => { const d = new frappe.ui.Dialog({ title: __('Send a message'), fields: [ { fieldname: 'to', fieldtype: 'Read Only', label: __('To'), default: this.item.company }, { fieldtype: 'Text', fieldname: 'message', label: __('Message') } ] }); d.show(); }); } get_item(hub_item_code) { return hub.call('get_item_details', { hub_item_code }); } render(item) { const title = item.item_name || item.name; const seller = item.company; const who = __('Posted By {0}', [seller]); const when = comment_when(item.creation); const city = item.city ? item.city + ', ' : ''; const country = item.country ? item.country : ''; const where = `${city}${country}`; const dot_spacer = ''; const description = item.description || ''; const rating_html = get_rating_html(item.average_rating); const rating_count = item.no_of_ratings > 0 ? `${item.no_of_ratings} reviews` : __('No reviews yet'); let edit_buttons_html = ''; if(this.own_item) { edit_buttons_html = `
`; } const html = `

${title}

${where}${dot_spacer}${when}

${rating_html} (${rating_count})


${description ? `${__('Description')}

${description}

` : `

${__('No description')}

` }

${edit_buttons_html}
Seller Information
`; this.$wrapper.html(html); if(this.own_item) { this.bind_edit_buttons(); } this.make_review_area(); this.get_reviews() .then(reviews => { this.reviews = reviews; this.render_reviews(reviews); }); } bind_edit_buttons() { this.edit_dialog = new frappe.ui.Dialog({ title: "Edit Your Product", fields: [] }); this.$wrapper.find('.edit-item').on('click', this.on_edit.bind(this)); this.$wrapper.find('.unpublish').on('click', this.on_unpublish.bind(this)); } on_edit() { this.edit_dialog.show(); } on_unpublish() { if(!this.unpublish_dialog) { this.unpublish_dialog = new frappe.ui.Dialog({ title: "Edit Your Product", fields: [] }); } this.unpublish_dialog.show(); } make_review_area() { this.comment_area = new frappe.ui.ReviewArea({ parent: this.$wrapper.find('.timeline-head').empty(), mentions: [], on_submit: (values) => { values.user = frappe.session.user; values.username = frappe.session.user_fullname; hub.call('add_item_review', { hub_item_code: this.hub_item_code, review: JSON.stringify(values) }) .then(review => { this.reviews = this.reviews || []; this.reviews.push(review); this.render_reviews(this.reviews); this.comment_area.reset(); }); } }); } get_reviews() { return hub.call('get_item_reviews', { hub_item_code: this.hub_item_code }).catch(() => {}); } render_reviews(reviews=[]) { this.$wrapper.find('.timeline-items').empty(); reviews.sort((a, b) => { if (a.modified > b.modified) { return -1; } if (a.modified < b.modified) { return 1; } return 0; }); reviews.forEach(review => this.render_review(review)); } render_review(review) { let username = review.username || review.user || __("Anonymous"); let image_html = review.user_image ? `
` : `
${frappe.get_abbr(username)}
` let edit_html = review.own ? `
${'data.edit'}
` : ''; let rating_html = get_rating_html(review.rating); const $timeline_items = this.$wrapper.find('.timeline-items'); $(this.get_timeline_item(review, image_html, edit_html, rating_html)) .appendTo($timeline_items); } get_timeline_item(data, image_html, edit_html, rating_html) { return `
${edit_html}
${image_html}

${rating_html}

${data.subject}

${data.content}

`; } }