import { defineStore } from 'pinia' import { ref } from 'vue' import { polylanSubmitterApiGetUserInfo, marketApiListMarkets, marketApiListUserBets, marketApiGetUserPoints } from '@/api' import type { Market } from '@/types' import type { UserInfoOut, UserBetSchema } from '@/api/types.gen' export const useMarketStore = defineStore('market', () => { // State const markets = ref([]) const userInfo = ref() const userBets = ref([]) const userPoints = ref(0) const isLoading = ref(true) const error = ref('') // Actions const loadUserInfo = async () => { try { const response = await polylanSubmitterApiGetUserInfo() if (response.data) { userInfo.value = response.data } } catch (err) { error.value = 'Failed to load user info' console.error('Error loading user info:', err) } } const loadMarkets = async (eventPath: string) => { try { const response = await marketApiListMarkets({ path: { event_path: eventPath } }) if (response.data) { markets.value = response.data as unknown as Market[] } } catch (err) { error.value = 'Failed to load markets' console.error('Error loading markets:', err) } } const loadUserBets = async (eventPath: string) => { try { const response = await marketApiListUserBets({ path: { event_path: eventPath } }) if (response.data) { userBets.value = response.data } } catch (err) { error.value = 'Failed to load user bets' console.error('Error loading user bets:', err) } } const loadUserPoints = async (eventPath: string) => { try { const response = await marketApiGetUserPoints({ path: { event_path: eventPath } }) if (response.data) { userPoints.value = response.data.points } } catch (err) { error.value = 'Failed to load user points' console.error('Error loading user points:', err) } } const initializeMarketPage = async (eventPath: string) => { isLoading.value = true error.value = '' try { await Promise.all([ loadUserInfo(), loadMarkets(eventPath), ]) // Load user bets and points if authenticated if (userInfo.value?.is_authenticated) { await Promise.all([loadUserBets(eventPath), loadUserPoints(eventPath)]) } } finally { isLoading.value = false } } const refreshPage = async (eventPath: string) => { await Promise.all([ loadUserInfo(), loadMarkets(eventPath), userInfo.value?.is_authenticated ? loadUserBets(eventPath) : Promise.resolve(), userInfo.value?.is_authenticated ? loadUserPoints(eventPath) : Promise.resolve(), ]) } return { // State markets, userInfo, userBets, userPoints, isLoading, error, // Actions loadUserInfo, loadMarkets, loadUserBets, loadUserPoints, initializeMarketPage, refreshPage, } })