Skip to content

Commit

Permalink
fix: change feedback to value of 0/1/2
Browse files Browse the repository at this point in the history
  • Loading branch information
RitvikSardana committed Nov 25, 2024
1 parent 5d96b71 commit 1db79d3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 65 deletions.
60 changes: 15 additions & 45 deletions desk/src/pages/KnowledgeBaseArticle.vue
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
shadow: editMode,
'p-4': editMode,
}"
editor-class="prose-f"
editor-class="prose-f mt-2"
@change="articleContent = $event"
>
<template #top>
Expand Down Expand Up @@ -76,29 +76,23 @@
<Icon
class="w-6 h-6 cursor-pointer"
:icon="
articleFeedback?.user_feedback == 'Like'
userFeedback === 1
? 'prime:thumbs-up-fill'
: 'prime:thumbs-up'
"
@click="handleFeedbackClick('Like')"
/>
<span class="text-p-md text-gray-700">{{
articleFeedback?.total_likes
}}</span>
</div>
<div class="flex items-center justify-center">
<Icon
class="w-6 h-6 cursor-pointer"
:icon="
articleFeedback?.user_feedback == 'Dislike'
userFeedback === 2
? 'prime:thumbs-down-fill'
: 'prime:thumbs-down'
"
@click="handleFeedbackClick('Dislike')"
/>
<span class="text-p-md text-gray-700">{{
articleFeedback?.total_dislikes
}}</span>
</div>
</div>
</div>
Expand Down Expand Up @@ -139,7 +133,7 @@ import KnowledgeBaseArticleTopNew from "./knowledge-base/KnowledgeBaseArticleTop
import KnowledgeBaseArticleTopView from "./knowledge-base/KnowledgeBaseArticleTopView.vue";
import { PreserveIds } from "@/tiptap-extensions";
import { Icon } from "@iconify/vue";
import { ArticleFeedback, Feedback } from "@/types";
import { FeedbackAction } from "@/types";
const props = defineProps({
articleId: {
type: String,
Expand Down Expand Up @@ -260,15 +254,15 @@ const topComponent = computed(() => {
return KnowledgeBaseArticleTopView;
});
const articleFeedback = ref<ArticleFeedback>(null);
const userFeedback = ref<FeedbackAction>(null);
const article = createResource({
url: "helpdesk.helpdesk.doctype.hd_article.api.get_article",
params: {
name: props.articleId,
},
onSuccess(data) {
articleTitle.value = data.title;
articleFeedback.value = data.feedback;
userFeedback.value = data.user_feedback;
capture("article_viewed", {
data: {
user: authStore.userId,
Expand Down Expand Up @@ -296,43 +290,19 @@ const setFeedback = createResource({
},
});
const userAction = ref<Feedback>("");
function handleFeedbackClick(action: Feedback) {
if (
articleFeedback.value.user_feedback &&
articleFeedback.value.user_feedback === action
) {
articleFeedback.value.user_feedback = null;
userAction.value = "";
} else {
articleFeedback.value.user_feedback = action;
userAction.value = action;
}
handleOptimisticUpdate(action);
const userAction = ref();
const feedbackMap = {
Remove: 0,
Like: 1,
Dislike: 2,
};
function handleFeedbackClick(action: string) {
userAction.value = feedbackMap[action];
userFeedback.value = feedbackMap[action];
setFeedback.submit();
}
function handleOptimisticUpdate(action: Feedback) {
if (action === "Like" && action === articleFeedback.value.user_feedback) {
articleFeedback.value.total_likes++;
articleFeedback.value.total_dislikes > 0 &&
articleFeedback.value.total_dislikes--;
} else if (
action === "Dislike" &&
action === articleFeedback.value.user_feedback
) {
articleFeedback.value.total_dislikes++;
articleFeedback.value.total_likes > 0 &&
articleFeedback.value.total_likes--;
} else if (action == "Like" && !articleFeedback.value.user_feedback) {
articleFeedback.value.total_likes--;
} else if (action == "Dislike" && !articleFeedback.value.user_feedback) {
articleFeedback.value.total_dislikes--;
}
}
const category = createDocumentResource({
doctype: "HD Article Category",
name: categoryId.value,
Expand Down
8 changes: 1 addition & 7 deletions desk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,4 @@ export interface Category {
children?: (Article | SubCategory)[];
}

export interface ArticleFeedback {
user_feedback: string;
total_likes: number;
total_dislikes: number;
}

export type Feedback = "Like" | "Dislike" | "";
export type FeedbackAction = 0 | 1 | 2; // 0: neutral, 1: like, 2: dislike
17 changes: 6 additions & 11 deletions helpdesk/helpdesk/doctype/hd_article/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,18 @@ def get_article(name: str):
# if not is_agent() and user != author.name:
# frappe.db.set_value("HD Article", name, "views", article["views"] + 1)

feedback = {
"user_feedback": frappe.db.get_value(
user_feedback = int(
frappe.db.get_value(
"HD Article Feedback", {"user": user, "article": name}, "feedback"
)
or None,
"total_likes": frappe.db.count(
"HD Article Feedback", {"article": name, "feedback": "Like"}
),
"total_dislikes": frappe.db.count(
"HD Article Feedback", {"article": name, "feedback": "Dislike"}
),
}
or 0
)
print("\n\n", user_feedback, "\n\n")

return {
**article,
"author": author,
"category": category,
"sub_category": sub_category,
"feedback": feedback,
"user_feedback": user_feedback,
}
1 change: 1 addition & 0 deletions helpdesk/helpdesk/doctype/hd_article/hd_article.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def before_save(self):

@frappe.whitelist()
def set_feedback(self, value):
# 0 empty, 1 like, 2 dislike
user = frappe.session.user
feedback = frappe.db.exists(
"HD Article Feedback", {"user": user, "article": self.name}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"fieldtype": "Select",
"in_list_view": 1,
"label": "Feedback",
"options": "\nLike\nDislike"
"options": "0\n1\n2"
},
{
"fieldname": "article",
Expand All @@ -33,7 +33,7 @@
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-11-25 13:02:57.975496",
"modified": "2024-11-25 14:00:41.166072",
"modified_by": "Administrator",
"module": "Helpdesk",
"name": "HD Article Feedback",
Expand Down

0 comments on commit 1db79d3

Please sign in to comment.