Completed
Pull Request — master (#290)
by korelstar
30:58
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 AppGlobal from './mixins/AppGlobal'
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 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...
3
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...
4
5
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...
6
7
	tn: AppGlobal.methods.tn,
8
9
	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...
10
		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...
11
		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...
12
	},
13
14
	handleSyncError(message) {
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...
15
		OC.Notification.showTemporary(message + ' ' + this.tn('See JavaScript console for details.'))
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...
16
	},
17
18
	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...
19
		return axios
20
			.put(this.url('/settings'), settings)
21
			.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...
22
				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...
23
				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...
24
				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...
25
			})
26
			.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...
27
				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...
28
				this.handleSyncError(this.tn('Updating settings has failed.'))
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...
29
				throw 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...
30
			})
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
	},
32
33
	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...
34
		return axios
35
			.get(this.url('/notes'))
36
			.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...
37
				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...
38
				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...
39
				if (response.data.errorMessage) {
40
					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...
41
				}
42
				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...
43
			})
44
			.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...
45
				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...
46
				this.handleSyncError(this.tn('Fetching notes has failed.'))
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
				throw 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...
48
			})
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...
49
	},
50
51
	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...
52
		return axios
53
			.get(this.url('/notes/' + noteId))
54
			.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...
55
				let localNote = store.getters.getNote(parseInt(noteId))
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...
56
				// only overwrite if there are no unsaved changes
57
				if (!localNote || !localNote.unsaved) {
58
					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...
59
				}
60
				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...
61
			})
62
			.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...
63
				if (err.response.status === 404) {
64
					throw 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...
65
				} else {
66
					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...
67
					let msg = this.tn('Fetching note {id} has failed.', { id: noteId })
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...
68
					store.commit('setNoteAttribute', { noteId: noteId, attribute: 'error', value: true })
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...
69
					store.commit('setNoteAttribute', { noteId: noteId, attribute: 'errorMessage', value: msg })
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...
70
					return store.getter.getNote(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...
71
				}
72
			})
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 36%.

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

Further Reading:

Loading history...
73
	},
74
75
	createNote(category) {
76
		return axios
77
			.post(this.url('/notes'), { category: category })
78
			.then(response => {
79
				store.commit('add', response.data)
80
				return response.data
81
			})
82
			.catch(err => {
83
				console.error(err)
84
				this.handleSyncError(this.tn('Creating new note has failed.'))
85
				throw err
86
			})
87
	},
88
89
	_updateNote(note) {
90
		return axios
91
			.put(this.url('/notes/' + note.id), { content: note.content })
92
			.then(response => {
93
				let updated = response.data
94
				note.saveError = false
95
				note.title = updated.title
96
				note.modified = updated.modified
97
				if (updated.content === note.content) {
98
					note.unsaved = false
99
				}
100
				store.commit('add', note)
101
				return note
102
			})
103
			.catch(err => {
104
				store.commit('setNoteAttribute', { noteId: note.id, attribute: 'saveError', value: true })
105
				console.error(err)
106
				this.handleSyncError(this.tn('Updating note {id} has failed.', { id: note.id }))
107
			})
108
	},
109
110
	deleteNote(noteId) {
111
		return axios
112
			.delete(this.url('/notes/' + noteId))
113
			.then(() => {
114
				store.commit('remove', noteId)
115
			})
116
			.catch(err => {
117
				console.error(err)
118
				this.handleSyncError(this.tn('Deleting note {id} has failed.', { id: noteId }))
119
				throw err
120
			})
121
	},
122
123
	setFavorite(noteId, favorite) {
124
		return axios
125
			.put(this.url('/notes/' + noteId + '/favorite'), { favorite: favorite })
126
			.then(response => {
127
				store.commit('setNoteAttribute', { noteId: noteId, attribute: 'favorite', value: response.data })
128
			})
129
			.catch(err => {
130
				console.error(err)
131
				this.handleSyncError(this.tn('Toggling favorite for note {id} has failed.', { id: noteId }))
132
				throw err
133
			})
134
	},
135
136
	setCategory(noteId, category) {
137
		return axios
138
			.put(this.url('/notes/' + noteId + '/category'), { category: category })
139
			.then(response => {
140
				let realCategory = response.data
141
				if (category !== realCategory) {
142
					this.handleSyncError(this.tn('Updating the note\'s category has failed. Is the target directory writable?'))
143
				}
144
				store.commit('setNoteAttribute', { noteId: noteId, attribute: 'category', value: realCategory })
145
			})
146
			.catch(err => {
147
				console.error(err)
148
				this.handleSyncError(this.tn('Updating the category for note {id} has failed.', { id: noteId }))
149
				throw err
150
			})
151
	},
152
153
	saveNote(noteId, manualSave = false) {
154
		store.commit('addUnsaved', noteId)
155
		if (manualSave) {
156
			store.commit('setManualSave', true)
157
		}
158
		this._saveNotes()
159
	},
160
	_saveNotes() {
161
		let unsaved = store.state.unsaved
162
		let keys = Object.keys(unsaved)
163
		if (store.state.isSaving || keys.length === 0) {
164
			return
165
		}
166
		store.commit('setSaving', true)
167
		let promises = []
168
		for (let i = 0; i < keys.length; i++) {
169
			let note = unsaved[keys[i]]
170
			promises.push(this._updateNote(note))
171
		}
172
		store.commit('clearUnsaved')
173
		Promise.all(promises).finally(() => {
174
			store.commit('setSaving', false)
175
			store.commit('setManualSave', false)
176
			this._saveNotes()
177
		})
178
	},
179
180
	saveNoteManually(noteId) {
181
		store.commit('setNoteAttribute', { noteId: noteId, attribute: 'saveError', value: false })
182
		this.saveNote(noteId, true)
183
	},
184
185
	noteExists(noteId) {
186
		return store.getters.noteExists(noteId)
187
	},
188
189
	getCategories(maxLevel, details) {
190
		return store.getters.getCategories(maxLevel, details)
191
	},
192
193
	categoryLabel(category) {
194
		return category === '' ? this.tn('Uncategorized') : category.replace(/\//g, ' / ')
195
	},
196
}
197