aboutsummaryrefslogtreecommitdiff
path: root/src/site/pages/dashboard/admin/settings.vue
blob: d2448a0d8872d2f5ca685e25da1510cce559f554 (plain) (blame)
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
<template>
	<section class="section is-fullheight dashboard">
		<div class="container">
			<div class="columns">
				<div class="column is-narrow">
					<Sidebar />
				</div>
				<div class="column">
					<h2 class="subtitle">
						Service settings
					</h2>
					<hr>

					<div v-for="[sectionName, fields] in Object.entries(sectionedSettings)" :key="sectionName" class="block">
						<h5 class="title is-5 has-text-grey-lighter">
							{{ sectionName }}
						</h5>
						<JoiObject ref="jois" :settings="fields" :errors="validationErrors" />
					</div>

					<div class="mb2 mt2 text-center">
						<button
							class="button is-primary"
							@click="promptRestartService">
							Save settings
						</button>
					</div>
				</div>
			</div>
		</div>
	</section>
</template>

<script>
import { mapState } from 'vuex';
import Sidebar from '~/components/sidebar/Sidebar.vue';
import JoiObject from '~/components/settings/JoiObject.vue';

export default {
	components: {
		Sidebar,
		JoiObject
	},
	middleware: ['auth', 'admin'],
	data() {
		return {
			validationErrors: {}
		};
	},
	computed: {
		...mapState({
			settings: state => state.admin.settings,
			settingsSchema: state => state.admin.settingsSchema
		}),
		sectionedSettings() {
			return Object.entries(this.settingsSchema.keys).reduce((acc, [key, field]) => {
				if (!field.metas) acc.Other = { ...acc.Other, [key]: field };

				const sectionMeta = field.metas.find(e => e.section);
				if (sectionMeta) {
					acc[sectionMeta.section] = { ...acc[sectionMeta.section], [key]: field };
				} else {
					acc.Other = { ...acc.Other, [key]: field };
				}

				return acc;
			}, {});
		}
	},
	async asyncData({ app }) {
		await app.store.dispatch('admin/fetchSettings');
		await app.store.dispatch('admin/getSettingsSchema');
		await app.store.commit('admin/populateSchemaWithValues');
	},
	methods: {
		promptRestartService() {
			this.$buefy.dialog.confirm({
				message: 'Keep in mind that restarting only works if you have PM2 or something similar set up. Continue?',
				onConfirm: () => this.saveSettings()
			});
		},
		async saveSettings() {
			// handle refs
			let settings = {};
			for (const joiComponent of this.$refs.jois) {
				settings = { ...settings, ...joiComponent.getValues() };
			}

			try {
				await this.$store.dispatch('admin/saveSettings', settings);
				this.$set(this, 'validationErrors', {});

				await this.$store.dispatch('config/fetchSettings');
				// this.$handler.executeAction('admin/restartService');
			} catch (e) {
				if (e.response?.data?.errors) {
					this.$set(this, 'validationErrors', e.response.data.errors);
				}
			}
		}
	},
	head() {
		return {
			title: 'Service settings'
		};
	}
};
</script>