@salla.sa/twilight-components
Version:
Salla Web Component
212 lines (211 loc) • 12.4 kB
JavaScript
/*!
* Crafted with ❤ by Salla
*/
import { h, Host } from "@stencil/core";
import CheckIcon from "../../assets/svg/check.svg";
import Reply from "../../assets/svg/reply.svg";
import ThumbsUp from "../../assets/svg/thumbs-up.svg";
export class SallaCommentItem {
constructor() {
// Translations
this.has_bought_trans = salla.lang.get('blocks.comments.has_bought');
this.rated_trans = salla.lang.get('pages.rating.rated');
this.waiting_approval_trans = salla.lang.get('blocks.comments.waiting_approval');
this.has_order_trans = salla.lang.get('blocks.comments.has_order');
this.allowLikes = salla.config.get('store.settings.rating.allow_likes');
this.allowAttachImages = salla.config.get('store.settings.rating.allow_attach_images');
this.helpfulLabel = salla.lang.getWithDefault('blocks.comments.helpful', 'مفيد');
this.likesCount = 0;
this.likedComments = [];
salla.lang.onLoaded(() => {
const setNestedAsync = (lang, key, value) => {
return new Promise((resolve) => {
salla.helpers.setNested(salla.lang.messages[lang], key, value);
resolve(true);
});
};
const setTranslations = async () => {
await setNestedAsync('ar.trans', 'blocks.comments.helpful', 'مفيد');
await setNestedAsync('en.trans', 'blocks.comments.helpful', 'Helpful');
this.helpfulLabel = salla.lang.getWithDefault('blocks.comments.helpful', 'مفيد');
};
this.has_bought_trans = salla.lang.get('blocks.comments.has_bought');
this.rated_trans = salla.lang.get('pages.rating.rated');
this.waiting_approval_trans = salla.lang.get('blocks.comments.waiting_approval');
this.has_order_trans = salla.lang.get('blocks.comments.has_order');
setTranslations();
});
}
componentDidLoad() {
this.likesCount = this.comment.likes_count;
try {
this.likedComments = JSON.parse(localStorage.getItem('liked_comments') || '[]');
if (this.likedComments.includes(this.comment.id)) {
this.likeBtn.classList.add('liked');
this.likeBtn.fill = "solid";
}
}
catch (_a) {
salla.log('Bad json for liked_comments');
}
}
getReplies() {
return Array.isArray(this.comment.replies) ? this.comment.replies : [this.comment.replies];
}
getDate(dateString) {
const [datePart] = dateString.split(' ');
const [year, month, day] = datePart.split('-');
const formattedDate = `${parseInt(day, 10)}/${parseInt(month, 10)}/${parseInt(year, 10)}`;
return formattedDate;
}
getTime(dateString) {
const [, timePart] = dateString.split(' ');
const [hours, minutes] = timePart.split(':');
const formattedTime = `${parseInt(hours, 10)}:${parseInt(minutes, 10)}`;
return formattedTime;
}
async toggleLike() {
if (salla.config.isGuest()) {
return salla.notify.error(salla.lang.get('common.messages.must_login'));
}
this.likedComments = JSON.parse(localStorage.getItem('liked_comments') || '[]');
const isLiked = this.likedComments.includes(this.comment.id);
try {
const endpoint = isLiked ? `rating/${this.comment.id}/unlike` : `rating/${this.comment.id}/like`;
const res = await salla.api.request(endpoint, '', 'put');
salla.log(res.message);
if (isLiked) {
this.likeBtn.classList.remove('liked');
this.likeBtn.fill = 'outline';
this.updateLikedComments(this.comment.id, false);
this.likesCount--;
}
else {
this.likeBtn.classList.add('liked');
this.likeBtn.fill = 'solid';
this.updateLikedComments(this.comment.id, true);
this.likesCount++;
}
}
catch (e) {
if (e.response.status == 409) {
if (this.likeBtn.classList.contains('liked')) {
this.likeBtn.fill = 'outline';
this.likeBtn.classList.remove('liked');
this.updateLikedComments(this.comment.id, false);
this.likesCount--;
}
else {
this.likeBtn.fill = 'solid';
this.likeBtn.classList.add('liked');
salla.logger.warn('Like already exists');
this.updateLikedComments(this.comment.id, true);
}
}
}
}
updateLikedComments(commentId, add) {
this.likedComments = JSON.parse(localStorage.getItem('liked_comments') || '[]');
if (add) {
if (!this.likedComments.includes(commentId)) {
this.likedComments.push(commentId);
}
}
else {
this.likedComments = this.likedComments.filter(id => id !== commentId);
}
localStorage.setItem('liked_comments', JSON.stringify(this.likedComments));
}
render() {
var _a, _b, _c, _d, _e, _f;
let isAdmin = this.comment.type == 'admin';
return (h(Host, { key: '779f9f3d8e70305fd8a875cb3d7692ddb9e9930e', class: isAdmin ? 's-comments-item-admin' : 's-comments-item' }, h("div", { key: '2a13cd2c638b713b31a97a9b5e7f03759a7e2296', class: { "s-comments-item-wrapper": !isAdmin, "s-comments-item-admin-wrapper": isAdmin }, id: `s-comments-item-${this.comment.id}` }, h("div", { key: 'c3accf6c8363a0cc0919c573ce5345eddb6be550', class: "s-comments-item-inner s-comments-flex-1" }, isAdmin && h("span", { key: '9538973ab5c1d4bdd33f4d532f7041f94a1cb2d7', class: "s-comments-item-reply-icon", innerHTML: Reply }), h("div", { key: '327abfdfab42ab6aeff9579dcc8a72991c141c19', class: "s-comments-item-avatar" }, h("img", { key: 'bd8db28bad90847a94773d59d0d96cc693a8cb92', "data-src": (_a = this.comment) === null || _a === void 0 ? void 0 : _a.avatar, alt: (_b = this.comment) === null || _b === void 0 ? void 0 : _b.name, src: (_c = this.comment) === null || _c === void 0 ? void 0 : _c.avatar, class: "s-comments-item-avatar-img lazy" })), h("div", { key: 'b7d764ee0c96ab53b89170f1609821d5c204faef', class: "s-comments-flex-1" }, h("div", { key: '6ed0d090d9ae163d75f438459326c2d25c7bfd69', class: "s-comments-item-user-wrapper" }, h("div", { key: '1631c0b8667906d279b7105022f197a3b5ed59b6', class: "s-comments-item-user-info" }, h("h3", { key: '6a325e676e77ab1ef6df92d5bbffad2c7393d517', class: "s-comments-item-user-info-name" }, (_d = this.comment) === null || _d === void 0 ? void 0 : _d.name), (this.comment.has_order || !!this.comment.rating) && !this.comment.is_pending && !this.hideBought ?
h("div", { class: "s-comments-flex" }, this.comment.has_order ?
[h("span", { class: "s-comments-item-has-order-check-icon", innerHTML: CheckIcon }), h("span", { class: "s-comments-item-has-order-check-text" }, this.has_bought_trans, " ", this.comment.rating ? ', ' : '')] : null, !!this.comment.rating ?
h("span", { class: "s-comments-item-rated-widget" }, this.rated_trans) : null)
: null), h("p", { key: '22efe65a7cb5717964a1e2012b7739ab7d4ad0c7', class: "s-comments-item-timestamp s-ltr" }, " ", this.getDate((_e = this.comment.created_at) === null || _e === void 0 ? void 0 : _e.date), " ", h("span", { key: '1057fa700388f7be30240254e07f532773332207', class: "s-comments-item-time" }, " - ", this.getTime((_f = this.comment.created_at) === null || _f === void 0 ? void 0 : _f.date), " ")), !!this.comment.rating || !!this.comment.stars ?
h("salla-rating-stars", { size: "mini", class: "s-comments-item-stars", value: this.comment.rating || this.comment.stars })
: null), h("div", { key: '38191d3d3374eb66bd41f29256f8e9edcb089f0e', class: "s-comments-item-content" }, h("p", { key: '8d4ae7f1e812821f5f76cfe638b8460a546e2d70', innerHTML: this.comment.content }), this.allowAttachImages && h("div", { key: '872de29de2c941586a6ff7ee1612099ce0808eff', class: "s-comments-item-images" }, this.comment.images.map((image, index) => (h("img", { key: index, src: image, alt: "", onClick: () => this.modal.open() }))), h("salla-modal", { key: '6930522e277f3e58f369857cd04ef4f76a10cef3', ref: modal => this.modal = modal, width: "sm" }, h("salla-slider", { key: '042b41bbbe78bcd7f0cca354c62290b0365d78ae', id: `s-comments-item-${this.comment.id}-images`, class: "s-comments-item-images-slider", type: "thumbs", "auto-height": true, showControls: this.comment.images.length > 1 ? true : false, "show-thumbs-controls": "false" }, h("div", { key: '5131bdcf40dcf80a81d0636806fbbb7d3dbd349e', slot: 'items' }, this.comment.images.map((image, index) => (h("img", { key: index, src: image, alt: "" })))), h("div", { key: '6efdd073e88d30f35e8ec5ef361590804451bd73', slot: "thumbs" }, this.comment.images.map((image, index) => (h("div", { class: "s-comments-item-images-slider-thumb" }, h("img", { key: index, src: image, alt: "" })))))))), this.allowLikes && !isAdmin && salla.url.is_page('product.single') ? h("salla-button", { ref: el => this.likeBtn = el, class: `s-comments-item-like-btn ${this.likedComments.includes(this.comment.id) ? 'liked' : ''}`, loaderPosition: 'center', fill: 'outline', size: 'small', onClick: () => this.toggleLike() }, h("span", null, this.helpfulLabel, " ", this.likesCount > 0 ? `(${this.likesCount})` : ''), h("span", { innerHTML: ThumbsUp })) : '', this.comment.is_pending ?
h("span", { class: "s-comments-item-pending-text" }, this.waiting_approval_trans) : null))), !!this.getReplies().length && !isAdmin ?
this.getReplies().map((reply) => {
return h("div", null, h("salla-comment-item", { comment: reply }));
}) : null)));
}
static get is() { return "salla-comment-item"; }
static get originalStyleUrls() {
return {
"$": ["salla-comments.scss"]
};
}
static get styleUrls() {
return {
"$": ["salla-comments.css"]
};
}
static get properties() {
return {
"comment": {
"type": "unknown",
"attribute": "comment",
"mutable": false,
"complexType": {
"original": "Comment",
"resolved": "Comment",
"references": {
"Comment": {
"location": "import",
"path": "./interfaces",
"id": "src/components/salla-comments/interfaces.ts::Comment"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Single Comment Instance"
},
"getter": false,
"setter": false
},
"hideBought": {
"type": "unknown",
"attribute": "hide-bought",
"mutable": false,
"complexType": {
"original": "Boolean",
"resolved": "Boolean",
"references": {
"Boolean": {
"location": "global",
"id": "global::Boolean"
}
}
},
"required": false,
"optional": false,
"docs": {
"tags": [],
"text": "Hide Bought"
},
"getter": false,
"setter": false
}
};
}
static get states() {
return {
"has_bought_trans": {},
"rated_trans": {},
"waiting_approval_trans": {},
"has_order_trans": {},
"allowLikes": {},
"allowAttachImages": {},
"helpfulLabel": {},
"likesCount": {},
"likedComments": {}
};
}
static get elementRef() { return "host"; }
}
//# sourceMappingURL=salla-comment-item.js.map