Files
boursecu_user/src/views/base/earnMoney/composables/useEarnMoneyPage.ts
T
safavimohadeseh78andCursor a5e458ed25 Split desktop and mobile layouts with shared page data so APIs fetch once per context.
Add layout shells, desktop views, module-level page caches, and UI fixes for auth, home, wallet, transactions, address, and related pages.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-14 10:08:46 +03:30

91 lines
2.3 KiB
TypeScript

import { inject, onMounted, provide, ref, type InjectionKey, type Ref } from 'vue'
import { message } from 'ant-design-vue'
import { getEarnMoney } from '@/api/earnMoney'
import type { EarnMoney } from '@/models/earnMoney'
import { useUserStore } from '@/store/userStore'
import { useConfigStore } from '@/store/configStore'
import { getReferralShareText, getReferralShareTitle } from '@/utils/config'
import { loading } from '@/utils'
export interface EarnMoneyPageContext {
data: Ref<EarnMoney | undefined>
userStore: ReturnType<typeof useUserStore>
nameProject: string
isTransaction: Ref<boolean>
loading: typeof loading
setIsTransaction: (value: boolean) => void
copyCode: () => void
shareCode: () => void
loadEarnMoney: () => Promise<void>
}
export const EARN_MONEY_PAGE_CONTEXT_KEY: InjectionKey<EarnMoneyPageContext> =
Symbol('earnMoneyPageContext')
export function useEarnMoneyPage() {
const data = ref<EarnMoney>()
const userStore = useUserStore()
const nameProject = useConfigStore().config.name_fa
const isTransaction = ref<boolean>(false)
function setIsTransaction(value: boolean) {
isTransaction.value = value
}
function copyCode() {
const val = userStore.userInfo?.referral_id
navigator.clipboard.writeText(val)
message.info('کد رونوشت شد')
}
function shareCode() {
const val = getReferralShareText({
userName: userStore.userInfo?.name,
projectName: nameProject,
referralId: userStore.userInfo?.referral_id,
})
navigator.share({
title: getReferralShareTitle(nameProject),
text: val,
})
}
async function loadEarnMoney() {
try {
loading.value = true
data.value = await getEarnMoney()
} catch (e) {
console.log(e)
} finally {
loading.value = false
}
}
onMounted(() => {
loadEarnMoney()
})
const context: EarnMoneyPageContext = {
data,
userStore,
nameProject,
isTransaction,
loading,
setIsTransaction,
copyCode,
shareCode,
loadEarnMoney,
}
provide(EARN_MONEY_PAGE_CONTEXT_KEY, context)
return context
}
export function useEarnMoneyPageContext() {
const context = inject(EARN_MONEY_PAGE_CONTEXT_KEY)
if (!context) {
throw new Error('useEarnMoneyPageContext must be used within a page that calls useEarnMoneyPage()')
}
return context
}