-
Notifications
You must be signed in to change notification settings - Fork 67
Expand file tree
/
Copy pathProjectAccessRequests.vue
More file actions
213 lines (206 loc) · 6.32 KB
/
ProjectAccessRequests.vue
File metadata and controls
213 lines (206 loc) · 6.32 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
<!--
Copyright (C) Lutra Consulting Limited
SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-MerginMaps-Commercial
-->
<template>
<PDataView
:value="accessRequests"
:lazy="true"
:paginator="accessRequestsCount > 10"
:loading="loading"
:rows="options.itemsPerPage"
:totalRecords="accessRequestsCount"
:data-key="'id'"
:paginator-template="'FirstPageLink PrevPageLink PageLinks NextPageLink LastPageLink'"
size="small"
@page="onPage"
>
<template #header>
<h3 class="font-semibold paragraph-p6 text-color m-0">Access requests</h3>
</template>
<template #list="slotProps">
<template v-for="item in slotProps.items" :key="item.id">
<!-- Row -->
<div
class="flex flex-column lg:flex-row align-items-center justify-content-between px-4 py-2 mt-0 border-bottom-1 border-gray-200"
>
<p class="w-12 lg:w-6 paragraph-p6 m-0">
User
<span class="font-semibold">{{ item.requested_by }}</span>
requested an access to your project
<span class="font-semibold">{{ item.project_name }}.</span>
</p>
<div
class="flex w-12 lg:w-4 align-items-center flex-wrap lg:flex-nowrap row-gap-2"
>
<p class="opacity-80 paragraph-p6 w-12">
<span v-tooltip.right="{ value: $filters.datetime(item.expire) }">
<template
v-if="$filters.remainingtime(item.expire) === 'expired'"
>Expired</template
>
<template v-else
>Expiring in
{{ $filters.remainingtime(item.expire) }}</template
>
</span>
</p>
<AppDropdown
:options="availablePermissions"
v-model="permissions[item.id]"
class="w-6 lg:w-5 lg:mr-2"
/>
<div class="flex justify-content-end w-6 lg:w-4">
<PButton
:disabled="!canCancelAccessRequest()"
icon="ti ti-x"
rounded
aria-label="Disallow"
severity="danger"
class="mr-2"
@click="cancelRequest(item)"
/>
<PButton
:disabled="!canAcceptAccessRequest(item.expire)"
icon="ti ti-check"
rounded
aria-label="Accept"
severity="success"
@click="acceptRequest(item)"
/>
</div>
</div>
</div>
</template>
</template>
<template #empty>
<p class="text-center p-4 m-0">No access requests found</p>
</template>
</PDataView>
</template>
<script lang="ts">
import { mapActions, mapState } from 'pinia'
import { DataViewPageEvent } from 'primevue/dataview'
import { defineComponent } from 'vue'
import AppDropdown from '@/common/components/AppDropdown.vue'
import { getErrorMessage } from '@/common/error_utils'
import { isAtLeastProjectRole, ProjectRole } from '@/common/permission_utils'
import { GetAccessRequestsPayload } from '@/modules'
import { useNotificationStore } from '@/modules/notification/store'
import { useProjectStore } from '@/modules/project/store'
import { useUserStore } from '@/modules/user/store'
export default defineComponent({
data() {
return {
loading: false,
options: {
// Default is order_params=requested_at ASC
sortBy: 'requested_at',
sortDesc: false,
itemsPerPage: 10,
page: 1
},
permissions: {}
}
},
components: { AppDropdown },
computed: {
...mapState(useProjectStore, [
'project',
'accessRequests',
'accessRequestsCount',
'availablePermissions'
]),
...mapState(useUserStore, ['loggedUser'])
},
created() {
this.fetchItems()
},
methods: {
...mapActions(useProjectStore, [
'cancelProjectAccessRequest',
'acceptProjectAccessRequest',
'getAccessRequests'
]),
...mapActions(useNotificationStore, ['error']),
onUpdateOptions(options) {
this.options = options
this.fetchItems()
},
/** Update pagination in case of last accepting / canceling feature */
async updatePaginationOrFetch() {
if (this.accessRequests.length === 1 && this.options.page > 1) {
this.options.page -= 1
return
}
await this.fetchItems()
},
canAcceptAccessRequest(expire: string) {
return (
!this.expired(expire) &&
isAtLeastProjectRole(this.project.role, ProjectRole.owner)
)
},
canCancelAccessRequest() {
return isAtLeastProjectRole(this.project.role, ProjectRole.owner)
},
async acceptRequest(request) {
try {
const data = {
permissions: this.permissions[request.id]
}
await this.acceptProjectAccessRequest({
data,
itemId: request.id,
workspace: this.project.namespace
})
await this.updatePaginationOrFetch()
} catch (err) {
this.error({
text: getErrorMessage(err, 'Failed to accept access request')
})
}
},
expired(expire) {
return Date.parse(expire) < Date.now()
},
async cancelRequest(request) {
await this.cancelProjectAccessRequest({
itemId: request.id,
workspace: this.project.namespace
})
await this.updatePaginationOrFetch()
},
async fetchItems() {
this.loading = true
try {
const payload: GetAccessRequestsPayload = {
namespace: this.project.namespace,
params: {
project_name: this.project.name,
page: this.options.page,
per_page: this.options.itemsPerPage,
order_params:
this.options.sortBy[0] &&
`${this.options.sortBy[0]} ${
this.options.sortDesc[0] ? 'DESC' : 'ASC'
}`
}
}
await this.getAccessRequests(payload)
this.accessRequests.forEach((request) => {
this.permissions[request.id] = 'read'
})
} finally {
this.loading = false
}
},
onPage(e: DataViewPageEvent) {
this.options.page = e.page + 1
this.options.itemsPerPage = e.rows
this.fetchItems()
}
}
})
</script>
<style lang="scss" scoped></style>