|
24 | 24 | <PInputText |
25 | 25 | placeholder="Search accounts" |
26 | 26 | data-cy="search-members-field" |
27 | | - v-model="searchByName" |
| 27 | + v-model="search" |
28 | 28 | class="w-full" |
29 | 29 | @input="onSearch" |
30 | 30 | /> |
|
44 | 44 | :first="(options.page - 1) * options.itemsPerPage" |
45 | 45 | :sort-field="options.sortBy[0]" |
46 | 46 | :sort-order="options.sortDesc[0] ? -1 : 1" |
| 47 | + :rowHover="true" |
47 | 48 | removableSort |
48 | 49 | reorderable-columns |
49 | 50 | @page="onPage" |
50 | | - @row-click="rowClick" |
51 | 51 | @sort="onSort" |
52 | 52 | data-cy="accounts-table" |
53 | 53 | > |
54 | 54 | <template v-for="header in headers" :key="header.field"> |
55 | 55 | <PColumn |
56 | | - v-if="header.field === 'username'" |
57 | 56 | :field="header.field" |
58 | 57 | :header="header.header" |
59 | 58 | :sortable="header.sortable" |
60 | 59 | > |
61 | | - <template #body="slotProps"> |
| 60 | + <template #body="{ data }"> |
62 | 61 | <router-link |
63 | | - class="title-t4" |
64 | | - :to="{ |
65 | | - name: 'account', |
66 | | - params: { username: slotProps.data.username } |
67 | | - }" |
| 62 | + :to="accountRoute(data)" |
| 63 | + class="dt-row-link" |
| 64 | + :class="header.class" |
68 | 65 | > |
69 | | - {{ slotProps.data.username }} |
| 66 | + <template v-if="header.field === 'active'">{{ |
| 67 | + fieldValue(data, header.field) ? 'Active' : 'Inactive' |
| 68 | + }}</template> |
| 69 | + <template v-else>{{ |
| 70 | + fieldValue(data, header.field) |
| 71 | + }}</template> |
70 | 72 | </router-link> |
71 | 73 | </template> |
72 | 74 | </PColumn> |
73 | | - <PColumn |
74 | | - v-else-if="header.field === 'active'" |
75 | | - :header="header.header" |
76 | | - :field="header.field" |
77 | | - > |
78 | | - <template #body="slotProps"> |
79 | | - <i v-if="slotProps.data.active" class="ti ti-check" /> |
80 | | - <i v-else class="ti ti-x" /> |
81 | | - </template> |
82 | | - </PColumn> |
83 | | - <PColumn |
84 | | - v-else |
85 | | - :field="header.field" |
86 | | - :header="header.header" |
87 | | - :sortable="header.sortable" |
88 | | - ></PColumn> |
89 | 75 | </template> |
90 | 76 | <template #paginatorstart> |
91 | 77 | <PButton |
|
106 | 92 | <script lang="ts"> |
107 | 93 | import { |
108 | 94 | PaginatedUsersParams, |
109 | | - useDialogStore, |
110 | 95 | TableDataHeader, |
| 96 | + useDataTableSearch, |
| 97 | + useDialogStore, |
111 | 98 | AppContainer, |
112 | 99 | AppSection |
113 | 100 | } from '@mergin/lib' |
114 | | -import debounce from 'lodash/debounce' |
115 | | -import { mapActions, mapState } from 'pinia' |
116 | | -import { |
117 | | - DataTablePageEvent, |
118 | | - DataTableRowClickEvent, |
119 | | - DataTableSortEvent |
120 | | -} from 'primevue/datatable' |
| 101 | +import get from 'lodash/get' |
| 102 | +import { mapState } from 'pinia' |
121 | 103 | import { defineComponent } from 'vue' |
122 | 104 |
|
123 | 105 | import { AdminRoutes } from '@/modules' |
124 | 106 | import CreateUserForm from '@/modules/admin/components/CreateUserForm.vue' |
125 | 107 | import { useAdminStore } from '@/modules/admin/store' |
126 | 108 |
|
| 109 | +const headers: TableDataHeader[] = [ |
| 110 | + { |
| 111 | + field: 'username', |
| 112 | + header: 'Username', |
| 113 | + sortable: true, |
| 114 | + linked: true, |
| 115 | + class: 'title-t4' |
| 116 | + }, |
| 117 | + { field: 'email', header: 'Email', sortable: true, linked: true }, |
| 118 | + { field: 'profile.name', header: 'Full name', linked: true }, |
| 119 | + { field: 'active', header: 'Active', linked: true } |
| 120 | +] |
| 121 | +
|
127 | 122 | export default defineComponent({ |
128 | 123 | name: 'AccountsTable', |
129 | 124 | components: { |
130 | 125 | AppContainer, |
131 | 126 | AppSection |
132 | 127 | }, |
133 | | - data() { |
| 128 | + setup() { |
| 129 | + const adminStore = useAdminStore() |
| 130 | + const dialogStore = useDialogStore() |
| 131 | +
|
| 132 | + const tableSearch = useDataTableSearch({ |
| 133 | + defaultSortBy: 'username', |
| 134 | + defaultSortDesc: false |
| 135 | + }) |
| 136 | +
|
| 137 | + tableSearch.setFetchFn((signal) => { |
| 138 | + const { options, search } = tableSearch |
| 139 | + const params: PaginatedUsersParams = { |
| 140 | + page: options.page, |
| 141 | + per_page: options.itemsPerPage |
| 142 | + } |
| 143 | + if (options.sortBy[0]) { |
| 144 | + params.descending = options.sortDesc[0] |
| 145 | + params.order_by = options.sortBy[0] |
| 146 | + } |
| 147 | + if (search.value) params.like = search.value.trim() |
| 148 | + adminStore.fetchUsers({ params, signal }) |
| 149 | + }) |
| 150 | +
|
134 | 151 | return { |
135 | | - options: { |
136 | | - sortBy: ['username'], |
137 | | - sortDesc: [false], |
138 | | - itemsPerPage: 20, |
139 | | - page: 1, |
140 | | - perPageOptions: [20, 50, 100] |
141 | | - }, |
142 | | - searchByName: '', |
143 | | - headers: [ |
144 | | - { field: 'username', header: 'Username', sortable: true }, |
145 | | - { field: 'email', header: 'Email', sortable: true }, |
146 | | - { field: 'profile.name', header: 'Full name' }, |
147 | | - { field: 'active', header: 'Active' } |
148 | | - ] as TableDataHeader[] |
| 152 | + ...tableSearch, |
| 153 | + show: dialogStore.show.bind(dialogStore), |
| 154 | + headers |
149 | 155 | } |
150 | 156 | }, |
151 | 157 | computed: { |
152 | 158 | ...mapState(useAdminStore, ['users', 'loading']) |
153 | 159 | }, |
154 | 160 | created() { |
155 | | - this.resetPaging = debounce(this.resetPaging, 1000) |
156 | | - this.fetchUsers({ params: this.getParams() }) |
| 161 | + this.initFromQuery() |
| 162 | + this.doFetch() |
157 | 163 | }, |
158 | 164 | methods: { |
159 | | - ...mapActions(useAdminStore, ['fetchUsers']), |
160 | | - ...mapActions(useDialogStore, ['show']), |
161 | | -
|
162 | | - onSearch() { |
163 | | - this.resetPaging() |
164 | | - this.fetchUsers({ params: this.getParams() }) |
| 165 | + fieldValue(data: unknown, field: string) { |
| 166 | + return get(data, field) |
165 | 167 | }, |
166 | 168 |
|
167 | | - async resetPaging() { |
168 | | - this.options.page = 1 |
169 | | - }, |
170 | | -
|
171 | | - getParams(): PaginatedUsersParams { |
172 | | - const params = { |
173 | | - page: this.options.page, |
174 | | - per_page: this.options.itemsPerPage |
175 | | - } as PaginatedUsersParams |
176 | | - if (this.options.sortBy[0]) { |
177 | | - params.descending = this.options.sortDesc[0] |
178 | | - params.order_by = this.options.sortBy[0] |
179 | | - } |
180 | | - if (this.searchByName) { |
181 | | - params.like = this.searchByName.trim() |
182 | | - } |
183 | | - return params |
184 | | - }, |
185 | | -
|
186 | | - onRefresh() { |
187 | | - this.fetchUsers({ params: this.getParams() }) |
188 | | - }, |
189 | | -
|
190 | | - onPage(event: DataTablePageEvent) { |
191 | | - this.options.page = event.page + 1 |
192 | | - this.options.itemsPerPage = event.rows |
193 | | - this.fetchUsers({ params: this.getParams() }) |
194 | | - }, |
195 | | -
|
196 | | - onSort(event: DataTableSortEvent) { |
197 | | - this.options.sortBy[0] = event.sortField?.toString() |
198 | | - this.options.sortDesc[0] = event.sortOrder < 1 |
199 | | - this.fetchUsers({ params: this.getParams() }) |
200 | | - }, |
201 | | -
|
202 | | - rowClick(event: DataTableRowClickEvent) { |
203 | | - this.$router.push({ |
204 | | - name: AdminRoutes.ACCOUNT, |
205 | | - params: { username: event.data.username } |
206 | | - }) |
| 169 | + accountRoute(data) { |
| 170 | + return { name: AdminRoutes.ACCOUNT, params: { username: data.username } } |
207 | 171 | }, |
208 | 172 |
|
209 | 173 | createUserDialog() { |
210 | 174 | const dialog = { maxWidth: 500, header: 'Create user' } |
211 | 175 | const listeners = { |
212 | 176 | success: () => { |
213 | | - this.resetPaging() |
214 | | - this.fetchUsers({ params: this.getParams() }) |
| 177 | + this.options.page = 1 |
| 178 | + this.doFetch() |
215 | 179 | } |
216 | 180 | } |
217 | 181 | this.show({ |
|
0 commit comments