import * as React from 'react' import { formatNumber, padding } from '../utils/format'; import { TemplateActivityContext } from '../common/template-context'; import { goToLogin } from '../utils/login' import * as Styled from './index.style' import { Props, State } from './type' import { fetchRank, postDunToRankItem, RankItem } from './actions'; const SHOWING_ITEM_MIN_COUNT = 5; const MOCK_RANK_ITEMS = [ { "score": 19, "blogInfo": { "blogNickName": "我是一个演示数据", "bigAvaImg": "https://avaimg.lf127.net/img/739342e8ee7d77ed/aUNBazBSVlNSc1h6TmIyRHNybFdZdVJvOXpPbnJiaVJ5OVZHc21yRDJlYz0.jpg", "blogId": 529047591, "blogName": "pan22012", "selfIntro": "", "imageProtected": false }, "buttonAct": 0, "dun": 0 }, { "score": 18, "blogInfo": { "blogNickName": "我是一个演示数据", "bigAvaImg": "https://avaimg.lf127.net/img/MTZIUnRCd0dETFcyZ1U4L3BuRVFRYlJkR1EwOGZUNzV4ejJJeFhUZzlvc3FjSTBXZmUzSzZ3PT0.jpg", "blogId": 1286330644, "blogName": "flz002", "selfIntro": "", "imageProtected": false }, "buttonAct": 0, "dun": 0 }, { "score": 11, "blogInfo": { "blogNickName": "我是一个演示数据", "bigAvaImg": "https://avaimg.lf127.net/img/98814176f56195d6/TWR3ejFUazF0OGZ2K0txWU5RUFhkVVEwblhNS3B4eEpFa3pNQndSRkxsMD0.jpg", "blogId": 496674184, "blogName": "qatest002", "selfIntro": "", "imageProtected": false }, "buttonAct": 0, "dun": 0 }, { "score": 10, "blogInfo": { "blogNickName": "我是一个演示数据", "bigAvaImg": "https://avaimg.lf127.net/img/4733ef579c36b4b6/ZURaNmU4eEF2NWEyZlFnOVpuWjJjOFR1VFVwaWQzOHhQMExBaU0vVzloTT0.jpg", "blogId": 542262100, "blogName": "11111111112222222222333333333344444444445555555555", "selfIntro": "", "imageProtected": false }, "buttonAct": 0, "dun": 0 }, { "score": 10, "blogInfo": { "blogNickName": "我是一个演示数据", "bigAvaImg": "https://avaimg.lf127.net/img/ce786004eba5a003/NzJoNSthVzlMK2p4b1U3VitvRGg5bDhnRmIrNHN2SHM4R2RZOWlYMkIyZz0.jpg", "blogId": 508150100, "blogName": "gggg6368", "selfIntro": "", "imageProtected": true }, "buttonAct": 0, "dun": 0 } ]; export class TemplateActivityRank extends React.Component< Props, State > { static defaultProps = new Props() state = new State() static contextType = TemplateActivityContext context: React.ContextType = null; expendMax = 10; activityId: string | null = null; componentDidUpdate (prevProps: Props) { if ( ( this.context && this.context.activityInfo && this.activityId !== this.context.activityInfo.actEventInfo.activityId ) || prevProps.rankId !== this.props.rankId ) { this.refresh(); } } componentDidMount() { this.refresh(); } public render() { const { isEdit, style: oriStyle, rankHeader, } = this.props let { expand, rankItemList } = this.state; const style = { ...oriStyle }; let mocking = false; if (isEdit && rankItemList.length < SHOWING_ITEM_MIN_COUNT) { mocking = true; rankItemList = MOCK_RANK_ITEMS as any; } const withButton = rankItemList[0] && rankItemList[0].buttonAct === 1; const shouldShowExpend = rankItemList.length > this.expendMax; const showingRankItemList = !expand ? rankItemList.slice(0, this.expendMax) : rankItemList; return (
{ mocking && ( 当前数据为演示数据 ) } { !this.context && isEdit ? ( 请添加该组件至 [模板活动-容器]中 ) : ( showingRankItemList.length < SHOWING_ITEM_MIN_COUNT ? ( 榜单正在计算中 ) : (
{ !withButton && ( {rankHeader.topText} {rankHeader.contentText} {rankHeader.scoreText} ) } {showingRankItemList.map((rankItem, index) => { const blogUrl = `https://${rankItem.blogInfo.blogName}.lofter.com`; return ( {padding(index + 1)} { withButton ? ( {rankItem.blogInfo.blogNickName} {formatNumber(rankItem.score)}  {rankHeader.scoreText} ) : ( <> {rankItem.blogInfo.blogNickName} {formatNumber(rankItem.score)} ) } { withButton && ( rankItem.dun === 0 ? ( { this.postDunToRankItem(rankItem) }} > 我蹲 ) : ( 已蹲 ) ) } ) })} { shouldShowExpend && ( !expand ? ( { this.setState({ expand: true }) }} > 加载更多 ) : ( { this.setState({ expand: false }) }} > 收起 ) ) }
) ) }
) } async postDunToRankItem ( rankItem: RankItem ) { if (!this.activityId || !this.props.rankId) { return; } if (!this.context?.activityInfo?.hasLogin) { goToLogin(this.context?.getActivityInfo) return; } await postDunToRankItem( this.props.rankId, this.activityId, rankItem.blogInfo.blogId ) this.refresh(); } async refresh () { this.activityId = ( this.context && this.context.activityInfo && this.context.activityInfo.actEventInfo.activityId ); if (!this.activityId || !this.props.rankId) { return; } const { itemList } = await fetchRank( this.props.rankId, this.activityId ) this.setState({ rankItemList: itemList }) } }