aboutsummaryrefslogtreecommitdiff
path: root/src/site/store
diff options
context:
space:
mode:
authorZephyrrus <[email protected]>2020-07-09 02:24:40 +0300
committerZephyrrus <[email protected]>2020-07-09 02:24:40 +0300
commitfd3f6de51a082dcd72c2ef557747e031ef7b9c4a (patch)
tree78e655286f6984b171604f3bc15e41eb52b01cef /src/site/store
parentfix: register handler as a plugin (diff)
downloadhost.fuwn.me-fd3f6de51a082dcd72c2ef557747e031ef7b9c4a.tar.xz
host.fuwn.me-fd3f6de51a082dcd72c2ef557747e031ef7b9c4a.zip
refactor: refactor most of the admin pages to use the store instead of internal states
Diffstat (limited to 'src/site/store')
-rw-r--r--src/site/store/admin.js93
-rw-r--r--src/site/store/alert.js18
-rw-r--r--src/site/store/auth.js10
-rw-r--r--src/site/store/images.js2
4 files changed, 115 insertions, 8 deletions
diff --git a/src/site/store/admin.js b/src/site/store/admin.js
index e69de29..2586a18 100644
--- a/src/site/store/admin.js
+++ b/src/site/store/admin.js
@@ -0,0 +1,93 @@
+export const state = () => ({
+ users: [],
+ user: {
+ id: null,
+ username: null,
+ enabled: false,
+ createdAt: null,
+ editedAt: null,
+ apiKeyEditedAt: null,
+ isAdmin: null,
+ files: [],
+ },
+ file: {},
+ settings: {},
+});
+
+export const actions = {
+ async fetchUsers({ commit }) {
+ const response = await this.$axios.$get('admin/users');
+ commit('setUsers', response);
+
+ return response;
+ },
+ async fetchUser({ commit }, id) {
+ const response = await this.$axios.$get(`/admin/users/${id}`);
+ commit('setUserInfo', response);
+
+ return response;
+ },
+ async enableUser({ commit }, id) {
+ const response = await this.$axios.$post('admin/users/enable', { id });
+
+ commit('changeUserState', { userId: id, enabled: true });
+
+ return response;
+ },
+ async disableUser({ commit }, id) {
+ const response = await this.$axios.$post('admin/users/disable', { id });
+
+ commit('changeUserState', { userId: id, enabled: false });
+
+ return response;
+ },
+ async promoteUser({ commit }, id) {
+ const response = await this.$axios.$post('admin/users/promote', { id });
+
+ commit('changeUserState', { userId: id, isAdmin: true });
+
+ return response;
+ },
+ async demoteUser({ commit }, id) {
+ const response = await this.$axios.$post('admin/users/demote', { id });
+
+ commit('changeUserState', { userId: id, isAdmin: false });
+
+ return response;
+ },
+ async purgeUserFiles(_, id) {
+ const response = await this.$axios.$post('admin/users/purge', { id });
+
+ return response;
+ },
+};
+
+export const mutations = {
+ setUsers(state, { users }) {
+ state.users = users;
+ },
+ setUserInfo(state, { user, files }) {
+ state.user = { ...state.user, ...user };
+ state.user.files = files || [];
+ },
+ changeUserState(state, { userId, enabled, isAdmin }) {
+ const foundIndex = state.users.findIndex(({ id }) => id === userId);
+ if (foundIndex > -1) {
+ if (enabled !== undefined) {
+ state.users[foundIndex].enabled = enabled;
+ }
+ if (isAdmin !== undefined) {
+ state.users[foundIndex].isAdmin = isAdmin;
+ }
+ }
+
+ if (state.user.id === userId) {
+ if (enabled !== undefined) {
+ state.user.enabled = enabled;
+ }
+ if (isAdmin !== undefined) {
+ state.user.isAdmin = isAdmin;
+ }
+ }
+ },
+};
diff --git a/src/site/store/alert.js b/src/site/store/alert.js
index ff38e09..580dcc8 100644
--- a/src/site/store/alert.js
+++ b/src/site/store/alert.js
@@ -1,12 +1,19 @@
+import AlertTypes from '~/constants/alertTypes';
+
const getDefaultState = () => ({
- text: null,
- error: false,
+ message: null,
+ type: null,
+ snackbar: false,
});
export const state = getDefaultState;
export const actions = {
set({ commit }, data) {
+ // Only exists for backwards compatibility, remove one day
+ if (data.error === true) data.type = AlertTypes.ERROR;
+ if (data.text !== undefined) data.message = data.text;
+
commit('set', data);
},
clear({ commit }) {
@@ -15,9 +22,10 @@ export const actions = {
};
export const mutations = {
- set(state, { text, error }) {
- state.text = text;
- state.error = error;
+ set(state, { message, type, snackbar }) {
+ state.message = message;
+ state.type = type;
+ state.snackbar = snackbar || false;
},
clear(state) {
Object.assign(state, getDefaultState());
diff --git a/src/site/store/auth.js b/src/site/store/auth.js
index fcc051b..465de7d 100644
--- a/src/site/store/auth.js
+++ b/src/site/store/auth.js
@@ -30,6 +30,12 @@ export const actions = {
commit('setToken', data.token);
commit('loginSuccess', { token: data.token, user: data.user });
},
+ async register(_, { username, password }) {
+ return this.$axios.$post('auth/register', {
+ username,
+ password,
+ });
+ },
async fetchCurrentUser({ commit, dispatch }) {
try {
const data = await this.$axios.$get('users/me');
@@ -83,13 +89,13 @@ export const mutations = {
state.isLoading = true;
},
loginSuccess(state, { user }) {
- this.$cookies.set('token', state.token);
+ this.$cookies.set('token', state.token, { path: '/' });
state.user = user;
state.loggedIn = true;
state.isLoading = false;
},
logout(state) {
- this.$cookies.remove('token');
+ this.$cookies.remove('token', { path: '/' });
// reset state to default
Object.assign(state, getDefaultState());
},
diff --git a/src/site/store/images.js b/src/site/store/images.js
index 3019d85..a7581e0 100644
--- a/src/site/store/images.js
+++ b/src/site/store/images.js
@@ -10,7 +10,7 @@ export const getDefaultState = () => ({
},
name: null,
downloadEnabled: false,
- filesAlbums: {},
+ filesAlbums: {}, // map of file ids with a list of album objects the file is in
});
export const state = getDefaultState;