-
Notifications
You must be signed in to change notification settings - Fork 20
/
PersonalSettings.vue
284 lines (267 loc) · 8.53 KB
/
PersonalSettings.vue
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
<template>
<div id="openproject_prefs" class="section">
<SettingsTitle />
<p v-if="!showOAuth && !connected" class="settings-hint">
{{ t('integration_openproject', 'To get your API access token yourself, go to the "Access token" section of your OpenProject account settings page.') }}
</p>
<div id="openproject-content">
<div id="toggle-openproject-navigation-link">
<input
id="openproject-link"
type="checkbox"
class="checkbox"
:checked="state.navigation_enabled"
@input="onNavigationChange">
<label for="openproject-link">{{ t('integration_openproject', 'Enable navigation link') }}</label>
</div>
<br><br>
<p v-if="isInsecureUrl" class="settings-hint">
<span class="icon icon-alert-outline" />
{{ t('integration_openproject', 'Warning, connecting to your OpenProject instance via HTTP is insecure.') }}
</p>
<div class="openproject-grid-form">
<label for="openproject-url">
<a class="icon icon-link" />
{{ t('integration_openproject', 'OpenProject instance address') }}
</label>
<input id="openproject-url"
v-model="state.url"
type="text"
:disabled="connected === true"
:placeholder="t('integration_openproject', 'https://my.openproject.org')"
@input="onInput">
<label v-show="!showOAuth"
for="openproject-token">
<a class="icon icon-category-auth" />
{{ t('integration_openproject', 'Access token') }}
</label>
<input v-show="!showOAuth"
id="openproject-token"
v-model="state.token"
type="password"
:disabled="connected === true"
:placeholder="t('integration_openproject', 'OpenProject access token')"
@input="onInput">
</div>
<button v-if="showOAuth && !connected"
id="openproject-oauth"
:disabled="loading === true"
:class="{ loading }"
@click="onOAuthClick">
<span class="icon icon-external" />
{{ t('integration_openproject', 'Connect to OpenProject') }}
</button>
<div v-if="connected" class="openproject-grid-form">
<label class="openproject-connected">
<a class="icon icon-checkmark-color" />
{{ t('integration_openproject', 'Connected as {user}', { user: state.user_name }) }}
</label>
<button id="openproject-rm-cred" @click="onLogoutClick">
<span class="icon icon-close" />
{{ t('integration_openproject', 'Disconnect from OpenProject') }}
</button>
</div>
<div v-if="connected" id="openproject-search-block">
<input
id="search-openproject"
type="checkbox"
class="checkbox"
:checked="state.search_enabled"
@input="onSearchChange">
<label for="search-openproject">{{ t('integration_openproject', 'Enable unified search for tickets') }}</label>
<br><br>
<p v-if="state.search_enabled" class="settings-hint">
<span class="icon icon-details" />
{{ t('integration_openproject', 'Warning, everything you type in the search bar will be sent to your OpenProject instance.') }}
</p>
<input
id="notification-openproject"
type="checkbox"
class="checkbox"
:checked="state.notification_enabled"
@input="onNotificationChange">
<label for="notification-openproject">{{ t('integration_openproject', 'Enable notifications for activity in my work packages') }}</label>
</div>
</div>
</div>
</template>
<script>
import { loadState } from '@nextcloud/initial-state'
import { generateUrl } from '@nextcloud/router'
import axios from '@nextcloud/axios'
import { delay } from '../utils'
import { showSuccess, showError } from '@nextcloud/dialogs'
import '@nextcloud/dialogs/styles/toast.scss'
import SettingsTitle from '../components/settings/SettingsTitle'
export default {
name: 'PersonalSettings',
components: {
SettingsTitle,
},
props: [],
data() {
return {
state: loadState('integration_openproject', 'user-config'),
initialToken: loadState('integration_openproject', 'user-config').token,
loading: false,
redirect_uri: window.location.protocol + '//' + window.location.host + generateUrl('/apps/integration_openproject/oauth-redirect'),
}
},
computed: {
isInsecureUrl() {
return !this.state.url.startsWith('https://')
},
showOAuth() {
return this.state.url.replace(/\/+$/, '') === this.state.oauth_instance_url.replace(/\/+$/, '')
&& this.state.client_id
&& this.state.client_secret
},
connected() {
return this.state.token && this.state.token !== ''
&& this.state.url && this.state.url !== ''
&& this.state.user_name && this.state.user_name !== ''
},
},
mounted() {
const paramString = window.location.search.substr(1)
// eslint-disable-next-line
const urlParams = new URLSearchParams(paramString)
const zmToken = urlParams.get('openprojectToken')
if (zmToken === 'success') {
showSuccess(t('integration_openproject', 'Successfully connected to OpenProject!'))
} else if (zmToken === 'error') {
showError(t('integration_openproject', 'OAuth access token could not be obtained:') + ' ' + urlParams.get('message'))
}
},
methods: {
onLogoutClick() {
this.state.token = ''
this.saveOptions({ token: this.state.token, token_type: '' })
},
onNotificationChange(e) {
this.state.notification_enabled = e.target.checked
this.saveOptions({ notification_enabled: this.state.notification_enabled ? '1' : '0' })
},
onSearchChange(e) {
this.state.search_enabled = e.target.checked
this.saveOptions({ search_enabled: this.state.search_enabled ? '1' : '0' })
},
onNavigationChange(e) {
this.state.navigation_enabled = e.target.checked
this.saveOptions({ navigation_enabled: this.state.navigation_enabled ? '1' : '0' })
},
onInput() {
this.loading = true
delay(() => {
if (!this.state.url.startsWith('https://') && !this.state.url.startsWith('http://')) {
this.state.url = 'https://' + this.state.url
}
const pattern = /^https?:\/\/[^ "]+$/
if (pattern.test(this.state.url)) {
this.saveOptions({
url: this.state.url,
token: this.state.token,
token_type: this.showOAuth ? 'oauth' : 'access',
})
} else {
this.saveOptions({
url: '',
token: this.state.token,
token_type: this.showOAuth ? 'oauth' : 'access',
})
}
}, 2000)()
},
saveOptions(values) {
const req = {
values,
}
const url = generateUrl('/apps/integration_openproject/config')
axios.put(url, req)
.then((response) => {
showSuccess(t('integration_openproject', 'OpenProject options saved'))
if (response.data.user_name !== undefined) {
this.state.user_name = response.data.user_name
if (this.state.token && response.data.user_name === '') {
showError(t('integration_openproject', 'Incorrect access token'))
}
}
})
.catch((error) => {
console.debug(error.response)
const msg = error.response?.data?.errorMessage === 'Invalid token'
? t('integration_openproject', 'Invalid token')
: error.response?.data?.errorMessage === 'Not found'
? t('integration_openproject', 'OpenProject instance not found')
: error.response?.request?.responseText
showError(
t('integration_openproject', 'Failed to save OpenProject options')
+ ': ' + msg
)
})
.then(() => {
this.loading = false
})
},
onOAuthClick() {
const oauthState = Math.random().toString(36).substring(3)
const requestUrl = this.state.url + '/oauth/authorize'
+ '?client_id=' + encodeURIComponent(this.state.client_id)
+ '&redirect_uri=' + encodeURIComponent(this.redirect_uri)
+ '&response_type=code'
+ '&state=' + encodeURIComponent(oauthState)
const req = {
values: {
oauth_state: oauthState,
redirect_uri: this.redirect_uri,
},
}
const url = generateUrl('/apps/integration_openproject/config')
axios.put(url, req)
.then((response) => {
window.location.replace(requestUrl)
})
.catch((error) => {
showError(
t('integration_openproject', 'Failed to save OpenProject OAuth state')
+ ': ' + error.response.request.responseText
)
})
.then(() => {
})
},
},
}
</script>
<style scoped lang="scss">
#openproject-search-block {
margin-top: 30px;
}
.openproject-grid-form label {
line-height: 38px;
}
.openproject-grid-form input {
width: 100%;
}
.openproject-grid-form {
max-width: 600px;
display: grid;
grid-template: 1fr / 1fr 1fr;
button .icon {
margin-bottom: -1px;
}
}
#openproject_prefs .icon {
display: inline-block;
width: 32px;
}
#openproject_prefs .grid-form .icon {
margin-bottom: -3px;
}
#openproject-content {
margin-left: 40px;
}
#openproject-search-block .icon {
width: 22px;
}
</style>