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 type React from 'react';
|
||||||
import { useRequest } from 'ahooks';
|
import { useRequest } from 'ahooks';
|
||||||
import { clubs } from './clubList';
|
import { clubs } from './clubList';
|
||||||
import { useCallback, useMemo, useState } from 'react';
|
import { useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
import dayjs from 'dayjs';
|
import dayjs from 'dayjs';
|
||||||
import type { IEventInfo } from '../../types';
|
import type { IEventInfo } from '../../types';
|
||||||
|
|
||||||
@ -12,11 +12,11 @@ interface Props {
|
|||||||
|
|
||||||
export const GameSelector: React.FC<Props> = props => {
|
export const GameSelector: React.FC<Props> = props => {
|
||||||
const [clubId, setClubId] = useState<string>(clubs[0]?.clubId ?? '');
|
const [clubId, setClubId] = useState<string>(clubs[0]?.clubId ?? '');
|
||||||
const requestEvents = useRequest<IEventInfo[], []>(
|
const requestEvents = useRequest<IEventInfo[], [string]>(
|
||||||
async () => {
|
async (clubId: string) => {
|
||||||
if (!clubId) return [];
|
if (!clubId) return [];
|
||||||
return (await fetch(`/api/events/${clubId}`)).json()
|
return (await fetch(`/api/events/${clubId}`)).json()
|
||||||
}, { manual: false, refreshDeps: [clubId] })
|
}, { manual: true });
|
||||||
const [showFinished, setShowFinished] = useState(false);
|
const [showFinished, setShowFinished] = useState(false);
|
||||||
const gameList = useMemo(() => {
|
const gameList = useMemo(() => {
|
||||||
const activeList = requestEvents.data?.map(e => ({
|
const activeList = requestEvents.data?.map(e => ({
|
||||||
@ -30,6 +30,15 @@ export const GameSelector: React.FC<Props> = props => {
|
|||||||
}, [gameList]);
|
}, [gameList]);
|
||||||
const handleClubChange = useCallback(async (id: string) => {
|
const handleClubChange = useCallback(async (id: string) => {
|
||||||
setClubId(id);
|
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 (
|
return (
|
||||||
<Space orientation='vertical' style={{ width: '100%' }}>
|
<Space orientation='vertical' style={{ width: '100%' }}>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user