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

AdminSettings.vue « src - github.com/nextcloud/files_retention.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a8a4999f48b245172adb5e61a9a5d93ad6c98e11 (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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<!--
  - SPDX-FileCopyrightText: Joas Schilling <coding@schilljs.com>
  - SPDX-License-Identifier: AGPL-3.0-only
  -->
<template>
	<NcSettingsSection :title="t('files_retention', 'File retention & automatic deletion')"
		:doc-url="docUrl"
		:description="t('files_retention', 'Define if files tagged with a specific tag should be deleted automatically after some time. This is useful for confidential documents.')">
		<table class="retention-rules-table">
			<thead>
				<tr>
					<th class="retention-heading__name">
						{{ t('files_retention', 'Files tagged with') }}
					</th>
					<th class="retention-heading__time">
						{{ t('files_retention','Retention time') }}
					</th>
					<th class="retention-heading__after">
						{{ t('files_retention','From date of') }}
					</th>
					<th class="retention-heading__action"></th>
				</tr>
			</thead>
			<tbody>
				<RetentionRule v-for="rule in retentionRules"
					:key="rule.id"
					v-bind="rule">
					{{ rule.tagid }}
				</RetentionRule>

				<tr>
					<td class="retention-rule__name">
						<NcMultiselectTags v-model="newTag"
							:disabled="loading"
							:multiple="false"
							:filter="filterAvailableTagList"
							:close-on-select="true" />
					</td>
					<td class="retention-rule__time">
						<NcTextField :value.sync="newAmount"
							:disabled="loading"
							type="text"
							:label="amountLabel"
							:placeholder="''" />
						<NcMultiselect v-model="newUnit"
							:disabled="loading"
							:options="unitOptions"
							:allow-empty="false"
							track-by="id"
							label="label"
							:close-on-select="true" />
					</td>
					<td class="retention-rule__after">
						<NcMultiselect v-model="newAfter"
							:disabled="loading"
							:options="afterOptions"
							:allow-empty="false"
							track-by="id"
							label="label"
							:close-on-select="true" />
					</td>
					<td class="retention-rule__action">
						<NcButton type="secondary"
							:disabled="loading"
							:aria-label="createLabel"
							@click="onClickCreate">
							<template #icon>
								<Plus :size="20" />
							</template>
							{{ t('files_retention', 'Create') }}
						</NcButton>
					</td>
				</tr>
			</tbody>
		</table>

		<NcCheckboxRadioSwitch type="switch"
			:checked="notifyBefore"
			:loading="loadingNotifyBefore"
			@update:checked="onToggleNotifyBefore">
			{{ t('files_retention', 'Notify owner a day before a file is automatically deleted') }}
		</NcCheckboxRadioSwitch>
	</NcSettingsSection>
</template>

<script>
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import NcCheckboxRadioSwitch from '@nextcloud/vue/dist/Components/NcCheckboxRadioSwitch.js'
import NcMultiselect from '@nextcloud/vue/dist/Components/NcMultiselect.js'
import NcMultiselectTags from '@nextcloud/vue/dist/Components/NcMultiselectTags.js'
import Plus from 'vue-material-design-icons/Plus.vue'
import NcSettingsSection from '@nextcloud/vue/dist/Components/NcSettingsSection.js'
import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'

import RetentionRule from './Components/RetentionRule.vue'

import { showError, showSuccess, showWarning } from '@nextcloud/dialogs'
import { loadState } from '@nextcloud/initial-state'

export default {
	name: 'AdminSettings',

	components: {
		NcButton,
		NcCheckboxRadioSwitch,
		NcMultiselect,
		NcMultiselectTags,
		Plus,
		RetentionRule,
		NcSettingsSection,
		NcTextField,
	},

	data() {
		return {
			loading: true,
			loadingNotifyBefore: false,
			notifyBefore: loadState('files_retention', 'notify_before'),
			docUrl: loadState('files_retention', 'doc-url'),

			unitOptions: [
				{ id: 0, label: t('files_retention', 'Days') },
				{ id: 1, label: t('files_retention', 'Weeks') },
				{ id: 2, label: t('files_retention', 'Months') },
				{ id: 3, label: t('files_retention', 'Years') },
			],
			newUnit: 0,

			afterOptions: [
				{ id: 0, label: t('files_retention', 'Creation') },
				{ id: 1, label: t('files_retention', 'Last modification') },
			],
			newAfter: 0,

			newAmount: '14', // FIXME TextField does not accept numbers …

			newTag: -1,
			tagOptions: [],
			filterAvailableTagList: (tag) => {
				return !this.tagIdsWithRule.includes(tag.id)
			},
		}
	},

	computed: {
		retentionRules() {
			return this.$store.getters.getRetentionRules()
		},

		tagIdsWithRule() {
			return this.$store.getters.getTagIdsWithRule()
		},

		amountLabel() {
			return t('files_retention', 'Number of days, weeks, months or years after which the files should be deleted')
		},

		createLabel() {
			return t('files_retention', 'Create new retention rule')
		},
	},

	async mounted() {
		try {
			await OC.SystemTags.collection.fetch({})
			await this.$store.dispatch('loadRetentionRules')

			this.loading = false
		} catch (e) {
			showError(t('files_retention', 'An error occurred while loading the existing retention rules'))
			console.error(e)
		}
	},

	methods: {
		onToggleNotifyBefore() {
			this.loadingNotifyBefore = true
			const newNotifyBefore = !this.notifyBefore

			OCP.AppConfig.setValue(
				'files_retention',
				'notify_before',
				newNotifyBefore ? 'yes' : 'no',
				{
					success() {
						if (newNotifyBefore) {
							showSuccess(t('files_retention', 'Users are now notified one day before a file or folder is being deleted'))
						} else {
							showWarning(t('files_retention', 'Users are no longer notified before a file or folder is being deleted'))
						}

						this.loadingNotifyBefore = false
						this.notifyBefore = newNotifyBefore
					},
					error() {
						this.loadingNotifyBefore = false
						showError(t('files_retention', 'An error occurred while changing the setting'))
					},
				}
			)
		},

		async onClickCreate() {
			// When the value is unchanged, the Multiselect component returns the initial ID
			// Otherwise the entry from this.unitOptions
			const newTag = this.newTag?.id ?? this.newTag
			const newUnit = this.newUnit?.id ?? this.newUnit
			const newAfter = this.newAfter?.id ?? this.newAfter
			const newAmount = parseInt(this.newAmount, 10)

			if (newTag < 0) {
				showError(t('files_retention', 'Invalid tag selected'))
				return
			}

			const tagName = OC.SystemTags.collection.get(newTag)?.attributes?.name

			if (this.tagIdsWithRule.includes(newTag)) {
				showError(t('files_retention', 'Tag {tagName} already has a retention rule', { tagName }))
				return
			}

			if (newUnit < 0 || newUnit > 3) {
				showError(t('files_retention', 'Invalid unit option'))
				return
			}

			if (newAfter < 0 || newAfter > 1) {
				showError(t('files_retention', 'Invalid action option'))
				return
			}

			if (isNaN(newAmount) || newAmount < 1) {
				showError(t('files_retention', 'Invalid retention time'))
				return
			}

			try {
				await this.$store.dispatch('createNewRule', {
					tagid: newTag,
					timeamount: newAmount,
					timeunit: newUnit,
					timeafter: newAfter,
				})

				showSuccess(t('files_retention', 'Retention rule for tag {tagName} saved', { tagName }))
				this.resetForm()
			} catch (e) {
				showError(t('files_retention', 'Failed to save retention rule for tag {tagName}', { tagName }))
				console.error(e)
			}
		},

		resetForm() {
			this.newTag = -1
			this.newAmount = '14'
			this.newUnit = 0
			this.newAfter = 0
		},
	},
}
</script>

<style scoped lang="scss">
.retention-rules-table {
	width: 100%;
	min-height: 50px;
	padding-top: 5px;
	max-width: 580px;

	.retention-heading,
	.retention-rule {
		&__name,
		&__time,
		&__after,
		&__active,
		&__action {
			color: var(--color-text-maxcontrast);
			padding: 10px 10px 10px 0;
		}

		&__time {
			text-align: center;
			min-width: 260px;
		}

		&__action {
			padding-left: 10px;
			flex-direction: row-reverse;
			display: flex;
		}
	}

	.retention-heading {
		&__name,
		&__time,
		&__after,
		&__active,
		&__action {
			padding-left: 13px;
		}
	}

	.retention-rule {
		&__time {
			> div {
				width: 49%;
				min-width: 0;
				display: inline-block;
			}

			::v-deep .input-field__input {
				text-align: right;
			}
		}
	}
}
</style>