Completed
Pull Request — master (#290)
by korelstar
30:35
created

src/NotesService.js (51 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
import store from './store'
0 ignored issues
show
'import' is only available in ES6 (use 'esversion: 6').

Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code.

Further Reading:

Loading history...
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
2
import axios from 'nextcloud-axios'
0 ignored issues
show
'import' is only available in ES6 (use 'esversion: 6').

Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code.

Further Reading:

Loading history...
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
3
4
export default {
0 ignored issues
show
'export' is only available in ES6 (use 'esversion: 6').

Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code.

Further Reading:

Loading history...
5
6
	url(url) {
0 ignored issues
show
Backwards Compatibility introduced by
'concise methods' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
Loading history...
7
		url = `/apps/notes${url}`
0 ignored issues
show
'template literal syntax' is only available in ES6 (use 'esversion: 6').

Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code.

Further Reading:

Loading history...
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
8
		return OC.generateUrl(url)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
9
	},
10
11
	setSettings(settings) {
0 ignored issues
show
Backwards Compatibility introduced by
'concise methods' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
Loading history...
12
		return axios
13
			.put(this.url('/settings'), settings)
14
			.then(response => {
0 ignored issues
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code.

Further Reading:

Loading history...
15
				let settings = response.data
0 ignored issues
show
Backwards Compatibility introduced by
'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
Loading history...
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
16
				store.commit('setSettings', settings)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
17
				return settings
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
18
			})
19
			.catch(err => {
0 ignored issues
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code.

Further Reading:

Loading history...
20
				console.error(err)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
21
				// TODO error handling
22
			})
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
23
	},
24
25
	fetchNotes() {
0 ignored issues
show
Backwards Compatibility introduced by
'concise methods' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
Loading history...
26
		return axios
27
			.get(this.url('/notes'))
28
			.then(response => {
0 ignored issues
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code.

Further Reading:

Loading history...
29
				store.commit('setSettings', response.data.settings)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
30
				store.dispatch('addAll', response.data.notes)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
31
				if (response.data.errorMessage) {
32
					OC.Notification.showTemporary(response.data.errorMessage)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
33
				}
34
				return response.data
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
35
			})
36
			.catch(err => {
0 ignored issues
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code.

Further Reading:

Loading history...
37
				console.error(err)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
38
				// TODO error handling
39
			})
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
40
	},
41
42
	fetchNote(noteId) {
0 ignored issues
show
Backwards Compatibility introduced by
'concise methods' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
Loading history...
43
		return axios
44
			.get(this.url('/notes/' + noteId))
45
			.then(response => {
0 ignored issues
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code.

Further Reading:

Loading history...
46
				store.commit('add', response.data)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
47
				return response.data
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
48
			})
49
			.catch(err => {
0 ignored issues
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code.

Further Reading:

Loading history...
50
				console.error(err)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
51
				// TODO error handling
52
			})
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
53
	},
54
55
	noteExists(noteId) {
0 ignored issues
show
Backwards Compatibility introduced by
'concise methods' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
Loading history...
56
		return store.getters.noteExists(noteId)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
57
	},
58
59
	createNote(category) {
0 ignored issues
show
Backwards Compatibility introduced by
'concise methods' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
Loading history...
60
		return axios
61
			.post(this.url('/notes'), { category: category })
62
			.then(response => {
0 ignored issues
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code.

Further Reading:

Loading history...
63
				store.commit('add', response.data)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
64
				return response.data
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
65
			})
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
66
	},
67
68
	updateNote(note) {
0 ignored issues
show
Backwards Compatibility introduced by
'concise methods' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
Loading history...
69
		return axios
70
			.put(this.url('/notes/' + note.id), { content: note.content })
71
			.then(response => {
0 ignored issues
show
'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

Generally using ECMAScript 6 specific syntax is fine if you are sure that it is already supported by all engines which are supposed to run this code.

Further Reading:

Loading history...
72
				let updated = response.data
0 ignored issues
show
Backwards Compatibility introduced by
'let' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).
Loading history...
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
73
				note.error = false
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
74
				note.title = updated.title
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
75
				note.modified = updated.modified
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
76
				if (updated.content === note.content) {
77
					note.unsaved = false
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
78
				}
79
				store.commit('add', note)
0 ignored issues
show
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
There were too many errors found in this file; checking aborted after 47%.

If JSHint finds too many errors in a file, it aborts checking altogether because it suspects a configuration issue.

Further Reading:

Loading history...
80
				return note
81
			})
82
			.catch(err => {
83
				console.error(err)
84
				note.error = true
85
				// TODO error handling
86
			})
87
	},
88
89
	deleteNote(noteId) {
90
		return axios
91
			.delete(this.url('/notes/' + noteId))
92
			.then(() => {
93
				store.commit('remove', noteId)
94
			})
95
	},
96
97
	setFavorite(noteId, favorite) {
98
		return axios
99
			.put(this.url('/notes/' + noteId + '/favorite'), { favorite: favorite })
100
			.then(response => {
101
				store.commit('setNoteAttribute', { noteId: noteId, attribute: 'favorite', value: response.data })
102
			})
103
			.catch(err => {
104
				console.error(err)
105
				// TODO error handling
106
			})
107
	},
108
109
	setCategory(noteId, category) {
110
		return axios
111
			.put(this.url('/notes/' + noteId + '/category'), { category: category })
112
			.then(response => {
113
				let realCategory = response.data
114
				if (category !== realCategory) {
115
					OC.Notification.showTemporary(
116
						t('notes', 'Updating the note\'s category has failed. Is the target directory writable?')
117
					)
118
				}
119
				store.commit('setNoteAttribute', { noteId: noteId, attribute: 'category', value: realCategory })
120
			})
121
			.catch(err => {
122
				console.error(err)
123
				// TODO error handling
124
			})
125
	},
126
127
	saveNote(noteId, manualSave = false) {
128
		store.commit('addUnsaved', noteId)
129
		if (manualSave) {
130
			store.commit('setManualSave', true)
131
		}
132
		this._saveNotes()
133
	},
134
	_saveNotes() {
135
		let unsaved = store.state.unsaved
136
		let keys = Object.keys(unsaved)
137
		if (store.state.isSaving || keys.length === 0) {
138
			return
139
		}
140
		store.commit('setSaving', true)
141
		let promises = []
142
		for (let i = 0; i < keys.length; i++) {
143
			let note = unsaved[keys[i]]
144
			promises.push(this.updateNote(note))
145
		}
146
		store.commit('clearUnsaved')
147
		Promise.all(promises).finally(() => {
148
			store.commit('setSaving', false)
149
			store.commit('setManualSave', false)
150
			this._saveNotes()
151
		})
152
	},
153
154
	saveNoteManually(noteId) {
155
		store.commit('setNoteAttribute', { noteId: noteId, attribute: 'error', value: false })
156
		this.saveNote(noteId, true)
157
	},
158
159
	getCategories(maxLevel, details) {
160
		return store.getters.getCategories(maxLevel, details)
161
	},
162
163
	categoryLabel(category) {
164
		return category === '' ? t('notes', 'Uncategorized') : category.replace(/\//g, ' / ')
165
	},
166
}
167