feat(GameSelector): request only once
This commit is contained in:
parent
23888b31bc
commit
bcfedd0009
@ -2,7 +2,7 @@ import { Card, Divider, Flex, Select, Skeleton, Space, Statistic, Switch, Typogr
|
||||
import type React from 'react';
|
||||
import { useRequest } from 'ahooks';
|
||||
import { clubs } from './clubList';
|
||||
import { useCallback, useMemo, useState } from 'react';
|
||||
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||
import dayjs from 'dayjs';
|
||||
import type { IEventInfo } from '../../types';
|
||||
|
||||
@ -12,11 +12,11 @@ interface Props {
|
||||
|
||||
export const GameSelector: React.FC<Props> = props => {
|
||||
const [clubId, setClubId] = useState<string>(clubs[0]?.clubId ?? '');
|
||||
const requestEvents = useRequest<IEventInfo[], []>(
|
||||
async () => {
|
||||
const requestEvents = useRequest<IEventInfo[], [string]>(
|
||||
async (clubId: string) => {
|
||||
if (!clubId) return [];
|
||||
return (await fetch(`/api/events/${clubId}`)).json()
|
||||
}, { manual: false, refreshDeps: [clubId] })
|
||||
}, { manual: true });
|
||||
const [showFinished, setShowFinished] = useState(false);
|
||||
const gameList = useMemo(() => {
|
||||
const activeList = requestEvents.data?.map(e => ({
|
||||
@ -30,6 +30,15 @@ export const GameSelector: React.FC<Props> = props => {
|
||||
}, [gameList]);
|
||||
const handleClubChange = useCallback(async (id: string) => {
|
||||
setClubId(id);
|
||||
requestEvents.runAsync(id);
|
||||
}, []);
|
||||
useEffect(() => {
|
||||
const id = setTimeout(() => {
|
||||
const clubId = clubs?.[0]?.clubId;
|
||||
if (!clubId) return;
|
||||
requestEvents.run(clubId);
|
||||
}, 100);
|
||||
return () => clearTimeout(id);
|
||||
}, []);
|
||||
return (
|
||||
<Space orientation='vertical' style={{ width: '100%' }}>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user