import { seasonApi, matchApi } from '@/axios'; import MatchList from '@/components/Match/MatchList'; import SeasonDropdownItem from '@/components/Season/SeasonDropdownItem'; import { customColor } from '@/constants/Colors'; import { shadow } from '@/constants/Styles'; import convertDatas from '@/converter/dataConverter'; import { State } from '@/redux'; import { reloadSeason, selectSeason, setSeason } from '@/redux/slice/season'; import { Ionicons } from '@expo/vector-icons'; import { useLayoutEffect, useState, useEffect, useCallback, useRef, memo } from 'react'; import { FlatList, ImageBackground, Pressable, SafeAreaView, StyleSheet, Text, TouchableOpacity, View } from 'react-native'; import { shallowEqual, useDispatch, useSelector } from 'react-redux'; import { useLoading, useSnackbar } from '../../../contexts'; import { TapGestureHandler } from 'react-native-gesture-handler'; import { clientApi } from '@/axios/client'; import { useFocusEffect } from '@react-navigation/native'; import TabBar from '@/components/UI/TabBar'; import { selectNewMatchs } from '@/redux/slice/match'; import SearchModal from '@/components/modal/SearchModal'; export default function MatchScreen({ route, navigation }: { route: any; navigation: any }) { const isInitialRender = useRef(true); const [isFocus, setIsFocus] = useState(false); const [currentMatchs, setCurrentMatchs] = useState([]); const fixedMatchs = useRef([]); const { isLoading, startLoading, stopLoading } = useLoading(); const { seasons, selectedSeason } = useSelector((state: State) => state.season); const [currentSeason, setCurrentSeason] = useState({}); const { newMatchs } = useSelector((state: State) => state.match, shallowEqual); const [refreshing, setRefreshing] = useState(false); const [thisUserMatchData, setThisUserMatchData] = useState({ lost_money: 0, win: 0, lose: 0 }); const [showSearch, setShowSearch] = useState(false); const dispatch = useDispatch(); const { showSnackbar } = useSnackbar(); const fetchMatchs = async (is_new_match?: number) => { try { const rawMatchs = await matchApi.getMatchBySeason(selectedSeason, is_new_match); const convertedMatchs = convertDatas(rawMatchs.data.data); setCurrentMatchs(convertedMatchs); fixedMatchs.current = convertedMatchs; } catch (err: any) { console.log('Match error', err.response.data.detail); console.log('Match error'); showSnackbar(err.response.data.detail, 'error'); } }; const pressSeason = useCallback( (item: any) => { if (selectedSeason !== item.season_id) { dispatch(selectSeason({ selectedSeason: item.season_id })); dispatch(reloadSeason()); setIsFocus(!isFocus); } }, [selectedSeason], ); const fetchSeason = async () => { try { startLoading(); console.log('fetchSeason: selectedSeason', selectedSeason); const rawSeasons = await seasonApi.getSession(); const convertedSeasons = convertDatas(rawSeasons.data.data); dispatch(setSeason({ seasons: convertedSeasons })); dispatch(selectSeason({ selectedSeason: selectedSeason === -1 ? convertedSeasons[0].season_id : selectedSeason })); //dispatch(reloadSeason()); } catch (err: any) { console.log('Season error'); console.log('season error', err.response.data.detail); showSnackbar(err.response.data.detail, 'error'); } finally { stopLoading(); } }; const getLostMoney = async () => { try { const response = await clientApi.getUserLostMoney(selectedSeason); const money = response.data.data[0].user_lost_money; setThisUserMatchData((prev) => { return { ...prev, lost_money: money }; }); } catch (error: any) { console.log('Money error'); showSnackbar(error?.response.data.detail, 'error'); } }; const getMatchStatus = async () => { let win: number = 0, lose: number = 0; fixedMatchs.current.filter((val: any) => { const selectedOption = val.options.find((option: any) => option.selected === true); if (selectedOption && val.match_correct_option !== null) { if (selectedOption.option_name === val.match_correct_option) { win++; } else { lose++; } } }); setThisUserMatchData((prev) => { return { ...prev, win: win, lose: lose }; }); }; useEffect(() => { const currentSeason = seasons.find((item: any) => item.season_id === selectedSeason); setCurrentSeason(currentSeason); fetchMatchs(newMatchs); getLostMoney(); }, [selectedSeason]); useEffect(() => { console.log('newMatchs', newMatchs); console.log('selectSeason', selectSeason); if (newMatchs === 0) { getMatchStatus(); } console.log('Uffect chay khi tiên or newmath'); }, [thisUserMatchData.lost_money, newMatchs, selectedSeason]); useFocusEffect( useCallback(() => { console.log('useFocusEffect chạy'); fetchMatchs(newMatchs); }, [selectedSeason]), ); useLayoutEffect(() => { navigation.setOptions({ headerRight: () => { return ( setIsFocus((prev) => !prev)}> ); }, }); }, [navigation, isFocus, currentSeason]); const pressTabbar = useCallback( async (id: number) => { startLoading(); try { await fetchMatchs(id); dispatch(selectNewMatchs({ is_new_match: id })); } catch (error) { showSnackbar('Có lỗi xảy ra', 'error'); } finally { stopLoading(); } }, [selectedSeason], ); const onRefresh = useCallback(async () => { setRefreshing(true); try { console.log('onfre nè'); await fetchSeason(); await fetchMatchs(newMatchs); await getLostMoney(); } catch (error) { showSnackbar('Có lỗi xảy ra', 'error'); } finally { setRefreshing(false); } }, [selectedSeason, newMatchs]); return ( {seasons?.length > 0 && ( {isFocus && ( { return ( { pressSeason(item); }} > ); }} /> )} )} {`Nộp quỹ: ${thisUserMatchData.lost_money.toLocaleString() + 'đ'}`} {(thisUserMatchData.win !== 0 || thisUserMatchData.lose !== 0) && ( {`(Thắng: ${thisUserMatchData.win} - Thua: ${thisUserMatchData.lose})`} )} setShowSearch(true)}> {currentMatchs.length > 0 && } {!isLoading && currentMatchs.length === 0 ? ( Không có kèo được tìm thấy ) : ( .... )} {showSearch && ( setShowSearch(false)} /> )} ); } const styles = StyleSheet.create({ imgBackground: { opacity: 1, }, seasonContainer: { position: 'absolute', right: 10, zIndex: 999, }, shadow: { ...shadow, }, });