brotherton-erpnext/erpnext/public/js/hub/pages/Item.vue

357 lines
8.1 KiB
Vue
Raw Normal View History

<template>
2019-12-09 10:14:31 +00:00
<div class="marketplace-page" :data-page-name="page_name" v-if="init || item">
<detail-view
:title="title"
:image="image"
:sections="sections"
:menu_items="menu_items"
:show_skeleton="init"
>
2019-12-09 10:14:31 +00:00
<detail-header-item slot="detail-header-item" :value="item_subtitle"></detail-header-item>
<detail-header-item slot="detail-header-item" :value="item_views_and_ratings"></detail-header-item>
2019-12-09 10:14:31 +00:00
<button
v-if="primary_action"
slot="detail-header-item"
class="btn btn-primary btn-sm margin-top"
@click="primary_action.action"
2019-12-09 10:14:31 +00:00
>{{ primary_action.label }}</button>
</detail-view>
2018-09-04 05:29:20 +00:00
<review-area v-if="!init" :hub_item_name="hub_item_name"></review-area>
</div>
</template>
<script>
import ReviewArea from '../components/ReviewArea.vue';
import { get_rating_html } from '../components/reviews';
2019-12-19 14:41:37 +00:00
import { edit_details_dialog } from '../components/edit_details_dialog';
export default {
name: 'item-page',
components: {
ReviewArea
},
data() {
return {
page_name: frappe.get_route()[1],
hub_item_name: frappe.get_route()[2],
init: true,
item: null,
title: null,
image: null,
2019-12-09 10:14:31 +00:00
sections: []
};
},
computed: {
is_own_item() {
let is_own_item = false;
2019-12-09 10:14:31 +00:00
if (this.item) {
if (this.item.hub_seller === hub.settings.hub_seller_name) {
is_own_item = true;
}
}
return is_own_item;
},
2019-12-09 10:14:31 +00:00
menu_items() {
return [
{
label: __('Save Item'),
condition: hub.is_user_registered() && !this.is_own_item,
action: this.add_to_saved_items
},
{
label: __('Add to Featured Item'),
condition: hub.is_user_registered() && this.is_own_item,
action: this.add_to_featured_items
},
{
2018-08-30 10:05:06 +00:00
label: __('Report this Item'),
condition: !this.is_own_item,
action: this.report_item
},
{
label: __('Edit Details'),
condition: hub.is_user_registered() && this.is_own_item,
2018-08-30 10:38:52 +00:00
action: this.edit_details
},
{
2018-08-30 10:05:06 +00:00
label: __('Unpublish Item'),
condition: hub.is_user_registered() && this.is_own_item,
2018-08-30 10:38:52 +00:00
action: this.unpublish_item
}
2019-12-09 10:14:31 +00:00
];
},
item_subtitle() {
2019-12-09 10:14:31 +00:00
if (!this.item) {
return '';
}
const dot_spacer = '<span aria-hidden="true"> · </span>';
let subtitle_items = [comment_when(this.item.creation)];
const rating = this.item.average_rating;
if (rating > 0) {
2019-12-09 10:14:31 +00:00
subtitle_items.push(rating + `<i class='fa fa-fw fa-star-o'></i>`);
}
2019-12-09 10:14:31 +00:00
subtitle_items.push({
value: this.item.company,
on_click: this.go_to_seller_profile_page
});
return subtitle_items;
},
item_views_and_ratings() {
2019-12-09 10:14:31 +00:00
if (!this.item) {
return '';
}
let stats = __('No views yet');
2019-12-09 10:14:31 +00:00
if (this.item.view_count) {
const views_message = __(`${this.item.view_count} Views`);
const rating_html = get_rating_html(this.item.average_rating);
2019-12-09 10:14:31 +00:00
const rating_count =
this.item.no_of_ratings > 0
? `${this.item.no_of_ratings} reviews`
: __('No reviews yet');
stats = [views_message, rating_html, rating_count];
}
return stats;
},
primary_action() {
if (hub.is_user_registered()) {
return {
label: __('Contact Seller'),
action: this.contact_seller.bind(this)
2019-12-09 10:14:31 +00:00
};
} else {
return undefined;
}
}
},
created() {
this.get_item_details();
},
mounted() {
// To record a single view per session, (later)
// erpnext.hub.item_view_cache = erpnext.hub.item_view_cache || [];
// if (erpnext.hub.item_view_cache.includes(this.hub_item_name)) {
// return;
// }
this.item_received.then(() => {
setTimeout(() => {
hub.call('add_item_view', {
hub_item_name: this.hub_item_name
2019-12-09 10:14:31 +00:00
});
// .then(() => {
// erpnext.hub.item_view_cache.push(this.hub_item_name);
// });
}, 5000);
});
},
methods: {
get_item_details() {
2019-12-09 10:14:31 +00:00
this.item_received = hub
.call('get_item_details', { hub_item_name: this.hub_item_name })
.then(item => {
2019-12-09 10:14:31 +00:00
this.init = false;
this.item = item;
2019-12-09 10:14:31 +00:00
this.build_data();
this.make_dialogs();
});
},
go_to_seller_profile_page(seller_name) {
frappe.set_route(`marketplace/seller/${seller_name}`);
},
build_data() {
this.title = this.item.item_name || this.item.name;
this.image = this.item.image;
this.sections = [
{
2018-08-30 10:05:06 +00:00
title: __('Item Description'),
content: this.item.description
? __(this.item.description)
: __('No description')
},
{
title: __('Seller Information'),
content: this.item.seller_description
? __(this.item.seller_description)
: __('No description')
}
];
},
2018-08-30 10:38:52 +00:00
make_dialogs() {
this.make_contact_seller_dialog();
2018-08-30 10:41:41 +00:00
this.make_report_item_dialog();
2019-12-01 19:39:59 +00:00
this.make_editing_dialog();
},
add_to_saved_items() {
hub.call('add_item_to_user_saved_items', {
2019-12-09 10:14:31 +00:00
hub_item_name: this.hub_item_name,
hub_user: frappe.session.user
})
.then(() => {
2019-12-24 12:09:15 +00:00
const saved_items_link = `<b><a href="#marketplace/saved-items">${__('Saved')}</a></b>`;
2019-12-09 10:14:31 +00:00
frappe.show_alert(saved_items_link);
erpnext.hub.trigger('action:item_save');
})
.catch(e => {
console.error(e);
});
},
add_to_featured_items() {
hub.call('add_item_to_seller_featured_items', {
2019-12-09 10:14:31 +00:00
hub_item_name: this.hub_item_name,
hub_user: frappe.session.user
})
.then(() => {
2019-12-24 12:09:15 +00:00
const featured_items_link = `<b><a href="#marketplace/featured-items">${__('Added to Featured Items')}</a></b>`;
2019-12-09 10:14:31 +00:00
frappe.show_alert(featured_items_link);
erpnext.hub.trigger('action:item_feature');
})
.catch(e => {
console.error(e);
});
},
2018-08-30 10:38:52 +00:00
make_contact_seller_dialog() {
this.contact_seller_dialog = 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;
hub.call('send_message', {
2019-12-09 10:14:31 +00:00
hub_item: this.item.name,
message
})
.then(() => {
this.contact_seller_dialog.hide();
frappe.set_route('marketplace', 'buying', this.item.name);
2019-12-09 10:14:31 +00:00
erpnext.hub.trigger('action:send_message');
});
}
});
2018-08-30 10:38:52 +00:00
},
2018-08-30 10:41:41 +00:00
make_report_item_dialog() {
this.report_item_dialog = new frappe.ui.Dialog({
title: __('Report Item'),
fields: [
{
label: __('Why do think this Item should be removed?'),
fieldtype: 'Text',
fieldname: 'message'
}
],
primary_action: ({ message }) => {
2019-12-19 14:41:37 +00:00
hub.call('add_reported_item', {
2019-12-09 10:14:31 +00:00
hub_item_name: this.item.name,
message
})
2018-08-30 10:41:41 +00:00
.then(() => {
d.hide();
frappe.show_alert(__('Item Reported'));
});
}
});
},
2019-12-01 19:39:59 +00:00
make_editing_dialog() {
2019-12-19 14:41:37 +00:00
this.edit_dialog = edit_details_dialog({
primary_action: {
fn: values => {
2019-12-01 19:39:59 +00:00
this.update_details(values);
2019-12-19 14:41:37 +00:00
this.edit_dialog.hide();
2019-12-01 19:39:59 +00:00
}
2019-12-19 14:41:37 +00:00
},
defaults: {
item_name: this.item.item_name,
hub_category: this.item.hub_category,
description: this.item.description
2019-12-01 19:39:59 +00:00
}
2019-12-19 14:41:37 +00:00
});
2019-12-01 19:39:59 +00:00
},
update_details(values) {
2019-12-19 14:41:37 +00:00
frappe.call('erpnext.hub_node.api.update_item', {
2019-12-01 19:39:59 +00:00
ref_doc: this.item.name,
data: values
2019-12-19 14:41:37 +00:00
})
.then(r => {
return this.get_item_details();
})
.then(() => {
frappe.show_alert(__(`${this.item.item_name} Updated`));
});
2019-12-01 19:39:59 +00:00
},
2018-08-30 10:38:52 +00:00
contact_seller() {
this.contact_seller_dialog.show();
},
2018-08-30 10:41:41 +00:00
report_item() {
2018-11-05 13:48:48 +00:00
if (!hub.is_seller_registered()) {
2019-12-09 10:14:31 +00:00
frappe.throw(
__('Please login as a Marketplace User to report this item.')
);
2018-11-05 13:48:48 +00:00
}
2018-08-30 10:41:41 +00:00
this.report_item_dialog.show();
},
2018-08-30 10:38:52 +00:00
edit_details() {
2019-12-01 19:39:59 +00:00
if (!hub.is_seller_registered()) {
2019-12-19 14:41:37 +00:00
frappe.throw(
__('Please login as a Marketplace User to edit this item.')
);
2019-12-01 19:39:59 +00:00
}
2019-12-19 14:41:37 +00:00
this.edit_dialog.show();
2018-08-30 10:38:52 +00:00
},
2018-08-30 10:38:52 +00:00
unpublish_item() {
2019-12-24 12:09:15 +00:00
frappe.confirm(__(`Unpublish {0}?`, [this.item.item_name]), () => {
2019-12-09 10:14:31 +00:00
frappe
.call('erpnext.hub_node.api.unpublish_item', {
2019-12-09 13:54:29 +00:00
item_code: this.item.item_code,
hub_item_name: this.hub_item_name
2019-12-09 10:14:31 +00:00
})
.then(r => {
frappe.set_route(`marketplace/home`);
frappe.show_alert(__('Item listing removed'));
});
});
}
}
2019-12-09 10:14:31 +00:00
};
</script>
<style scoped></style>