Skip to content

Commit 7dbe89f

Browse files
committed
fix(files): Add missing properties and fix Typescript errors in FileEntryName
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
1 parent dbd2bc7 commit 7dbe89f

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

apps/files/src/components/FileEntry/FileEntryName.vue

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,17 @@
5555
</template>
5656

5757
<script lang="ts">
58+
import type { Node, View } from '@nextcloud/files'
5859
import type { PropType } from 'vue'
5960
61+
import { showError, showSuccess } from '@nextcloud/dialogs'
6062
import { emit } from '@nextcloud/event-bus'
6163
import { FileType, NodeStatus, Permission } from '@nextcloud/files'
6264
import { loadState } from '@nextcloud/initial-state'
63-
import { showError, showSuccess } from '@nextcloud/dialogs'
6465
import { translate as t } from '@nextcloud/l10n'
6566
import axios from '@nextcloud/axios'
66-
import Vue from 'vue'
67+
import { isAxiosError } from 'axios'
68+
import Vue, { defineComponent } from 'vue'
6769
6870
import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'
6971
@@ -72,7 +74,7 @@ import logger from '../../logger.js'
7274
7375
const forbiddenCharacters = loadState<string[]>('files', 'forbiddenCharacters', [])
7476
75-
export default Vue.extend({
77+
export default defineComponent({
7678
name: 'FileEntryName',
7779
7880
components: {
@@ -114,6 +116,10 @@ export default Vue.extend({
114116
},
115117
116118
computed: {
119+
currentView(): View {
120+
return this.$navigation.active as View
121+
},
122+
117123
isRenaming() {
118124
return this.renamingStore.renamingNode === this.source
119125
},
@@ -201,7 +207,7 @@ export default Vue.extend({
201207
* input validity using browser's native validation.
202208
* @param event the keyup event
203209
*/
204-
checkInputValidity(event?: KeyboardEvent) {
210+
checkInputValidity(event: KeyboardEvent) {
205211
const input = event.target as HTMLInputElement
206212
const newName = this.newName.trim?.() || ''
207213
logger.debug('Checking input validity', { newName })
@@ -210,21 +216,26 @@ export default Vue.extend({
210216
input.setCustomValidity('')
211217
input.title = ''
212218
} catch (e) {
213-
input.setCustomValidity(e.message)
214-
input.title = e.message
219+
if (e instanceof Error) {
220+
input.setCustomValidity(e.message)
221+
input.title = e.message
222+
} else {
223+
input.setCustomValidity(t('files', 'Invalid file name'))
224+
}
215225
} finally {
216226
input.reportValidity()
217227
}
218228
},
219-
isFileNameValid(name) {
229+
230+
isFileNameValid(name: string) {
220231
const trimmedName = name.trim()
221232
if (trimmedName === '.' || trimmedName === '..') {
222233
throw new Error(t('files', '"{name}" is an invalid file name.', { name }))
223234
} else if (trimmedName.length === 0) {
224235
throw new Error(t('files', 'File name cannot be empty.'))
225236
} else if (trimmedName.indexOf('/') !== -1) {
226237
throw new Error(t('files', '"/" is not allowed inside a file name.'))
227-
} else if (trimmedName.match(OC.config.blacklist_files_regex)) {
238+
} else if (trimmedName.match(window.OC.config.blacklist_files_regex)) {
228239
throw new Error(t('files', '"{name}" is not an allowed filetype.', { name }))
229240
} else if (this.checkIfNodeExists(name)) {
230241
throw new Error(t('files', '{newName} already exists.', { newName: name }))
@@ -237,7 +248,8 @@ export default Vue.extend({
237248
238249
return true
239250
},
240-
checkIfNodeExists(name) {
251+
252+
checkIfNodeExists(name: string) {
241253
return this.nodes.find(node => node.basename === name && node !== this.source)
242254
},
243255
@@ -289,7 +301,6 @@ export default Vue.extend({
289301
}
290302
291303
// Set loading state
292-
this.loading = 'renaming'
293304
Vue.set(this.source, 'status', NodeStatus.LOADING)
294305
295306
// Update node
@@ -314,26 +325,27 @@ export default Vue.extend({
314325
// Reset the renaming store
315326
this.stopRenaming()
316327
this.$nextTick(() => {
317-
this.$refs.basename.focus()
328+
this.$refs.basename?.focus()
318329
})
319330
} catch (error) {
320331
logger.error('Error while renaming file', { error })
321332
this.source.rename(oldName)
322-
this.$refs.renameInput.focus()
323-
324-
// TODO: 409 means current folder does not exist, redirect ?
325-
if (error?.response?.status === 404) {
326-
showError(t('files', 'Could not rename "{oldName}", it does not exist any more', { oldName }))
327-
return
328-
} else if (error?.response?.status === 412) {
329-
showError(t('files', 'The name "{newName}" is already used in the folder "{dir}". Please choose a different name.', { newName, dir: this.currentDir }))
330-
return
333+
this.$refs.renameInput?.focus()
334+
335+
if (isAxiosError(error)) {
336+
// TODO: 409 means current folder does not exist, redirect ?
337+
if (error?.response?.status === 404) {
338+
showError(t('files', 'Could not rename "{oldName}", it does not exist any more', { oldName }))
339+
return
340+
} else if (error?.response?.status === 412) {
341+
showError(t('files', 'The name "{newName}" is already used in the folder "{dir}". Please choose a different name.', { newName, dir: this.currentDir }))
342+
return
343+
}
331344
}
332345
333346
// Unknown error
334347
showError(t('files', 'Could not rename "{oldName}"', { oldName }))
335348
} finally {
336-
this.loading = false
337349
Vue.set(this.source, 'status', undefined)
338350
}
339351
},

0 commit comments

Comments
 (0)