mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
refactor: rewrite user profile store using pinia
This commit is contained in:
1 parent
103ce4774c
commit
8a31a58c56
8 files changed
+68
-178
No files matched your search
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
Run mongoDB first:
|
Run mongoDB first:
|
||||||
|
|
||||||
docker compose -f docker-compose.dev.yml up -d db
|
docker compose up -d
|
||||||
|
|
||||||
The db will automatically restarts by Docker.
|
The db will automatically restarts by Docker.
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -16,13 +16,14 @@
|
|||||||
"@fortawesome/vue-fontawesome": "^3.0.3",
|
"@fortawesome/vue-fontawesome": "^3.0.3",
|
||||||
"@linux-odyssey/constants": "*",
|
"@linux-odyssey/constants": "*",
|
||||||
"@linux-odyssey/file-graph": "*",
|
"@linux-odyssey/file-graph": "*",
|
||||||
"@linux-odyssey/utils": "*",
|
|
||||||
"@linux-odyssey/models": "*",
|
"@linux-odyssey/models": "*",
|
||||||
|
"@linux-odyssey/utils": "*",
|
||||||
"@types/dompurify": "^3.0.5",
|
"@types/dompurify": "^3.0.5",
|
||||||
"axios": "^1.6.0",
|
"axios": "^1.6.0",
|
||||||
"dompurify": "^3.0.6",
|
"dompurify": "^3.0.6",
|
||||||
"echarts": "^5.4.3",
|
"echarts": "^5.4.3",
|
||||||
"marked": "^10.0.0",
|
"marked": "^10.0.0",
|
||||||
|
"pinia": "^2.1.7",
|
||||||
"socket.io-client": "^4.7.1",
|
"socket.io-client": "^4.7.1",
|
||||||
"validator": "^13.11.0",
|
"validator": "^13.11.0",
|
||||||
"vue": "^3.3.4",
|
"vue": "^3.3.4",
|
||||||
|
|||||||
@@ -1,142 +0,0 @@
|
|||||||
<script setup lang="ts">
|
|
||||||
import { defineProps, onMounted } from 'vue'
|
|
||||||
import { useRouter } from 'vue-router'
|
|
||||||
import { useToast } from 'vue-toastification'
|
|
||||||
import { logout } from '../utils/auth'
|
|
||||||
import { bugReportUrl, surveyUrl } from '../config'
|
|
||||||
import { reset } from '../store/session'
|
|
||||||
import userProfileStore, {
|
|
||||||
loadUserProfile,
|
|
||||||
resetUserProfile,
|
|
||||||
} from '../store/userProfile'
|
|
||||||
|
|
||||||
const router = useRouter()
|
|
||||||
|
|
||||||
const toast = useToast()
|
|
||||||
|
|
||||||
const handleLogout = async () => {
|
|
||||||
try {
|
|
||||||
await logout()
|
|
||||||
resetUserProfile()
|
|
||||||
reset()
|
|
||||||
router.push({ name: 'login' })
|
|
||||||
} catch (err) {
|
|
||||||
toast.error('登出失敗')
|
|
||||||
console.error(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
defineProps({
|
|
||||||
title: {
|
|
||||||
type: String,
|
|
||||||
required: false,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
onMounted(async () => {
|
|
||||||
try {
|
|
||||||
await loadUserProfile()
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err)
|
|
||||||
toast.error('無法讀取使用者資料')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<div id="header" class="bg-bg-secondary content-center h-full w-full p-1">
|
|
||||||
<div class="h-full flex items-center justify-center mx-3">
|
|
||||||
<div class="flex items-center gap-3">
|
|
||||||
<!-- <button id="stream" class="flex items-center h-5 w-5">
|
|
||||||
<font-awesome-icon
|
|
||||||
:icon="['fas', 'bars']"
|
|
||||||
class="text-text-primary h-full w-full"
|
|
||||||
/>
|
|
||||||
</button>
|
|
||||||
<button id="backward" class="flex items-center h-5 w-5">
|
|
||||||
<font-awesome-icon
|
|
||||||
:icon="['fas', 'arrow-left']"
|
|
||||||
class="text-border h-full w-full"
|
|
||||||
/>
|
|
||||||
</button> -->
|
|
||||||
<img src="../img/icon_totem.svg" class="h-5" />
|
|
||||||
<button
|
|
||||||
id="GameTitle"
|
|
||||||
class="text-text-primary inline-block font-bold whitespace-nowrap"
|
|
||||||
style="font-size: 2vh"
|
|
||||||
>
|
|
||||||
Linux Odyssey
|
|
||||||
</button>
|
|
||||||
<p
|
|
||||||
id="QuestTitle"
|
|
||||||
class="text-text inline-block font-bold whitespace-nowrap"
|
|
||||||
style="font-size: 2vh"
|
|
||||||
>
|
|
||||||
{{ title }}
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
<div class="h-full w-full flex gap-3 items-center justify-end">
|
|
||||||
<!-- <button id="warning" class="flex items-center h-5 w-5">
|
|
||||||
<font-awesome-icon
|
|
||||||
:icon="['fas', 'triangle-exclamation']"
|
|
||||||
class="text-text-primary h-full w-full"
|
|
||||||
/>
|
|
||||||
</button>
|
|
||||||
<button id="setting" class="flex items-center h-5 w-5">
|
|
||||||
<font-awesome-icon
|
|
||||||
:icon="['fas', 'gear']"
|
|
||||||
class="text-text-primary h-full w-full"
|
|
||||||
/>
|
|
||||||
</button>
|
|
||||||
<button id="fullscreen" class="flex items-center h-5 w-5">
|
|
||||||
<font-awesome-icon
|
|
||||||
:icon="['fas', 'expand']"
|
|
||||||
class="text-text-primary h-full w-full"
|
|
||||||
/>
|
|
||||||
</button> -->
|
|
||||||
<p
|
|
||||||
id="UsernameText"
|
|
||||||
class="text-text inline-block whitespace-nowrap"
|
|
||||||
style="font-size: 2vh"
|
|
||||||
>
|
|
||||||
{{ userProfileStore.username }}
|
|
||||||
</p>
|
|
||||||
<a title="Survey" :href="surveyUrl" target="_blank" class="h-5 w-5">
|
|
||||||
<font-awesome-icon
|
|
||||||
:icon="['fas', 'file-invoice']"
|
|
||||||
class="text-text-primary h-full w-full"
|
|
||||||
/>
|
|
||||||
</a>
|
|
||||||
<RouterLink title="LeaderBoard" to="/leaderboard" class="h-5 w-5">
|
|
||||||
<font-awesome-icon
|
|
||||||
:icon="['fas', 'trophy']"
|
|
||||||
class="text-text-primary h-full w-full"
|
|
||||||
/>
|
|
||||||
</RouterLink>
|
|
||||||
<RouterLink title="Map" to="/map" class="h-5 w-5">
|
|
||||||
<font-awesome-icon
|
|
||||||
:icon="['fas', 'map']"
|
|
||||||
class="text-text-primary h-full w-full"
|
|
||||||
/>
|
|
||||||
</RouterLink>
|
|
||||||
<a
|
|
||||||
title="Bug Report"
|
|
||||||
:href="bugReportUrl"
|
|
||||||
target="_blank"
|
|
||||||
class="h-5 w-5"
|
|
||||||
>
|
|
||||||
<font-awesome-icon
|
|
||||||
:icon="['fas', 'bug']"
|
|
||||||
class="text-text-primary h-full w-full"
|
|
||||||
/>
|
|
||||||
</a>
|
|
||||||
<button title="Sign Out" @click="handleLogout" class="h-5 w-5">
|
|
||||||
<font-awesome-icon
|
|
||||||
:icon="['fas', 'arrow-right-from-bracket']"
|
|
||||||
class="text-text-primary h-full w-full"
|
|
||||||
/>
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
@@ -5,11 +5,9 @@ import { useToast } from 'vue-toastification'
|
|||||||
import { logout } from '../../utils/auth'
|
import { logout } from '../../utils/auth'
|
||||||
import { bugReportUrl, surveyUrl } from '../../config'
|
import { bugReportUrl, surveyUrl } from '../../config'
|
||||||
import { reset } from '../../store/session'
|
import { reset } from '../../store/session'
|
||||||
import userProfileStore, {
|
import useUserProfile from '../../store/userProfile'
|
||||||
loadUserProfile,
|
|
||||||
resetUserProfile,
|
|
||||||
} from '../../store/userProfile'
|
|
||||||
|
|
||||||
|
const store = useUserProfile()
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
|
|
||||||
const toast = useToast()
|
const toast = useToast()
|
||||||
@@ -17,7 +15,7 @@ const toast = useToast()
|
|||||||
const handleLogout = async () => {
|
const handleLogout = async () => {
|
||||||
try {
|
try {
|
||||||
await logout()
|
await logout()
|
||||||
resetUserProfile()
|
store.resetUserProfile()
|
||||||
reset()
|
reset()
|
||||||
router.push({ name: 'login' })
|
router.push({ name: 'login' })
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -28,7 +26,7 @@ const handleLogout = async () => {
|
|||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
try {
|
try {
|
||||||
await loadUserProfile()
|
await store.loadUserProfile()
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
toast.error('無法讀取使用者資料')
|
toast.error('無法讀取使用者資料')
|
||||||
console.error(err)
|
console.error(err)
|
||||||
@@ -59,7 +57,7 @@ onMounted(async () => {
|
|||||||
class="text-text inline-block whitespace-nowrap px-1.5"
|
class="text-text inline-block whitespace-nowrap px-1.5"
|
||||||
style="font-size: 2vh"
|
style="font-size: 2vh"
|
||||||
>
|
>
|
||||||
{{ userProfileStore.username }}
|
{{ store.username }}
|
||||||
</p>
|
</p>
|
||||||
<a
|
<a
|
||||||
title="Survey"
|
title="Survey"
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
import { createApp } from 'vue'
|
import { createApp } from 'vue'
|
||||||
|
import { createPinia } from 'pinia'
|
||||||
|
|
||||||
import './style.css'
|
import './style.css'
|
||||||
/* import font awesome icon component */
|
/* import font awesome icon component */
|
||||||
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
|
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
|
||||||
@@ -13,9 +15,11 @@ import App from './App.vue'
|
|||||||
import router from './router'
|
import router from './router'
|
||||||
|
|
||||||
const app = createApp(App)
|
const app = createApp(App)
|
||||||
|
const pinia = createPinia()
|
||||||
|
|
||||||
app.component('font-awesome-icon', FontAwesomeIcon)
|
app.component('font-awesome-icon', FontAwesomeIcon)
|
||||||
|
|
||||||
|
app.use(pinia)
|
||||||
app.use(router)
|
app.use(router)
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
|
|||||||
@@ -1,30 +1,35 @@
|
|||||||
import { reactive } from 'vue'
|
import { defineStore } from 'pinia'
|
||||||
import api from '../utils/api'
|
import { getUserProfile } from '../utils/api'
|
||||||
|
|
||||||
const store = reactive({
|
interface IProgress {
|
||||||
username: '',
|
quest: string
|
||||||
email: '',
|
sessions: string[]
|
||||||
progress: {},
|
completed: boolean
|
||||||
|
startedAt: Date
|
||||||
|
finishedAt?: Date
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface UserProfile {
|
||||||
|
username: string
|
||||||
|
email: string
|
||||||
|
progress: Map<string, IProgress>
|
||||||
|
}
|
||||||
|
|
||||||
|
const useUserProfile = defineStore('userProfile', {
|
||||||
|
state: (): UserProfile => ({
|
||||||
|
username: '',
|
||||||
|
email: '',
|
||||||
|
progress: new Map<string, IProgress>(),
|
||||||
|
}),
|
||||||
|
actions: {
|
||||||
|
async loadUserProfile() {
|
||||||
|
const res = await getUserProfile()
|
||||||
|
this.$patch(res)
|
||||||
|
},
|
||||||
|
async resetUserProfile() {
|
||||||
|
this.$reset()
|
||||||
|
},
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
export async function loadUserProfile() {
|
export default useUserProfile
|
||||||
try {
|
|
||||||
const res = await api.get('/users/me')
|
|
||||||
const { username, email, progress } = res.data
|
|
||||||
store.username = username
|
|
||||||
store.email = email
|
|
||||||
store.progress = progress
|
|
||||||
return store
|
|
||||||
} catch (err: any) {
|
|
||||||
console.error(err)
|
|
||||||
throw new Error(`Failed to load user profile: ${err.message}`)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function resetUserProfile() {
|
|
||||||
store.username = ''
|
|
||||||
store.email = ''
|
|
||||||
store.progress = {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export default store
|
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import axios from 'axios'
|
import axios from 'axios'
|
||||||
|
import { UserProfile } from '../store/userProfile'
|
||||||
|
|
||||||
const api = axios.create({
|
const api = axios.create({
|
||||||
baseURL: '/api/v1',
|
baseURL: '/api/v1',
|
||||||
@@ -8,3 +9,8 @@ const api = axios.create({
|
|||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
})
|
})
|
||||||
export default api
|
export default api
|
||||||
|
|
||||||
|
export async function getUserProfile(): Promise<UserProfile> {
|
||||||
|
const res = await api.get<UserProfile>('/users/me')
|
||||||
|
return res.data
|
||||||
|
}
|
||||||
@@ -2479,6 +2479,11 @@
|
|||||||
"@vue/compiler-dom" "3.4.19"
|
"@vue/compiler-dom" "3.4.19"
|
||||||
"@vue/shared" "3.4.19"
|
"@vue/shared" "3.4.19"
|
||||||
|
|
||||||
|
"@vue/devtools-api@^6.5.0":
|
||||||
|
version "6.6.3"
|
||||||
|
resolved "https://registry.yarnpkg.com/@vue/devtools-api/-/devtools-api-6.6.3.tgz#b23a588154cba8986bba82b6e1d0248bde3fd1a0"
|
||||||
|
integrity sha512-0MiMsFma/HqA6g3KLKn+AGpL1kgKhFWszC9U29NfpWK5LE7bjeXxySWJrOJ77hBz+TBrBQ7o4QJqbPbqbs8rJw==
|
||||||
|
|
||||||
"@vue/devtools-api@^6.5.1":
|
"@vue/devtools-api@^6.5.1":
|
||||||
version "6.6.1"
|
version "6.6.1"
|
||||||
resolved "https://registry.yarnpkg.com/@vue/devtools-api/-/devtools-api-6.6.1.tgz#7c14346383751d9f6ad4bea0963245b30220ef83"
|
resolved "https://registry.yarnpkg.com/@vue/devtools-api/-/devtools-api-6.6.1.tgz#7c14346383751d9f6ad4bea0963245b30220ef83"
|
||||||
@@ -6773,6 +6778,14 @@ pify@^2.2.0, pify@^2.3.0:
|
|||||||
resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c"
|
resolved "https://registry.yarnpkg.com/pify/-/pify-2.3.0.tgz#ed141a6ac043a849ea588498e7dca8b15330e90c"
|
||||||
integrity sha512-udgsAY+fTnvv7kI7aaxbqwWNb0AHiB0qBO89PZKPkoTmGOgdbrHDKD+0B2X4uTfJ/FT1R09r9gTsjUjNJotuog==
|
integrity sha512-udgsAY+fTnvv7kI7aaxbqwWNb0AHiB0qBO89PZKPkoTmGOgdbrHDKD+0B2X4uTfJ/FT1R09r9gTsjUjNJotuog==
|
||||||
|
|
||||||
|
pinia@^2.1.7:
|
||||||
|
version "2.1.7"
|
||||||
|
resolved "https://registry.yarnpkg.com/pinia/-/pinia-2.1.7.tgz#4cf5420d9324ca00b7b4984d3fbf693222115bbc"
|
||||||
|
integrity sha512-+C2AHFtcFqjPih0zpYuvof37SFxMQ7OEG2zV9jRI12i9BOy3YQVAHwdKtyyc8pDcDyIc33WCIsZaCFWU7WWxGQ==
|
||||||
|
dependencies:
|
||||||
|
"@vue/devtools-api" "^6.5.0"
|
||||||
|
vue-demi ">=0.14.5"
|
||||||
|
|
||||||
pirates@^4.0.1, pirates@^4.0.4:
|
pirates@^4.0.1, pirates@^4.0.4:
|
||||||
version "4.0.6"
|
version "4.0.6"
|
||||||
resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.6.tgz#3018ae32ecfcff6c29ba2267cbf21166ac1f36b9"
|
resolved "https://registry.yarnpkg.com/pirates/-/pirates-4.0.6.tgz#3018ae32ecfcff6c29ba2267cbf21166ac1f36b9"
|
||||||
@@ -8160,6 +8173,11 @@ vite@^4.5.3:
|
|||||||
optionalDependencies:
|
optionalDependencies:
|
||||||
fsevents "~2.3.2"
|
fsevents "~2.3.2"
|
||||||
|
|
||||||
|
vue-demi@>=0.14.5:
|
||||||
|
version "0.14.8"
|
||||||
|
resolved "https://registry.yarnpkg.com/vue-demi/-/vue-demi-0.14.8.tgz#00335e9317b45e4a68d3528aaf58e0cec3d5640a"
|
||||||
|
integrity sha512-Uuqnk9YE9SsWeReYqK2alDI5YzciATE0r2SkA6iMAtuXvNTMNACJLJEXNXaEy94ECuBe4Sk6RzRU80kjdbIo1Q==
|
||||||
|
|
||||||
vue-eslint-parser@^9.4.2:
|
vue-eslint-parser@^9.4.2:
|
||||||
version "9.4.2"
|
version "9.4.2"
|
||||||
resolved "https://registry.yarnpkg.com/vue-eslint-parser/-/vue-eslint-parser-9.4.2.tgz#02ffcce82042b082292f2d1672514615f0d95b6d"
|
resolved "https://registry.yarnpkg.com/vue-eslint-parser/-/vue-eslint-parser-9.4.2.tgz#02ffcce82042b082292f2d1672514615f0d95b6d"
|
||||||
|
|||||||
Reference in new issue
Block a user