-
Notifications
You must be signed in to change notification settings - Fork 731
Expand file tree
/
Copy pathmember-list-toolbar.vue
More file actions
411 lines (357 loc) · 11.4 KB
/
member-list-toolbar.vue
File metadata and controls
411 lines (357 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
<template>
<lf-table-bulk-actions :selected-items="selectedMembers">
<el-dropdown trigger="click" @command="handleCommand">
<lf-button
type="secondary-gray"
size="small"
>
<span class="mr-2">Actions</span>
<lf-icon name="chevron-down" :size="24" />
</lf-button>
<template #dropdown>
<el-tooltip
v-if="selectedMembers.length === 2 && hasPermission(LfPermission.mergeMembers)"
content="Coming soon"
placement="top"
>
<span>
<el-dropdown-item
:command="{ action: 'mergeMembers' }"
:disabled="!hasPermission(LfPermission.mergeMembers)"
>
<lf-icon name="user-group" :size="20" class="mr-1" />
Merge profile
</el-dropdown-item>
</span>
</el-tooltip>
<el-dropdown-item
v-if="hasPermission(LfPermission.memberEdit)"
:command="{
action: 'markAsTeamMember',
value: markAsTeamMemberOptions.value,
}"
>
<lf-icon :name="markAsTeamMemberOptions.icon" :size="20" class="mr-1" />
{{ markAsTeamMemberOptions.copy }}
</el-dropdown-item>
<el-dropdown-item
v-if="hasPermission(LfPermission.memberEdit)"
:command="{
action: 'markAsBot',
value: markAsBotOptions.value,
}"
>
<lf-icon :name="markAsBotOptions.icon" :size="20" class="mr-1" />
{{ markAsBotOptions.copy }}
</el-dropdown-item>
<el-dropdown-item
v-if="hasPermission(LfPermission.memberEdit)"
:command="{ action: 'editAttribute' }"
>
<lf-icon name="file-pen" :size="20" class="mr-1" />
Edit attribute
</el-dropdown-item>
<template v-if="hasPermission(LfPermission.memberDestroy)">
<hr class="border-gray-200 my-1 mx-2" />
<el-dropdown-item
:command="{ action: 'destroyAll' }"
>
<div
class="flex items-center text-red-500"
>
<lf-icon name="trash-can" :size="20" class="mr-2" />
Delete
</div>
</el-dropdown-item>
</template>
</template>
</el-dropdown>
</lf-table-bulk-actions>
<app-bulk-edit-attribute-popover
v-model="bulkAttributesUpdateVisible"
/>
</template>
<script setup>
import { computed, ref } from 'vue';
import { useRoute } from 'vue-router';
import { storeToRefs } from 'pinia';
import pluralize from 'pluralize';
import { useMemberStore } from '@/modules/member/store/pinia';
import { useLfSegmentsStore } from '@/modules/lf/segments/store';
import { MemberService } from '@/modules/member/member-service';
import ConfirmDialog from '@/shared/dialog/confirm-dialog';
import { ToastStore } from '@/shared/message/notification';
import { showExportDialog } from '@/modules/member/member-export-limit';
import AppBulkEditAttributePopover from '@/modules/member/components/bulk/bulk-edit-attribute-popover.vue';
import useMemberMergeMessage from '@/shared/modules/merge/config/useMemberMergeMessage';
import { useAuthStore } from '@/modules/auth/store/auth.store';
import usePermissions from '@/shared/modules/permissions/helpers/usePermissions';
import { LfPermission } from '@/shared/modules/permissions/types/Permissions';
import useProductTracking from '@/shared/modules/monitoring/useProductTracking';
import { EventType, FeatureEventKey } from '@/shared/modules/monitoring/types/event';
import LfIcon from '@/ui-kit/icon/Icon.vue';
import LfButton from '@/ui-kit/button/Button.vue';
import LfTableBulkActions from '@/ui-kit/table/table-bulk-actions.vue';
import { useQueryClient } from '@tanstack/vue-query';
import { TanstackKey } from '@/shared/types/tanstack';
const { trackEvent } = useProductTracking();
const route = useRoute();
const queryClient = useQueryClient();
const authStore = useAuthStore();
const { getUser } = authStore;
const memberStore = useMemberStore();
const { selectedMembers, filters } = storeToRefs(memberStore);
const { hasPermission } = usePermissions();
const bulkAttributesUpdateVisible = ref(false);
// Refresh member data by invalidating TanStack Query cache
// Note: Backend cache is invalidated by passing invalidateCache parameter to update/delete operations
const refreshMemberData = async () => {
await queryClient.invalidateQueries({
queryKey: [TanstackKey.MEMBERS_LIST],
});
};
// Helper function to fetch member with all attributes before bulk update
const fetchMemberWithAllAttributes = async (memberId) => {
const lsSegmentsStore = useLfSegmentsStore();
const { selectedProjectGroup } = storeToRefs(lsSegmentsStore);
const response = await MemberService.find(
memberId,
selectedProjectGroup.value?.id,
true,
);
return response;
};
const markAsTeamMemberOptions = computed(() => {
const isTeamView = filters.value.settings.teamMember === 'filter';
const membersCopy = pluralize(
'person',
selectedMembers.value.length,
false,
);
if (isTeamView) {
return {
icon: 'bookmark-slash',
copy: `Unmark as team ${membersCopy}`,
value: false,
};
}
return {
icon: 'bookmark',
copy: `Mark as team ${membersCopy}`,
value: true,
};
});
const markAsBotOptions = computed(() => {
const membersCopy = pluralize(
'person',
selectedMembers.value.length,
false,
);
// Check if any of the selected members is already marked as bot
const hasBot = selectedMembers.value.some((member) => member.attributes?.isBot?.default);
if (hasBot) {
return {
icon: 'robot',
copy: 'Unmark as bot',
value: false,
};
}
return {
icon: 'robot',
copy: 'Mark as bot',
value: true,
};
});
const handleMergeMembers = async () => {
const [firstMember, secondMember] = selectedMembers.value;
const { loadingMessage, apiErrorMessage } = useMemberMergeMessage;
loadingMessage();
return MemberService.merge(firstMember, secondMember)
.then(() => {
ToastStore.closeAll();
ToastStore.info(
"We're finalizing profiles merging. We will let you know once the process is completed.",
{
title: 'Profiles merging in progress',
},
);
})
.catch((error) => {
apiErrorMessage({ error });
});
};
const doDestroyAllWithConfirm = () => ConfirmDialog({
type: 'danger',
title: 'Delete profile',
message:
"Are you sure you want to proceed? You can't undo this action",
confirmButtonText: 'Confirm',
cancelButtonText: 'Cancel',
icon: 'fa-trash-can fa-light',
})
.then(() => {
trackEvent({
key: FeatureEventKey.DELETE_MEMBER,
type: EventType.FEATURE,
properties: {
path: route.path,
bulk: true,
},
});
const ids = selectedMembers.value.map((m) => m.id);
return MemberService.destroyAll(ids);
})
.then(async () => {
// Clear selection immediately to prevent UI issues
selectedMembers.value = [];
// Refresh data to ensure UI is up to date
await refreshMemberData();
});
const handleDoExport = async () => {
const ids = selectedMembers.value.map((i) => i.id);
const filter = {
id: {
in: ids,
},
};
try {
await showExportDialog({
badgeContent: pluralize('person', selectedMembers.value.length, true),
});
trackEvent({
key: FeatureEventKey.EXPORT_MEMBERS,
type: EventType.FEATURE,
properties: {
path: route.path,
bulk: true,
},
});
await MemberService.export({
filter,
orderBy: `${filters.value.order.prop}_${filters.value.order.order === 'descending' ? 'DESC' : 'ASC'}`,
limit: 0,
offset: null,
});
await getUser();
ToastStore.success(
'CSV download link will be sent to your e-mail',
);
} catch (error) {
console.error(error);
if (error !== 'cancel') {
ToastStore.error(
'An error has occurred while trying to export the CSV file. Please try again',
{
title: 'CSV Export failed',
},
);
}
}
};
const handleEditAttribute = async () => {
bulkAttributesUpdateVisible.value = true;
};
const doMarkAsTeamMember = async (value) => {
ToastStore.info('People are being updated');
const updatePromises = selectedMembers.value.map(async (member) => {
// Fetch member with all attributes to prevent data loss
const memberWithAllAttributes = await fetchMemberWithAllAttributes(member.id);
const currentAttributes = memberWithAllAttributes.attributes;
const updatedAttributes = {
...currentAttributes,
isTeamMember: {
default: value,
custom: value,
},
};
return MemberService.update(member.id, {
attributes: updatedAttributes,
invalidateCache: true,
});
});
return Promise.all(updatePromises)
.then(async () => {
ToastStore.closeAll();
ToastStore.success(`${
pluralize('Person', selectedMembers.value.length, true)} updated successfully`);
// Clear selection immediately to prevent UI issues
selectedMembers.value = [];
// Refresh data to ensure UI is up to date
await refreshMemberData();
})
.catch(() => {
ToastStore.closeAll();
ToastStore.error('Error updating people');
});
};
const doMarkAsBot = async (value) => {
ToastStore.info('People are being updated');
const updatePromises = selectedMembers.value.map(async (member) => {
// Fetch member with all attributes to prevent data loss
const memberWithAllAttributes = await fetchMemberWithAllAttributes(member.id);
const currentAttributes = memberWithAllAttributes.attributes;
const updatedAttributes = {
...currentAttributes,
isBot: {
...currentAttributes.isBot,
default: value,
custom: value,
},
};
return MemberService.update(member.id, {
attributes: updatedAttributes,
invalidateCache: true,
});
});
return Promise.all(updatePromises)
.then(async () => {
ToastStore.closeAll();
ToastStore.success(`${
pluralize('Person', selectedMembers.value.length, true)} updated successfully`);
// Clear selection immediately to prevent UI issues
selectedMembers.value = [];
// Refresh data to ensure UI is up to date
await refreshMemberData();
})
.catch(() => {
ToastStore.closeAll();
ToastStore.error('Error updating people');
});
};
const handleCommand = async (command) => {
if (command.action === 'markAsTeamMember') {
trackEvent({
key: FeatureEventKey.MARK_AS_TEAM_MEMBER,
type: EventType.FEATURE,
properties: {
path: route.path,
bulk: true,
},
});
await doMarkAsTeamMember(command.value);
} else if (command.action === 'markAsBot') {
trackEvent({
key: FeatureEventKey.MARK_AS_BOT,
type: EventType.FEATURE,
properties: {
path: route.path,
bulk: true,
},
});
await doMarkAsBot(command.value);
} else if (command.action === 'export') {
await handleDoExport();
} else if (command.action === 'mergeMembers') {
await handleMergeMembers();
} else if (command.action === 'editAttribute') {
await handleEditAttribute();
} else if (command.action === 'destroyAll') {
await doDestroyAllWithConfirm();
}
};
</script>
<script>
export default {
name: 'AppMemberListToolbar',
};
</script>