Skip to content

Commit 2372fbc

Browse files
committed
feat: move disable user action to the resource table actions
1 parent a4b373d commit 2372fbc

File tree

3 files changed

+54
-48
lines changed

3 files changed

+54
-48
lines changed

custom/DisableButton.vue

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
<template>
2-
<div class="flex items-end justify-start gap-2 cursor-pointer" :class="{'opacity-50': checkboxes.length !== 1}">
3-
<p class="text-justify max-h-[18px] truncate max-w-[60vw] md:max-w-none">Deactivate user</p>
4-
</div>
2+
<Tooltip>
3+
<button
4+
@click="openDialog()"
5+
>
6+
<IconUserRemoveSolid class="w-5 h-5 me-2"/>
7+
</button>
8+
9+
<template v-slot:tooltip>
10+
{{ $t('Deactivate user') }}
11+
</template>
12+
</Tooltip>
513
<Dialog
614
ref="confirmDialog"
715
class="w-96"
@@ -19,36 +27,16 @@
1927

2028
<script lang="ts" setup>
2129
import { Dialog, Tooltip } from '@/afcl';
22-
import { ref, onMounted } from 'vue';
30+
import { ref } from 'vue';
2331
import { AdminUser, type AdminForthResourceCommon } from '@/types';
2432
import adminforth from "@/adminforth"
2533
import { callAdminForthApi } from '@/utils';
26-
import { AdminForthFilterOperators } from '@/types/Common';
27-
34+
import { IconUserRemoveSolid } from '@iconify-prerendered/vue-flowbite';
2835
2936
const confirmDialog = ref(null);
3037
31-
32-
onMounted(async () => {
33-
await new Promise((resolve) => setTimeout(resolve, 50));
34-
35-
adminforth?.list?.updateFilter?.({
36-
field: props.meta.field,
37-
operator: AdminForthFilterOperators.EQ,
38-
value: true,
39-
});
40-
});
41-
4238
function openDialog() {
43-
if ( props.checkboxes.length !== 1 ) {
44-
if (props.checkboxes.length > 1) {
45-
adminforth.alert({message: "Select only one account to deactivate", variant: "warning"})
46-
} else {
47-
adminforth.alert({message: "Select at least one account to deactivate", variant: "warning"})
48-
}
49-
} else {
50-
confirmDialog.value.open()
51-
}
39+
confirmDialog.value.open();
5240
}
5341
5442
async function deactivateUser() {
@@ -57,7 +45,7 @@ async function deactivateUser() {
5745
path: `/plugin/${props.meta.pluginInstanceId}/deactivateUser`,
5846
method: 'POST',
5947
body: {
60-
record: props.checkboxes[0],
48+
record: props.record,
6149
},
6250
});
6351
if (!res || res.ok === false || res.error) {
@@ -66,33 +54,19 @@ async function deactivateUser() {
6654
}
6755
throw new Error("")
6856
}
69-
props.updateList();
57+
props.updateRecords();
7058
} catch (e) {
7159
adminforth.alert({message: `Error deactivating user. ${e}`, variant: "warning"});
7260
}
7361
}
7462
7563
const props = defineProps<{
76-
checkboxes: any,
7764
meta: any,
7865
resource: AdminForthResourceCommon,
7966
adminUser: AdminUser,
80-
updateList: {
81-
type: Function,
82-
required: true
83-
},
84-
clearCheckboxes: {
85-
type: Function
86-
}
67+
record: any,
68+
updateRecords: Function,
8769
}>();
8870
89-
defineExpose({
90-
click
91-
});
92-
93-
function click() {
94-
openDialog();
95-
}
96-
9771
9872
</script>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<template>
2+
3+
</template>
4+
5+
<script lang="ts" setup>
6+
import { onMounted } from 'vue';
7+
import { AdminForthFilterOperators } from '@/types/Common';
8+
import adminforth from "@/adminforth"
9+
onMounted(async () => {
10+
await new Promise((resolve) => setTimeout(resolve, 50));
11+
adminforth?.list?.updateFilter?.({
12+
field: props.meta.field,
13+
operator: AdminForthFilterOperators.EQ,
14+
value: true,
15+
});
16+
});
17+
const props = defineProps<{
18+
meta: any
19+
}>();
20+
</script>

index.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,20 @@ export default class UserSoftDelete extends AdminForthPlugin {
6464
if ( !resourceConfig.options.pageInjections.list ) {
6565
resourceConfig.options.pageInjections.list = {};
6666
}
67-
if ( !resourceConfig.options.pageInjections.list.threeDotsDropdownItems ) {
68-
resourceConfig.options.pageInjections.list.threeDotsDropdownItems = [];
67+
if ( !resourceConfig.options.pageInjections.list.customActionIcons ) {
68+
resourceConfig.options.pageInjections.list.customActionIcons = [];
6969
}
70-
(resourceConfig.options.pageInjections.list.threeDotsDropdownItems as AdminForthComponentDeclaration[]).push(
70+
(resourceConfig.options.pageInjections.list.customActionIcons as AdminForthComponentDeclaration[]).push(
7171
{ file: this.componentPath('DisableButton.vue'), meta: { pluginInstanceId: this.pluginInstanceId, field: this.options.activeFieldName } }
7272
);
7373

74+
if ( !resourceConfig.options.pageInjections.list.afterBreadcrumbs ) {
75+
resourceConfig.options.pageInjections.list.afterBreadcrumbs = [];
76+
}
77+
(resourceConfig.options.pageInjections.list.afterBreadcrumbs as AdminForthComponentDeclaration[]).push(
78+
{ file: this.componentPath('UserSoftDeleteFilterSetter.vue'), meta: { pluginInstanceId: this.pluginInstanceId, field: this.options.activeFieldName } }
79+
);
80+
7481
// simply modify resourceConfig or adminforth.config. You can get access to plugin options via this.options;
7582
}
7683

@@ -106,8 +113,9 @@ export default class UserSoftDelete extends AdminForthPlugin {
106113
if ( isAllowedToDeactivate === false ) {
107114
return {ok: false, error: "Not allowed to deactivate user"}
108115
}
109-
const id = body.record;
116+
110117
const primaryKeyColumn = this.resourceConfig.columns.find((col) => col.primaryKey);
118+
const id = body.record[primaryKeyColumn.name];
111119

112120
const oldUser = await this.adminforth
113121
.resource(this.resourceConfig.resourceId)
@@ -120,6 +128,10 @@ export default class UserSoftDelete extends AdminForthPlugin {
120128
if (oldUser[this.options.activeFieldName] === false) {
121129
return {ok: false, error: "User is already deactivated"}
122130
}
131+
132+
if (oldUser[primaryKeyColumn.name] === adminUser.dbUser[primaryKeyColumn.name]) {
133+
return {ok: false, error: "You cannot deactivate your own account"}
134+
}
123135

124136
const newUser = { ...oldUser, [this.options.activeFieldName]: false };
125137

0 commit comments

Comments
 (0)