From fd3f6de51a082dcd72c2ef557747e031ef7b9c4a Mon Sep 17 00:00:00 2001
From: Zephyrrus <krisztian1997@gmail.com>
Date: Thu, 9 Jul 2020 02:24:40 +0300
Subject: refactor: refactor most of the admin pages to use the store instead
 of internal states

---
 src/site/pages/dashboard/admin/user/_id.vue | 66 ++++++++++++++++-------------
 1 file changed, 37 insertions(+), 29 deletions(-)

(limited to 'src/site/pages/dashboard/admin/user/_id.vue')

diff --git a/src/site/pages/dashboard/admin/user/_id.vue b/src/site/pages/dashboard/admin/user/_id.vue
index 1755b89..7814468 100644
--- a/src/site/pages/dashboard/admin/user/_id.vue
+++ b/src/site/pages/dashboard/admin/user/_id.vue
@@ -41,20 +41,27 @@
 					<b-field
 						label="Files"
 						horizontal>
-						<span>{{ files.length }}</span>
+						<span>{{ user.files.length }}</span>
 					</b-field>
 
 					<div class="mb2 mt2 text-center">
-						<button
-							class="button is-danger"
+						<b-button
+							v-if="user.enabled"
+							type="is-danger"
 							@click="promptDisableUser">
 							Disable user
-						</button>
+						</b-button>
+						<b-button
+							v-if="!user.enabled"
+							type="is-success"
+							@click="promptEnableUser">
+							Enable user
+						</b-button>
 					</div>
 
 					<Grid
-						v-if="files.length"
-						:files="files" />
+						v-if="user.files.length"
+						:files="user.files" />
 				</div>
 			</div>
 		</div>
@@ -62,6 +69,7 @@
 </template>
 
 <script>
+import { mapState } from 'vuex';
 import Sidebar from '~/components/sidebar/Sidebar.vue';
 import Grid from '~/components/grid/Grid.vue';
 
@@ -70,42 +78,42 @@ export default {
 		Sidebar,
 		Grid,
 	},
-	middleware: ['auth', 'admin'],
+	middleware: ['auth', 'admin', ({ route, store }) => {
+		try {
+			store.dispatch('admin/fetchUser', route.params.id);
+		} catch (e) {
+			// eslint-disable-next-line no-console
+			console.error(e);
+		}
+	}],
 	data() {
 		return {
 			options: {},
-			files: null,
-			user: null,
 		};
 	},
-	async asyncData({ $axios, route }) {
-		try {
-			const response = await $axios.$get(`/admin/users/${route.params.id}`);
-			return {
-				files: response.files ? response.files : null,
-				user: response.user ? response.user : null,
-			};
-		} catch (error) {
-			console.error(error);
-			return {
-				files: null,
-				user: null,
-			};
-		}
-	},
+	computed: mapState({
+		user: (state) => state.admin.user,
+	}),
 	methods: {
 		promptDisableUser() {
 			this.$buefy.dialog.confirm({
 				type: 'is-danger',
-				message: 'Are you sure you want to disable the account of the user that uploaded this file?',
+				message: 'Are you sure you want to disable the account of this user?',
 				onConfirm: () => this.disableUser(),
 			});
 		},
-		async disableUser() {
-			const response = await this.$axios.$post('admin/users/disable', {
-				id: this.user.id,
+		promptEnableUser() {
+			this.$buefy.dialog.confirm({
+				type: 'is-danger',
+				message: 'Are you sure you want to enable the account of this user?',
+				onConfirm: () => this.enableUser(),
 			});
-			this.$buefy.toast.open(response.message);
+		},
+		disableUser() {
+			this.$handler.executeAction('admin/disableUser', this.user.id);
+		},
+		enableUser() {
+			this.$handler.executeAction('admin/enableUser', this.user.id);
 		},
 	},
 };
-- 
cgit v1.2.3

