Commit c004c329 authored by m's avatar m

feat: update

parent cf07f2a1
...@@ -6,10 +6,10 @@ ...@@ -6,10 +6,10 @@
export default { export default {
pages: [ pages: [
'pages/prepare/index',
'pages/main/main', 'pages/main/main',
'pages/group/index', 'pages/group/index',
'pages/game/play/index', 'pages/game/play/index',
'pages/prepare/index',
], ],
subPackages: {{packages}}, subPackages: {{packages}},
resizable: true, resizable: true,
......
import { useState } from 'react'
interface UseModalResult {
isVisible: boolean;
showModal: () => void;
closeModal: () => void;
}
const useModal = (): UseModalResult => {
const [isVisible, setIsVisible] = useState(false)
const showModal = () => {
setIsVisible(true)
}
const closeModal = () => {
setIsVisible(false)
}
return {
isVisible,
showModal,
closeModal,
}
}
export default useModal
import { useCallback, useEffect, useRef, useState } from 'react'
import Taro from '@tarojs/taro'
import type { RequestResult } from '@/types'
interface Options<T, P> {
immediate?: boolean;
params?: P;
pollInterval?: number;
pollRetryCount?: number;
trigger?: unknown[];
debounceDelay?: number;
throttleDelay?: number;
retryCount?: number;
onError?: (error: Error & RequestResult<T>) => void;
onSuccess?: (result: T) => void;
onComplete?: () => void;
}
interface Result<T, P> {
data: T | null;
loading: boolean;
error: Error & RequestResult<T> | null;
run: (params?: P) => Promise<void>;
refresh: () => Promise<void>;
cancel: () => void;
updateData: (newData: T) => void;
}
export default function useRequest<T = unknown, P = unknown> (
service: (params?: P) => Promise<T>,
options: Options<T, P>
): Result<T, P> {
const [data, updateData] = useState<T | null>(null)
const [error, setError] = useState<Error & RequestResult<T> | null>(null)
const [loading, setLoading] = useState<boolean>(false)
const paramsRef = useRef<P | undefined>(options.params)
const pollRetryCountRef = useRef<number>(0)
const debounceTimerRef = useRef<NodeJS.Timeout | null>(null)
const throttleTimerRef = useRef<NodeJS.Timeout | null>(null)
const cancelSourceRef = useRef<Taro.RequestTask<RequestResult<T>> | null>(null)
const run = useCallback(async (params?: P) => {
if (params !== undefined) {
paramsRef.current = { ...paramsRef.current, ...params }
}
runConfig(paramsRef.current)
}, [service, options])
const refresh = useCallback(async () => run(paramsRef.current), [run])
const cancel = useCallback(() => cancelSourceRef.current?.abort(), [])
useEffect(() => {
if (options.immediate !== false) {
run(paramsRef.current)
}
}, [])
useEffect(() => {
if (options.trigger !== undefined) {
run(paramsRef.current)
}
}, [JSON.stringify(options.trigger)])
const runService = async (params?: P, retryCount: number = options.retryCount || 0) => {
try {
cancelSourceRef.current?.abort()
paramsRef.current = params
const res = await service(params)
updateData(res)
options.onSuccess?.(res)
} catch (err) {
if (retryCount > 0) {
runService(params, retryCount - 1)
} else {
setError(err)
options.onError?.(err)
}
} finally {
setLoading(false)
options.onComplete?.()
}
}
const executeWithPolling = (params?: P) => {
runService(params)
if (options.pollInterval && (!options.pollRetryCount || pollRetryCountRef.current < options.pollRetryCount)) {
setTimeout(() => runConfig(params), options.pollInterval)
pollRetryCountRef.current += 1
}
}
const executeWithDebounce = (params?: P) => {
if (debounceTimerRef.current) {
clearTimeout(debounceTimerRef.current)
}
debounceTimerRef.current = setTimeout(() => {
executeWithPolling(params)
}, options.debounceDelay)
}
const executeWithThrottle = (params?: P) => {
if (!throttleTimerRef.current) {
throttleTimerRef.current = setTimeout(() => {
throttleTimerRef.current = null
}, options.throttleDelay)
executeWithPolling(params)
}
}
const runConfig = (params?: P) => {
if (options.debounceDelay) {
executeWithDebounce(params)
} else if (options.throttleDelay) {
executeWithThrottle(params)
} else {
executeWithPolling(params)
}
}
return {
data,
error,
loading,
run,
refresh,
cancel,
updateData,
}
}
import { useState, useCallback, useRef } from 'react'
export type ToggleValue = boolean | string | number
interface UseToggleOptions {
initialValue?: ToggleValue;
reverseValue?: ToggleValue;
}
export default function useToggle ({
initialValue = false,
reverseValue = !initialValue,
}: UseToggleOptions = {}) {
const [value, setValue] = useState<ToggleValue>(initialValue)
const initialValueRef = useRef(initialValue)
const reverseValueRef = useRef(reverseValue)
const toggle = useCallback((newInitialValue?: ToggleValue, newReverseValue?: ToggleValue) => {
if (newInitialValue !== undefined) {
initialValueRef.current = newInitialValue
}
if (newReverseValue !== undefined) {
reverseValueRef.current = newReverseValue
}
if (reverseValue === undefined) {
setValue((prevValue) =>
typeof prevValue === 'boolean' ? !prevValue : initialValue
)
} else {
setValue((prevValue) =>
prevValue === initialValueRef.current
? reverseValueRef.current
: initialValueRef.current
)
}
}, [initialValue, reverseValue])
return [value, toggle] as const
}
import { memo } from 'react'
import { View, Text } from '@tarojs/components' import { View, Text } from '@tarojs/components'
// import { useStoreState } from '@/store/hooks'; import Taro from '@tarojs/taro'
import useRequest from '@/hooks/useRequest'
import TopLeftInfo from './top-left-info' import TopLeftInfo from './top-left-info'
import LaunchQuest from './launch-quest' import LaunchQuest from './launch-quest'
import GloryBadge from './glory-badge' import GloryBadge from './glory-badge'
...@@ -8,14 +12,28 @@ import { formatTime } from '../../utils' ...@@ -8,14 +12,28 @@ import { formatTime } from '../../utils'
import styles from './index.module.scss' import styles from './index.module.scss'
const getUserInfo = async ({ uid }: {uid: number}) => {
const response = await Taro.request({
url: `https://api.example.com/users/${uid}`,
method: 'GET',
})
return response.data
}
const Overview: React.FC = () => { const Overview: React.FC = () => {
// const levelInfo = useStoreState((state) => state.prepareModel.levelInfo)
const levelInfo = { const levelInfo = {
title: '表内除法', title: '表内除法',
totalQuestions: 10, totalQuestions: 10,
duration: 80, duration: 80,
} }
const { data, error } = useRequest(getUserInfo, {
params: { uid: 100 },
})
console.log('error', error)
console.log('data', data)
return ( return (
<> <>
<TopLeftInfo /> <TopLeftInfo />
...@@ -41,4 +59,4 @@ const Overview: React.FC = () => { ...@@ -41,4 +59,4 @@ const Overview: React.FC = () => {
) )
} }
export default Overview export default memo(Overview)
import React from 'react' import React from 'react'
import { View, Text, Button } from '@tarojs/components' import { View, Text, Button } from '@tarojs/components'
import Modal from '@/components/Modal' import Modal from '@/components/Modal'
import useModal from '@/hooks/useModal' import useToggle from '@/hooks/useToggle'
import styles from './index.module.scss' import styles from './index.module.scss'
const LaunchQuest: React.FC = () => { const LaunchQuest: React.FC = () => {
const { isVisible, showModal, closeModal } = useModal() const [isVisible, toggleModal] = useToggle()
return ( return (
<> <>
<View className={styles.launchQuest}> <View className={styles.launchQuest}>
<Button onClick={showModal}>开始闯关</Button> <Button onClick={() => toggleModal(true)}>开始闯关</Button>
<Text className={styles.tip}> <Text className={styles.tip}>
首次免答题卡 首次免答题卡
{/* {hasFreePass ? '首次免答题卡' : '-1答题卡'} */} {/* {hasFreePass ? '首次免答题卡' : '-1答题卡'} */}
</Text> </Text>
</View> </View>
<Modal isVisible={isVisible} onClose={closeModal}> <Modal isVisible={isVisible as boolean} onClose={() => toggleModal(false)}>
<View> <View>
<Text>你的答题卡已经用完了</Text> <Text>你的答题卡已经用完了</Text>
<Button onClick={closeModal}>建设学习部落得更多答题卡</Button> <Button onClick={() => toggleModal(false)}>建设学习部落得更多答题卡</Button>
<Button onClick={closeModal}>购买</Button> <Button onClick={() => toggleModal(false)}>购买</Button>
</View> </View>
</Modal> </Modal>
</> </>
......
import { useEffect, memo } from 'react'
import { View, Text } from '@tarojs/components' import { View, Text } from '@tarojs/components'
import { useEffect } from 'react'
import Tabs, { type TabItem } from '@/components/Tabs' import Tabs, { type TabItem } from '@/components/Tabs'
import { useStoreState, useStoreActions } from '@/store/hooks' import { useStoreState, useStoreActions } from '@/store/hooks'
import Overview from './components/overview/index' import Overview from './components/overview/index'
import RankList from './components/rank-list/index' import RankList from './components/rank-list/index'
...@@ -11,10 +13,15 @@ const Prepare: React.FC = () => { ...@@ -11,10 +13,15 @@ const Prepare: React.FC = () => {
useEffect(() => { useEffect(() => {
refresh() refresh()
}, [currentUser]) }, [])
console.log('list')
const tabs: TabItem[] = [ const tabs: TabItem[] = [
{ label: '成都排行榜', children: <RankList users={list} currentUser={currentUser} /> }, {
label: '成都排行榜',
children: <RankList users={list} currentUser={currentUser} />
},
{ {
label: '学习部落排行榜', label: '学习部落排行榜',
children: ( children: (
...@@ -34,4 +41,4 @@ const Prepare: React.FC = () => { ...@@ -34,4 +41,4 @@ const Prepare: React.FC = () => {
) )
} }
export default Prepare export default memo(Prepare)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment