import SubPage from './subpage'; import { get_rating_html } from '../helpers'; erpnext.hub.Item = class Item extends SubPage { 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); } get_item(hub_item_code) { return hub.call('get_item_details', { hub_seller: hub.settings.company_email, 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 || ''; let stats = __('No views yet'); if(item.view_count) { const views_message = __(`${item.view_count} Views`); 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'); stats = `${views_message}${dot_spacer}${rating_html} (${rating_count})`; } let menu_items = ''; if(this.own_item) { menu_items = `
  • ${__('Edit Details')}
  • ${__('Unpublish')}
  • `; } else { menu_items = `
  • ${__('Report this item')}
  • `; } const html = `

    ${title}

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

    ${stats}


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

    ${description}

    ` : `

    ${__('No description')}

    ` }

    Seller Information
    `; this.$wrapper.html(html); this.make_review_area(); this.get_reviews() .then(reviews => { this.reviews = reviews; this.render_reviews(reviews); }); } edit_details() { if (!this.edit_dialog) { this.edit_dialog = new frappe.ui.Dialog({ title: "Edit Your Product", fields: [] }); } this.edit_dialog.show(); } unpublish_item() { if(!this.unpublish_dialog) { this.unpublish_dialog = new frappe.ui.Dialog({ title: "Edit Your Product", fields: [] }); } this.unpublish_dialog.show(); } 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') } ], primary_action: ({ message }) => { if (!message) return; } }); d.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}

    `; } }