♻ Move project source files to top level from src, update Sentry dependency (#630)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
60
frontend/src/store/admin/actions.ts
Normal file
60
frontend/src/store/admin/actions.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { api } from '@/api';
|
||||
import { ActionContext } from 'vuex';
|
||||
import { IUserProfileCreate, IUserProfileUpdate } from '@/interfaces';
|
||||
import { State } from '../state';
|
||||
import { AdminState } from './state';
|
||||
import { getStoreAccessors } from 'typesafe-vuex';
|
||||
import { commitSetUsers, commitSetUser } from './mutations';
|
||||
import { dispatchCheckApiError } from '../main/actions';
|
||||
import { commitAddNotification, commitRemoveNotification } from '../main/mutations';
|
||||
|
||||
type MainContext = ActionContext<AdminState, State>;
|
||||
|
||||
export const actions = {
|
||||
async actionGetUsers(context: MainContext) {
|
||||
try {
|
||||
const response = await api.getUsers(context.rootState.main.token);
|
||||
if (response) {
|
||||
commitSetUsers(context, response.data);
|
||||
}
|
||||
} catch (error) {
|
||||
await dispatchCheckApiError(context, error);
|
||||
}
|
||||
},
|
||||
async actionUpdateUser(context: MainContext, payload: { id: number, user: IUserProfileUpdate }) {
|
||||
try {
|
||||
const loadingNotification = { content: 'saving', showProgress: true };
|
||||
commitAddNotification(context, loadingNotification);
|
||||
const response = (await Promise.all([
|
||||
api.updateUser(context.rootState.main.token, payload.id, payload.user),
|
||||
await new Promise((resolve, reject) => setTimeout(() => resolve(), 500)),
|
||||
]))[0];
|
||||
commitSetUser(context, response.data);
|
||||
commitRemoveNotification(context, loadingNotification);
|
||||
commitAddNotification(context, { content: 'User successfully updated', color: 'success' });
|
||||
} catch (error) {
|
||||
await dispatchCheckApiError(context, error);
|
||||
}
|
||||
},
|
||||
async actionCreateUser(context: MainContext, payload: IUserProfileCreate) {
|
||||
try {
|
||||
const loadingNotification = { content: 'saving', showProgress: true };
|
||||
commitAddNotification(context, loadingNotification);
|
||||
const response = (await Promise.all([
|
||||
api.createUser(context.rootState.main.token, payload),
|
||||
await new Promise((resolve, reject) => setTimeout(() => resolve(), 500)),
|
||||
]))[0];
|
||||
commitSetUser(context, response.data);
|
||||
commitRemoveNotification(context, loadingNotification);
|
||||
commitAddNotification(context, { content: 'User successfully created', color: 'success' });
|
||||
} catch (error) {
|
||||
await dispatchCheckApiError(context, error);
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const { dispatch } = getStoreAccessors<AdminState, State>('');
|
||||
|
||||
export const dispatchCreateUser = dispatch(actions.actionCreateUser);
|
||||
export const dispatchGetUsers = dispatch(actions.actionGetUsers);
|
||||
export const dispatchUpdateUser = dispatch(actions.actionUpdateUser);
|
||||
18
frontend/src/store/admin/getters.ts
Normal file
18
frontend/src/store/admin/getters.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { AdminState } from './state';
|
||||
import { getStoreAccessors } from 'typesafe-vuex';
|
||||
import { State } from '../state';
|
||||
|
||||
export const getters = {
|
||||
adminUsers: (state: AdminState) => state.users,
|
||||
adminOneUser: (state: AdminState) => (userId: number) => {
|
||||
const filteredUsers = state.users.filter((user) => user.id === userId);
|
||||
if (filteredUsers.length > 0) {
|
||||
return { ...filteredUsers[0] };
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const { read } = getStoreAccessors<AdminState, State>('');
|
||||
|
||||
export const readAdminOneUser = read(getters.adminOneUser);
|
||||
export const readAdminUsers = read(getters.adminUsers);
|
||||
15
frontend/src/store/admin/index.ts
Normal file
15
frontend/src/store/admin/index.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { mutations } from './mutations';
|
||||
import { getters } from './getters';
|
||||
import { actions } from './actions';
|
||||
import { AdminState } from './state';
|
||||
|
||||
const defaultState: AdminState = {
|
||||
users: [],
|
||||
};
|
||||
|
||||
export const adminModule = {
|
||||
state: defaultState,
|
||||
mutations,
|
||||
actions,
|
||||
getters,
|
||||
};
|
||||
20
frontend/src/store/admin/mutations.ts
Normal file
20
frontend/src/store/admin/mutations.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { IUserProfile } from '@/interfaces';
|
||||
import { AdminState } from './state';
|
||||
import { getStoreAccessors } from 'typesafe-vuex';
|
||||
import { State } from '../state';
|
||||
|
||||
export const mutations = {
|
||||
setUsers(state: AdminState, payload: IUserProfile[]) {
|
||||
state.users = payload;
|
||||
},
|
||||
setUser(state: AdminState, payload: IUserProfile) {
|
||||
const users = state.users.filter((user: IUserProfile) => user.id !== payload.id);
|
||||
users.push(payload);
|
||||
state.users = users;
|
||||
},
|
||||
};
|
||||
|
||||
const { commit } = getStoreAccessors<AdminState, State>('');
|
||||
|
||||
export const commitSetUser = commit(mutations.setUser);
|
||||
export const commitSetUsers = commit(mutations.setUsers);
|
||||
5
frontend/src/store/admin/state.ts
Normal file
5
frontend/src/store/admin/state.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { IUserProfile } from '@/interfaces';
|
||||
|
||||
export interface AdminState {
|
||||
users: IUserProfile[];
|
||||
}
|
||||
Reference in New Issue
Block a user