import { useEffect, useRef, useState } from "react"; import Taro from "@tarojs/taro"; import classnames from "classnames"; import dayjs from "dayjs"; import { Text, View, Image } from "@tarojs/components"; import OrderService from "@/services/orderService"; import { EvaluateCallback, EvaluateScene } from "@/store/evaluateStore"; import { MATCH_STATUS, IsSubstituteSupported } from "@/services/detailService"; import { GameManagePopup, NTRPEvaluatePopup } from "@/components"; import img from "@/config/images"; import RMB_ICON from "@/static/detail/rmb.svg"; import { toast, navto } from "../../utils/helper"; import styles from "./index.module.scss"; function isFull(counts) { const { max_players, current_players, max_substitute_players, current_substitute_count, is_substitute_supported, } = counts; if (max_players === current_players &&is_substitute_supported === IsSubstituteSupported.NOTSUPPORT) { return true; } else if (max_players === current_players &&is_substitute_supported === IsSubstituteSupported.SUPPORT) { return max_substitute_players === current_substitute_count; } return false; } // 底部操作栏 export default function StickyButton(props) { const { handleShare, handleJoinGame, detail, onStatusChange, handleAddComment, getCommentCount, } = props; const [commentCount, setCommentCount] = useState(0); const ntrpRef = useRef<{ show: (evaluateCallback: EvaluateCallback) => void; }>({ show: () => {} }); const { id, price, user_action_status, match_status, start_time, end_time, is_organizer, } = detail || {}; const gameManageRef = useRef(); function handleSelfEvaluate() { ntrpRef?.current?.show({ type: EvaluateScene.detail, next: (flag) => { if (flag) { Taro.navigateTo({ url: `/order_pages/orderDetail/index?gameId=${id}`, }); return; } Taro.redirectTo({ url: `/order_pages/orderDetail/index?gameId=${id}` }); }, onCancel: () => { // Taro.redirectTo({ url: `/game_pages/detail/index?id=${id}` }); Taro.navigateBack(); }, }); } useEffect(() => { getCommentCount?.((count) => { setCommentCount(count); }); }, [getCommentCount]); function generateTextAndAction( user_action_status: null | { [key: string]: boolean } ): | undefined | { text: string | React.FC; action?: () => void; available?: boolean } { if (!user_action_status) { return; } const priceStrArr = price.toString().split("."); const displayPrice = is_organizer ? ( <> 0 {/* .00 */} ) : ( <> {priceStrArr[0]} .{priceStrArr[1]} ); // user_action_status.can_assess = true; // user_action_status.can_join = false; // console.log(user_action_status, "user_action"); const { can_assess, can_join, can_substitute, can_pay, is_substituting, waiting_start, } = user_action_status || {}; if (MATCH_STATUS.CANCELED === match_status) { return { text: "活动已取消", available: false, // action: () => toast("活动已取消"), }; } else if (MATCH_STATUS.FINISHED === match_status) { return { text: "活动已结束", available: false, // action: () => toast("活动已取消"), }; } else if (dayjs(end_time).isBefore(dayjs())) { return { text: "活动已结束", available: false, // action: () => toast("活动已结束"), }; } else if (dayjs(start_time).isBefore(dayjs())) { return { text: "活动已开始", available: false, // action: () => toast("活动已开始"), }; } else if (isFull(detail)) { return { text: "活动已满员", available: false, // action: () => toast("活动已满员"), }; } if (waiting_start) { return { text: () => ( <> {displayPrice} 已加入 ), action: () => toast("已加入"), }; } else if (is_substituting) { return { text: () => ( <> {displayPrice} 已加入候补 ), action: () => toast("已加入候补"), }; } else if (can_pay) { return { text: () => ( <> {price} 继续支付 ), action: async () => { const res = await OrderService.getUnpaidOrder(id); if (res.code === 0) { navto( `/order_pages/orderDetail/index?id=${res.data.order_info.order_id}` ); } }, }; } else if (can_substitute) { return { text: () => ( <> {displayPrice} 我要候补 ), action: handleJoinGame, }; } else if (can_join) { return { text: () => { return ( <> {displayPrice} 立即加入 ); }, action: handleJoinGame, }; } else if (can_assess) { return { text: () => ( <> {displayPrice} 立即加入 ), action: handleSelfEvaluate, }; } return { text: "球局无法加入", available: false, }; } if (!user_action_status) { return ""; } const { text, available = true, action = () => {}, } = generateTextAndAction(user_action_status)!; let ActionText: React.FC | string = text; if (typeof ActionText === "string") { ActionText = () => { return {text as string}; }; } return ( <> handleShare()} > 分享 { // Taro.showToast({ title: "To be continued", icon: "none" }); handleAddComment(); }} > {commentCount > 0 ? commentCount : "评论"} {is_organizer && ( { gameManageRef.current.show(detail, onStatusChange); }} > 管理 )} ); }