mirror of
https://github.com/HangarMC/Hangar.git
synced 2024-12-09 06:32:43 +08:00
63 lines
2.4 KiB
TypeScript
63 lines
2.4 KiB
TypeScript
import { ActionTree, GetterTree, MutationTree } from 'vuex';
|
|
import { Context } from '@nuxt/types';
|
|
import { IPermission, IProjectCategory } from 'hangar-api';
|
|
import { NamedPermission, ProjectCategory } from '~/types/enums';
|
|
|
|
export const state = () => ({
|
|
projectCategories: (null as unknown) as Map<ProjectCategory, IProjectCategory>,
|
|
permissions: (null as unknown) as Map<NamedPermission, IPermission>,
|
|
});
|
|
|
|
export type RootState = ReturnType<typeof state>;
|
|
|
|
export const mutations: MutationTree<RootState> = {
|
|
SET_PROJECT_CATEGORIES: (state, payload: Map<ProjectCategory, IProjectCategory>) => {
|
|
state.projectCategories = payload;
|
|
},
|
|
SET_PERMISSIONS: (state, payload: Map<NamedPermission, IPermission>) => {
|
|
state.permissions = payload;
|
|
},
|
|
};
|
|
|
|
export const actions: ActionTree<RootState, RootState> = {
|
|
async nuxtServerInit({ commit }, { $api }: Context) {
|
|
try {
|
|
const categoryResult = await $api.requestInternal<IProjectCategory[]>('data/categories', false);
|
|
commit(
|
|
'SET_PROJECT_CATEGORIES',
|
|
convertToMap<ProjectCategory, IProjectCategory>(categoryResult, (value) => value.apiName)
|
|
);
|
|
const permissionResultTemp = await $api.requestInternal<{ value: string; frontendName: string; permission: string }[]>('data/permissions', false);
|
|
const permissionResult: IPermission[] = permissionResultTemp.map(({ value, frontendName, permission }) => ({
|
|
value,
|
|
frontendName,
|
|
permission: BigInt('0b' + permission),
|
|
}));
|
|
commit(
|
|
'SET_PERMISSIONS',
|
|
convertToMap<NamedPermission, IPermission>(permissionResult, (value) => value.value)
|
|
);
|
|
// others
|
|
} catch (e) {
|
|
console.error('ERROR FETCHING BACKEND DATA');
|
|
console.error(e);
|
|
}
|
|
},
|
|
};
|
|
|
|
export const getters: GetterTree<RootState, RootState> = {
|
|
visibleCategories: (state: RootState) => Array.from(state.projectCategories.values()).filter((value) => value.visible),
|
|
};
|
|
|
|
function convertToMap<E, T>(values: T[], toStringFunc: (value: T) => string): Map<E, T> {
|
|
const map = new Map<E, T>();
|
|
for (const value of values) {
|
|
const key: E = (toStringFunc(value) as unknown) as E;
|
|
if (key == null) {
|
|
throw new Error('Could not find an enum for ' + value);
|
|
}
|
|
map.set(key, value);
|
|
}
|
|
return map;
|
|
}
|