import { PROBLEM_PERMISSION, STORAGE_KEY, USER_TYPE } from "utils/constants" import storage from "utils/storage" import { Profile, User } from "utils/types" import { getProfile } from "../api" import { useConfigStore } from "./config" export const useUserStore = defineStore("user", () => { const configStore = useConfigStore() const profile = ref(null) const [isFinished] = useToggle(false) const user = computed(() => profile.value?.user ?? null) const isAuthed = computed(() => !!user.value?.email) const isAdminRole = computed( () => user.value?.admin_type === USER_TYPE.STUDENT_ADMIN || user.value?.admin_type === USER_TYPE.TEACHER_ADMIN || user.value?.admin_type === USER_TYPE.SUPER_ADMIN, ) const isStudentAdmin = computed( () => user.value?.admin_type === USER_TYPE.STUDENT_ADMIN, ) const isTeacherAdmin = computed( () => user.value?.admin_type === USER_TYPE.TEACHER_ADMIN, ) const isTeacherOrAbove = computed( () => user.value?.admin_type === USER_TYPE.TEACHER_ADMIN || user.value?.admin_type === USER_TYPE.SUPER_ADMIN, ) const isSuperAdmin = computed( () => user.value?.admin_type === USER_TYPE.SUPER_ADMIN, ) const hasProblemPermission = computed( () => user.value?.problem_permission !== PROBLEM_PERMISSION.NONE, ) const showSubmissions = computed(() => { let flag = configStore.config.submission_list_show_all if (isAdminRole.value) flag = true return flag }) async function getMyProfile() { isFinished.value = false const res = await getProfile() profile.value = res.data isFinished.value = true storage.set(STORAGE_KEY.AUTHED, !!user.value?.email) } function clearProfile() { profile.value = null storage.clear() } return { profile, isFinished, user, isAdminRole, isStudentAdmin, isTeacherAdmin, isTeacherOrAbove, isSuperAdmin, hasProblemPermission, isAuthed, showSubmissions, getMyProfile, clearProfile, } })