-
-
Notifications
You must be signed in to change notification settings - Fork 444
Expand file tree
/
Copy pathClientTable.vue
More file actions
231 lines (222 loc) · 8.25 KB
/
ClientTable.vue
File metadata and controls
231 lines (222 loc) · 8.25 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
<script setup lang="ts">
import SecondaryButton from '@/packages/ui/src/Buttons/SecondaryButton.vue';
import { UserCircleIcon } from '@heroicons/vue/24/solid';
import {
ChevronUpDownIcon,
ChevronDownIcon,
ChevronUpIcon,
PlusIcon,
} from '@heroicons/vue/16/solid';
import { type Component, computed, h, ref, watchEffect } from 'vue';
import { type Client } from '@/packages/api/src';
import ClientCreateModal from '@/Components/Common/Client/ClientCreateModal.vue';
import { canCreateClients } from '@/utils/permissions';
const props = defineProps<{
clients: Client[];
}>();
const createClient = ref(false);
import {
FlexRender,
getCoreRowModel,
useVueTable,
createColumnHelper,
type SortingState,
getSortedRowModel,
} from '@tanstack/vue-table';
import { storeToRefs } from 'pinia';
import { useProjectsStore } from '@/utils/useProjects';
import { CheckCircleIcon } from '@heroicons/vue/20/solid';
import TableHeading from '@/Components/Common/TableHeading.vue';
import ClientMoreOptionsDropdown from '@/Components/Common/Client/ClientMoreOptionsDropdown.vue';
import { useClientsStore } from '@/utils/useClients';
import ClientEditModal from '@/Components/Common/Client/ClientEditModal.vue';
import TableRow from '@/Components/TableRow.vue';
import TableCell from '@/Components/TableCell.vue';
const columnHelper = createColumnHelper<Client>();
const { projects } = storeToRefs(useProjectsStore());
const columns = computed(() => [
columnHelper.accessor((row) => row.name, {
id: 'name',
cell: (info) => info.getValue(),
header: () => 'Name',
}),
columnHelper.accessor((row) => row, {
id: 'projects',
sortingFn: (a, b) => {
return (
projects.value.filter(
(projects) => projects.client_id === a.original.id
).length -
projects.value.filter(
(projects) => projects.client_id === b.original.id
).length
);
},
cell: (info) =>
h('div', {
innerHTML:
projects.value.filter(
(projects) => projects.client_id === info.getValue().id
).length + ' Projects',
}),
header: () => 'Projects',
}),
columnHelper.accessor((row) => row, {
id: 'status',
enableSorting: false,
cell: (info) =>
h(
'div',
{
class: 'flex space-x-1 items-center',
},
[
h(CheckCircleIcon, {
class: 'w-5',
}),
h('span', {
innerHTML: info.getValue().is_archived
? 'Archived'
: 'Active',
}),
]
),
header: () => 'Status',
}),
columnHelper.display({
id: 'actions',
cell: (info) => {
const showEditModal = ref(false);
return h(
'div',
{
class: 'flex space-x-1 items-center',
},
[
h(ClientEditModal, {
client: info.row.original,
show: showEditModal.value,
}),
h(ClientMoreOptionsDropdown, {
class: 'w-5',
client: info.row.original,
onEdit: () => (showEditModal.value = true),
onArchive: () => {
useClientsStore().updateClient(
info.row.original.id,
{
...info.row.original,
is_archived: !info.row.original.is_archived,
}
);
},
onDelete: () => {
useClientsStore().deleteClient(
info.row.original.id
);
},
}),
]
);
},
}),
]);
const data = ref(props.clients);
watchEffect(() => {
data.value = props.clients;
});
const table = useVueTable({
get data() {
return data.value;
},
onSortingChange: (updaterOrValue) => {
sorting.value =
typeof updaterOrValue === 'function'
? updaterOrValue(sorting.value)
: updaterOrValue;
},
getCoreRowModel: getCoreRowModel(),
getSortedRowModel: getSortedRowModel(),
state: {
get sorting() {
return sorting.value;
},
},
columns: columns.value,
});
const sorting = ref<SortingState>([]);
</script>
<template>
<ClientCreateModal v-model:show="createClient"></ClientCreateModal>
<div class="flow-root max-w-[100vw] overflow-x-auto">
<div class="inline-block min-w-full align-middle">
<div
data-testid="client_table"
class="grid min-w-full"
style="grid-template-columns: 1fr 150px 200px 80px">
<TableHeading>
<TableCell
v-for="header in table.getHeaderGroups()[0].headers"
:key="header.id"
:class="
header.column.getCanSort()
? 'cursor-pointer select-none'
: ''
"
@click="
header.column.getToggleSortingHandler()?.($event)
"
:cell="header">
<FlexRender
v-if="!header.isPlaceholder"
:render="header.column.columnDef.header"
:props="header.getContext()" />
<div class="px-1" v-if="header.column.getCanSort()">
<ChevronUpDownIcon
class="h-4 text-text-tertiary"
v-if="
header.column.getIsSorted() === false
"></ChevronUpDownIcon>
<ChevronDownIcon
class="h-4 text-accent-300"
v-if="
header.column.getIsSorted() === 'desc'
"></ChevronDownIcon>
<ChevronUpIcon
class="h-4 text-accent-300"
v-if="
header.column.getIsSorted() === 'asc'
"></ChevronUpIcon>
</div>
</TableCell>
</TableHeading>
<div
class="col-span-2 py-24 text-center"
v-if="clients.length === 0">
<UserCircleIcon
class="w-8 text-icon-default inline pb-2"></UserCircleIcon>
<h3 class="text-white font-semibold">No clients found</h3>
<p class="pb-5" v-if="canCreateClients()">
Create your first client now!
</p>
<SecondaryButton
v-if="canCreateClients()"
@click="createClient = true"
:icon="PlusIcon as Component"
>Create your First Client
</SecondaryButton>
</div>
<TableRow v-for="row in table.getRowModel().rows" :key="row.id">
<TableCell
v-for="cell in row.getVisibleCells()"
:key="cell.id"
:cell="cell">
<FlexRender
:render="cell.column.columnDef.cell"
:props="cell.getContext()" />
</TableCell>
</TableRow>
</div>
</div>
</div>
</template>