import * as React from 'react' import * as Styled from './index.style' import Tab from '../tab' import { PostsShowConfig } from '../../template-activity-container/type' import { PostConfig } from '../type' import { getPostListByApi, getPostListByFile } from '../post-list/fetch-post' import Theme1 from './theme1' import Theme2 from './theme2' import Theme3 from './theme3' export interface PostData { imageUrl?: string; title?: string; link?: string; blogInfo?: { blogNickName?: string; bigAvaImg?: string; }, /** 1文章 2图文 3音乐 4视频 5问答 6长文章 */ type?: number; digest?: string; extraData?: { duration: number; } } interface Props { /** 作品展示模块数据 */ data?: PostsShowConfig[]; backgroundColor: string; /** 作品展示配置数据 */ postConfigs: PostConfig[]; isEdit?: boolean; loading?: boolean; } class PostList extends React.Component { state: { active: number; postData: PostData[]; postDataLoading: boolean; } = { active: 0, postData: [], postDataLoading: true } handleTabChange = (index: number) => { this.setState({ active: index }) // 点击 tab 更新数据 this.fetchPostData(index) } fetchPostData = async (index: number) => { if (!this.props.data) return const config = this.props.data[index] const showConfig = this.props.postConfigs[index] this.setState({ postDataLoading: true }) try { if (config.postsGenType === 'auto') { let res = await getPostListByApi({ tag: config.showTag, type: config.autoType === 'day' ? 'date' : config.autoType, limit: showConfig.maxPostCount }) this.setState({ postData: res, postDataLoading: false }) } else { let res = await getPostListByFile( config.activityId, config.id ) this.setState({ postData: res, postDataLoading: false }) } } catch { this.setState({ postDataLoading: false }) } } componentDidMount() { this.fetchPostData(this.state.active) } componentDidUpdate(preProps) { const showCount = this.props.postConfigs.map(item => item.maxPostCount) const preShowCount = preProps.postConfigs.map(item => item.maxPostCount) let showCountDiff = false showCount.forEach((item, index) => { if (item !== preShowCount[index]) { showCountDiff = true } }) if ( this.props.data !== preProps.data || showCountDiff ) { this.fetchPostData(this.state.active) } } renderLoading() { const temp = new Array(8).fill(6) const { postConfigs } = this.props const { active } = this.state if (postConfigs[active]?.theme === 'theme-1') { return ( { temp.map((_item, index) => (
)) } ) } else { return ( { temp.map((_item, index) => (
)) } ) } } public render() { const { backgroundColor, data, postConfigs, isEdit, loading } = this.props const { postData, active, postDataLoading } = this.state return ( item.cname)} onChange={this.handleTabChange} /> { data && data.length ? ( { postConfigs[active]?.descImgUrl ? ( ) : null } { !postDataLoading ? ( <> { postConfigs[active]?.theme === 'theme-1' ? : postConfigs[active]?.theme === 'theme-2' ? : postConfigs[active]?.theme === 'theme-3' ? : null } ) : this.renderLoading() } ) : isEdit ? ( 未配置作品展示模块 ) : this.renderLoading() } ) } } export default React.memo(PostList)