Welcome to mirror list, hosted at ThFree Co, Russian Federation.

AppDataSection.vue « components « src - github.com/nextcloud/backup.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 97611b78070ff3b582f488e28903d9297a689c60 (plain)
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
<!--
  - @copyright Copyright (c) 2021 Louis Chemineau <louis@chmn.me>
  -
  - @author Louis Chemineau <louis@chmn.me>
  -
  - @license GNU AGPL version 3 or any later version
  -
  - This program is free software: you can redistribute it and/or modify
  - it under the terms of the GNU Affero General Public License as
  - published by the Free Software Foundation, either version 3 of the
  - License, or (at your option) any later version.
  -
  - This program is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU Affero General Public License for more details.
  -
  - You should have received a copy of the GNU Affero General Public License
  - along with this program. If not, see <http://www.gnu.org/licenses/>.
  -
  -->

<template>
	<SettingsSection :title="t('backup', 'App Data')"
		:description="t('backup', 'Choose where the backup app will initially store the restoring points.')">
		<form ref="app-data-form" class="app-data__form">
			<select v-model="appDataForm.storageId"
				class="app-data__form__select"
				name="storageId"
				:disabled="loadingAppData">
				<template v-if="appDataForm !== undefined">
					<option v-for="external in externalStoragesWithLocal"
						:key="external.storageId"
						:selected="external.storageId === appDataForm.storageId"
						:value="external.storageId">
						{{ external.storage }}
					</option>
				</template>
			</select>
			<input v-model="appDataForm.root"
				class="app-data__form__input"
				type="text"
				:placeholder="t('backup', 'Path in which to store the data. (ex: app_data)')"
				:disabled="loadingAppData || appDataForm.storageId === 0"
				name="root">
			<button class="primary"
				:disabled="loadingAppData || !formIsTouched()"
				@click.prevent="showSetAppDataPopup = true">
				{{ t('backup', 'Set as App Data') }}
			</button>

			<span v-if="loadingAppData" class="icon-loading" />
			<WindowClose v-else-if="error"
				slot="icon"
				fill-color="#e9322d"
				:title="t('backup', 'Error')" />
		</form>

		<Modal v-if="showSetAppDataPopup"
			size="large"
			@close="!showSetAppDataPopup">
			<div class="app-data__set-popup">
				<div class="app-data__set-popup__header">
					{{ t('backup', "App Data change.") }}
				</div>
				<div class="app-data__set-popup__content">
					{{ t('backup', 'Changing the App Data will delete the data stored in the previous one including restoring points.') }}

					<CheckboxRadioSwitch :loading="loadingSetAppData" :checked.sync="validationCheckboxGorSetAppData">
						{{ t('backup', 'I understand some data will be deleted.') }}
					</CheckboxRadioSwitch>
				</div>
				<div class="app-data__set-popup__actions">
					<button @click="showSetAppDataPopup = false">
						Cancel
					</button>
					<button class="primary"
						:class="{loading: loadingSetAppData}"
						:disabled="!validationCheckboxGorSetAppData || loadingSetAppData"
						@click="setAppData">
						{{ t('backup', 'Change the App Data') }}
					</button>
				</div>
			</div>
		</Modal>
	</SettingsSection>
</template>

<script>
import WindowClose from 'vue-material-design-icons/WindowClose.vue'

import axios from '@nextcloud/axios'
import { generateOcsUrl } from '@nextcloud/router'
import { showSuccess, showError } from '@nextcloud/dialogs'
import Modal from '@nextcloud/vue/dist/Components/Modal'
import CheckboxRadioSwitch from '@nextcloud/vue/dist/Components/CheckboxRadioSwitch'

import SettingsSection from '@nextcloud/vue/dist/Components/SettingsSection'

import logger from '../logger'

/**
 * @typedef {object} ExternalLocation
 * @property {number} storageId - The ID of the external storage.
 * @property {string} storage - The description of the external storage.
 * @property {string} root - The path where the restoring points will be stored.
 */

export default {
	name: 'AppDataSection',
	components: {
		SettingsSection,
		WindowClose,
		Modal,
		CheckboxRadioSwitch,
	},

	props: {
		externalStorages: {
			type: Array,
			default: () => [],
		},
	},

	data() {
		return {
			/** @type {ExternalLocation} */
			appData: undefined,
			/** @type {ExternalLocation} */
			appDataForm: {
				storageId: 0,
				root: '',
			},
			loadingAppData: false,
			loadingSetAppData: false,
			showSetAppDataPopup: false,
			validationCheckboxGorSetAppData: false,
			error: false,
		}
	},

	computed: {
		/** @return {Array<ExternalLocation>} */
		externalStoragesWithLocal() {
			return [
				{
					storageId: 0,
					root: '',
					storage: t('backup', 'Local storage'),
				},
				...this.externalStorages,
			]
		},
	},

	async mounted() {
		this.fetchAppData()
	},

	methods: {
		async fetchAppData() {
			try {
				this.loadingAppData = true
				const response = await axios.get(generateOcsUrl('apps/backup/appdata'))
				this.appData = response.data.ocs.data
				this.appDataForm = { ...response.data.ocs.data }
			} catch (error) {
				showError(t('backup', 'Unable to fetch app data'))
				logger.error('An error occurred while fetching app data', error)
			} finally {
				this.loadingAppData = false
			}
		},

		/**
		 * Add a new external location based on the form.
		 */
		async displaySetAppDataPopup() {
			this.showSetAppDataPopup = true
		},

		/**
		 * Add a new external location based on the form.
		 */
		async setAppData() {
			if (this.loadingSetAppData) {
				return
			}

			try {
				this.error = false
				this.loadingSetAppData = true
				const response = await axios.post(generateOcsUrl('apps/backup/appdata'), this.appDataForm)
				this.appData = response.data.ocs.data
				this.appDataForm = { ...response.data.ocs.data }
				this.showSetAppDataPopup = false
				showSuccess(t('backup', 'App data has been set'))
			} catch (error) {
				this.error = true
				showError(t('backup', 'Unable to set app data'))
				logger.error('An error occurred while setting app data', error)
			} finally {
				this.loadingSetAppData = false
			}
		},

		/** @return {boolean} */
		formIsTouched() {
			if (this.appData === undefined) {
				return false
			}

			return this.appData.storageId !== this.appDataForm.storageId || this.appData.root !== this.appDataForm.root
		},
	},
}
</script>
<style lang="scss" scoped>

button.loading {
	color: transparent !important;

	&::after {
		height: 20px !important;
		width: 20px !important;
		margin: -12px 0 0 -12px !important;
	}

	.icon {
		background-image: none !important;
	}
}

.app-data {
	&__form {
		margin: 24px 0;
		display: flex;

		.icon-loading {
			margin-left: 8px;
		}

		& > * {
			margin-right: 8px;
		}

		&__select {
			width: 400px;
		}

		&__input {
			width: 400px;
		}
	}

	&__set-popup {
		padding: 16px;

		&__header {
			font-weight: bold;
			margin-bottom: 12px;
		}

		&__content {
			margin-bottom: 12px;
		}

		&__actions {
			display: flex;
			justify-content: end;

			button {
				margin: 0 8px;
			}
		}
	}
}
</style>