-
Notifications
You must be signed in to change notification settings - Fork 19
/
SearchInput.vue
219 lines (215 loc) · 5.67 KB
/
SearchInput.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
<template>
<div id="searchBar">
<Multiselect ref="workPackageMultiSelect"
class="searchInput"
:placeholder="placeholder"
:options="filterSearchResultsByFileId"
:user-select="true"
label="displayName"
track-by="id"
:internal-search="false"
open-direction="below"
:loading="isStateLoading"
:preselect-first="true"
:preserve-search="true"
@search-change="asyncFind"
@change="linkWorkPackageToFile">
<template #option="{option}">
<WorkPackage :key="option.id"
:workpackage="option" />
</template>
<template #noOptions>
{{ noOptionsText }}
</template>
</Multiselect>
<div v-if="!isStateOk"
class="stateMsg text-center">
{{ stateMessages }}
</div>
</div>
</template>
<script>
import debounce from 'lodash/debounce'
import axios from '@nextcloud/axios'
import { generateUrl } from '@nextcloud/router'
import Multiselect from '@nextcloud/vue/dist/Components/Multiselect'
import WorkPackage from './WorkPackage'
import { showError, showSuccess } from '@nextcloud/dialogs'
import '@nextcloud/dialogs/styles/toast.scss'
import { workpackageHelper } from '../../utils/workpackageHelper'
import { STATE } from '../../utils'
const SEARCH_CHAR_LIMIT = 1
const DEBOUNCE_THRESHOLD = 500
export default {
name: 'SearchInput',
components: {
Multiselect,
WorkPackage,
},
props: {
fileInfo: {
type: Object,
required: true,
},
linkedWorkPackages: {
type: Array,
required: true,
},
},
data: () => ({
state: STATE.OK,
searchResults: [],
noOptionsText: t('integration_openproject', 'Start typing to search'),
placeholder: t('integration_openproject', 'Search for a work package to create a relation'),
}),
computed: {
isStateOk() {
return this.state === STATE.OK
},
isStateLoading() {
return this.state === STATE.LOADING
},
stateMessages() {
if (this.state === STATE.NO_TOKEN) {
return t('integration_openproject', 'No OpenProject account connected')
} else if (this.state === STATE.ERROR) {
return t('integration_openproject', 'Error connecting to OpenProject')
}
return ''
},
filterSearchResultsByFileId() {
return this.searchResults.filter(wp => {
if (wp.fileId === undefined || wp.fileId === '') {
console.error('work-package data does not contain a fileId')
return false
}
return wp.fileId === this.fileInfo.id
})
},
},
watch: {
fileInfo(oldFile, newFile) {
if (oldFile.id !== newFile.id) {
this.resetState()
this.emptySearchInput()
}
},
},
methods: {
emptySearchInput() {
// FIXME: https://github.com/shentao/vue-multiselect/issues/633
if (this.$refs.workPackageMultiSelect?.$refs?.VueMultiselect?.search) {
this.$refs.workPackageMultiSelect.$refs.VueMultiselect.search = ''
}
},
resetState() {
this.searchResults = []
this.state = STATE.OK
},
checkForErrorCode(statusCode) {
if (statusCode === 200) return
if (statusCode === 401) {
this.state = STATE.NO_TOKEN
} else {
this.state = STATE.ERROR
}
},
async asyncFind(query) {
this.resetState()
await this.debounceMakeSearchRequest(query, this.fileInfo.id)
},
debounceMakeSearchRequest: debounce(function(...args) {
if (args[0].length < SEARCH_CHAR_LIMIT) return
return this.makeSearchRequest(...args)
}, DEBOUNCE_THRESHOLD),
async linkWorkPackageToFile(selectedOption) {
const params = new URLSearchParams()
params.append('workpackageId', selectedOption.id)
params.append('fileId', this.fileInfo.id)
params.append('fileName', this.fileInfo.name)
const config = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
}
const url = generateUrl('/apps/integration_openproject/work-packages')
try {
await axios.post(url, params, config)
this.$emit('saved', selectedOption)
showSuccess(
t('integration_openproject', 'Work package linked successfully!')
)
this.resetState()
this.emptySearchInput()
} catch (e) {
showError(
t('integration_openproject', 'Failed to link file to work package')
)
}
},
async makeSearchRequest(search, fileId) {
this.state = STATE.LOADING
const url = generateUrl('/apps/integration_openproject/work-packages')
const req = {}
req.params = {
searchQuery: search,
}
let response
try {
response = await axios.get(url, req)
} catch (e) {
response = e.response
}
this.checkForErrorCode(response.status)
if (response.status === 200) await this.processWorkPackages(response.data, fileId)
if (this.isStateLoading) this.state = STATE.OK
},
async processWorkPackages(workPackages, fileId) {
for (let workPackage of workPackages) {
try {
if (this.isStateLoading) {
workPackage.fileId = fileId
workPackage = await workpackageHelper.getAdditionalMetaData(workPackage)
const alreadyLinked = this.linkedWorkPackages.some(el => el.id === workPackage.id)
const alreadyInSearchResults = this.searchResults.some(el => el.id === workPackage.id)
// check the state again, it might have changed in between
if (!alreadyInSearchResults && !alreadyLinked && this.isStateLoading) {
this.searchResults.push(workPackage)
}
}
} catch (e) {
console.error('could not process work package data')
}
}
},
},
}
</script>
<style lang="scss">
#searchBar {
padding: 10px;
position: sticky;
position: -webkit-sticky; /* Safari */
z-index: 1;
top: 0;
background: var(--color-main-background);
.searchInput {
width: 100%;
}
.stateMsg {
padding: 30px;
text-align: center;
}
.multiselect {
.multiselect__content-wrapper {
.multiselect__content {
.multiselect__element {
span {
padding: 0 !important;
}
}
}
}
}
}
</style>