Skip to content

Commit

Permalink
feat: update frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
pangbo13 committed Apr 13, 2024
1 parent 4ffc29d commit 7eacd96
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@
labelKey="retort.hide_ignored_retorts"
checked=model.custom_fields.hide_ignored_retorts}}
</div>

</div>
{{/if}}
3 changes: 2 additions & 1 deletion assets/javascripts/discourse/initializers/retort-init.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ function initializePlugin(api) {
});

api.addPostMenuButton("retort", (attrs) => {
if (Retort.disableRetortButton(attrs.id)) {
const post = Retort.postFor(attrs.id);
if (!post.can_retort) {
return;
}
return {
Expand Down
30 changes: 25 additions & 5 deletions assets/javascripts/discourse/widgets/retort-toggle.js.es6
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ export default createWidget("retort-toggle", {
buildKey: (attrs) => `retort-toggle-${attrs.post.id}-${attrs.emoji}-${attrs.usernames.length}`,

defaultState({ emoji, post, usernames, emojiUrl }) {
const is_my_retort = (this.currentUser == null) ? false : usernames.includes(this.currentUser.username);
return { emoji, post, usernames, emojiUrl, is_my_retort};
const my_retort = post.my_retorts?.find((retort) => retort.emoji === emoji);
const is_my_retort = my_retort ? true : false;
const my_retort_update_time = is_my_retort ? new Date(my_retort?.updated_at) : undefined;
return { emoji, post, usernames, emojiUrl, is_my_retort, my_retort_update_time};
},

buildClasses() {
Expand All @@ -44,6 +46,12 @@ export default createWidget("retort-toggle", {
else {return ["not-my-retort"];}
},

// eslint-disable-next-line no-unused-vars
buildAttributes(attrs) {
if (this.disabled()) { return { disabled: true }; }
return {};
},

click() {
if (this.currentUser == null) {
return;
Expand All @@ -52,6 +60,18 @@ export default createWidget("retort-toggle", {
Retort.updateRetort(post, emoji).then(this.updateWidget.bind(this)).catch(popupAjaxError);
},

disabled() {
if (!this.state.post.can_retort) {return true;}
if (this.state.is_my_retort) {
const diff = new Date() - this.state.my_retort_update_time;
if (diff > this.siteSettings.retort_withdraw_tolerance * 1000) {
// cannot withdraw if exceeding torlerance time
return true;
}
}
return false;
},

updateWidget() {
if (this.currentUser == null) {
return;
Expand All @@ -63,20 +83,20 @@ export default createWidget("retort-toggle", {
} else {
this.state.usernames.push(this.currentUser.username);
this.state.is_my_retort = true;
this.state.my_retort_update_time = new Date();
}
this.scheduleRerender();
},

html(attrs) {
const { emoji, usernames, emojiUrl } = this.state;
if (usernames.length <= 0) {return [];}
const res = [
h("img.emoji", { src: emojiUrl, alt: `:${emoji}:` }),
h("span.post-retort__count", usernames.length.toString()),
h("span.post-retort__tooltip", this.sentence(this.state)),
];
if ((!Retort.disableRetortButton(this.state.post.id))
&& attrs.currentUser
&& (attrs.currentUser.trust_level === 4 || attrs.currentUser.staff)) {
if (attrs.post.can_remove_retort) {
res.push(this.attach("retort-remove-emoji", attrs));
}
return res;
Expand Down
4 changes: 4 additions & 0 deletions assets/stylesheets/common/retort.scss
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
display: inline-flex;
align-items: center;

&[disabled=true] {
cursor: not-allowed;
}

img {
margin-right: 4px;
}
Expand Down
2 changes: 1 addition & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins:
retort_withdraw_tolerance:
type: number
default: 5
client: false
client: true
retort_allow_multiple_reactions:
default: true
retort_disabled_categories:
Expand Down

0 comments on commit 7eacd96

Please sign in to comment.