Skip to content

Commit a758102

Browse files
committed
pkp/pkp-lib#11327 Allow user to delete comment
1 parent 4422173 commit a758102

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

src/frontend/components/PkpButton/PkpButton.vue

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ const props = defineProps({
1313
type: Boolean,
1414
default: false,
1515
},
16+
/** Use when this button represents an action such as delete, go back, revert or cancel. */
17+
isWarnable: Boolean,
1618
});
1719
1820
const buttonClass = computed(() => {
1921
return [
2022
'pkpButton',
2123
props.isSecondary ? 'pkpButton--secondary' : 'pkpButton--primary',
24+
props.isWarnable ? 'pkpButton--warnable' : '',
2225
];
2326
});
2427
</script>
@@ -73,4 +76,17 @@ const buttonClass = computed(() => {
7376
border-color: var(--pkp-text-color-disabled);
7477
color: var(--pkp-text-color-disabled);
7578
}
79+
80+
.pkpButton--warnable {
81+
color: var(--pkp-color-negative);
82+
border-color: var(--pkp-border-color-light);
83+
background-color: var(--pkp-background-color-secondary);
84+
}
85+
86+
.pkpButton--warnable:disabled {
87+
color: var(--pkp-text-color-disabled);
88+
}
89+
.pkpButton--warnable:hover {
90+
background-color: var(--pkp-background-color-secondary);
91+
}
7692
</style>

src/frontend/components/PkpModal/PkpDialog.vue

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ import {
6868
} from 'reka-ui';
6969
import PkpIcon from '@/frontend/components/PkpIcon/PkpIcon.vue';
7070
71+
import {useLocalize} from '@/composables/useLocalize';
72+
const {t} = useLocalize();
73+
7174
const props = defineProps({
7275
/** Used only internally, don't pass this prop via openDialog */
7376
opened: {type: Boolean, default: false},

src/frontend/components/PkpUserComment/PkpUserComment.vue

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ const props = defineProps({
3838
type: Number,
3939
required: true,
4040
},
41-
publicationId: {
42-
type: Number,
43-
required: true,
44-
},
4541
publicationIds: {
4642
type: Array,
4743
required: false,

src/frontend/components/PkpUserComment/PkpUserCommentsList.vue

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,9 @@ import {usePkpModal} from '@/frontend/composables/usePkpModal';
107107
import {usePkpFetch} from '@/frontend/composables/usePkpFetch';
108108
import {useUrl} from '@/frontend/composables/usePkpUrl';
109109
import {usePkpDate} from '@/frontend/composables/usePkpDate';
110-
import {t} from '@/utils/i18n';
110+
import {useLocalize} from '@/composables/useLocalize';
111+
112+
const {t} = useLocalize();
111113
112114
const {openModal, closeModal, openDialogNetworkError} = usePkpModal();
113115
const {formatShortDateTime} = usePkpDate();
@@ -145,8 +147,49 @@ const commentText = ref('');
145147
146148
const commentActionMethods = {
147149
commentReport,
150+
deleteComment,
148151
};
149152
153+
function deleteComment(comment) {
154+
if (!currentUser || currentUser.id !== comment.userId) {
155+
throw new Error('Only the comment author can delete the comment');
156+
}
157+
158+
const {openDialog} = usePkpModal();
159+
openDialog({
160+
title: 'Delete Comment',
161+
message: t('userComment.deleteCommentConfirm', {
162+
comment: comment.commentText,
163+
}),
164+
actions: [
165+
{
166+
label: t('common.delete'),
167+
isWarnable: true,
168+
callback: async (close) => {
169+
const {apiUrl} = useUrl(`comments/${comment.id}`);
170+
const {fetch: deleteComment, isSuccess} = usePkpFetch(apiUrl, {
171+
method: 'DELETE',
172+
});
173+
await deleteComment();
174+
if (isSuccess.value) {
175+
comments.value = comments.value.filter((c) => c.id !== comment.id);
176+
} else {
177+
openDialogNetworkError();
178+
}
179+
180+
close();
181+
},
182+
},
183+
{
184+
label: t('common.cancel'),
185+
callback: (close) => {
186+
close();
187+
},
188+
},
189+
],
190+
});
191+
}
192+
150193
function canReportComment(comment) {
151194
return currentUser && currentUser.id !== comment.userId && comment.isApproved;
152195
}

0 commit comments

Comments
 (0)