auto connect session, create session after button

This commit is contained in:
Annie Huang authored and wancat committed 2024-06-27 10:30:00 +08:00
1 parent ea2ba2c7a0
commit 15465feda9
2 files changed
+40 -25

No files matched your search

+20 -6
View File
@@ -1,7 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { useToast } from 'vue-toastification' import { useToast } from 'vue-toastification'
import { ref } from 'vue' import { ref } from 'vue'
import { init, setQuest } from '../../store/session' import { getActiveSession, setQuest, createSession } from '../../store/session'
import { LoadQuestError, LoadSessionError } from '../../utils/errors' import { LoadQuestError, LoadSessionError } from '../../utils/errors'
const props = defineProps({ const props = defineProps({
@@ -12,12 +12,12 @@ const props = defineProps({
}) })
const toast = useToast() const toast = useToast()
const showCover = ref(true) const showCover = ref(false)
setQuest(props.questId)
const startSession = async () => { function init(questId: string) {
showCover.value = false if (!questId) throw new Error('No quest ID provided')
try { try {
await init(props.questId) getActiveSession(props.questId)
} catch (err: any) { } catch (err: any) {
if (err instanceof LoadQuestError) { if (err instanceof LoadQuestError) {
toast.error(`無法讀取關卡: ${err.questId},請確認網頁連結。`) toast.error(`無法讀取關卡: ${err.questId},請確認網頁連結。`)
@@ -27,6 +27,20 @@ const startSession = async () => {
toast.error(`無法建立工作階段: ${err.questId},請重新登入再試一次。`) toast.error(`無法建立工作階段: ${err.questId},請重新登入再試一次。`)
return return
} }
console.error(err)
toast.warning('Failed to connect previous session. Creating a new one...')
showCover.value = true
}
}
init(props.questId)
setQuest(props.questId)
const startSession = async () => {
showCover.value = false
try {
createSession()
} catch (err: any) {
console.error(err) console.error(err)
toast.error(err.message) toast.error(err.message)
} }
+20 -19
View File
@@ -80,14 +80,15 @@ export async function createSession() {
} }
} }
async function getActiveSession(questId: string) { export async function getActiveSession(questId: string) {
try { try {
const res = await api.post('/sessions/active', { questId }) const res = await api.post('/sessions/active', { questId })
await setSession(res.data) await setSession(res.data)
} catch (err) { } catch (err) {
console.error(err) console.error(err)
toast.warning('Failed to connect previous session. Creating a new one...') throw new LoadSessionError('Failed to load session', questId)
await createSession() // toast.warning('Failed to connect previous session. Creating a new one...')
// await createSession()
} }
} }
@@ -119,22 +120,22 @@ export function reset() {
store.quest = null store.quest = null
} }
export async function init(questId: string) { // export async function init(questId: string) {
if (!questId) throw new Error('No quest ID provided') // if (!questId) throw new Error('No quest ID provided')
// reset() // reset()
// try { // try {
// await setQuest(questId) // await setQuest(questId)
// } catch (err) { // } catch (err) {
// console.error(err) // console.error(err)
// throw new LoadQuestError('Failed to load quest', questId) // throw new LoadQuestError('Failed to load quest', questId)
// } // }
try { // try {
await getActiveSession(questId) // await getActiveSession(questId)
} catch (err) { // } catch (err) {
console.error(err) // console.error(err)
throw new LoadSessionError('Failed to load session', questId) // throw new LoadSessionError('Failed to load session', questId)
} // }
} // }
function setup() { function setup() {
socket.on('terminal', (data: string) => { socket.on('terminal', (data: string) => {