index.tsx 3.88 KB
Newer Older
lirandong committed
1 2
import { ComponentClass } from 'react'
import Taro, { Component } from '@tarojs/taro'
hank committed
3
import { View, Text, Image, Swiper, SwiperItem } from '@tarojs/components'
hank committed
4
import api from '@/api/index.ts'
lirandong committed
5 6 7 8 9 10 11 12 13 14
import './index.scss'

type PageStateProps = {}

type PageDispatchProps = {}

type PageOwnProps = {
  templateUrl: string
  templateName: string
  templateDescribe: string
hank committed
15
  templateShow?: string
hank committed
16
  templateId?: string
lirandong committed
17 18
}

hank committed
19 20 21 22
type PageState = {
  collectionState: boolean
  collectionId: string
}
lirandong committed
23 24 25 26 27 28 29 30 31

type IProps = PageStateProps & PageDispatchProps & PageOwnProps

interface DerailTop {
  props: IProps
  state: PageState
}

class DerailTop extends Component {
hank committed
32 33 34
  public sendState = false
  componentWillMount() {
    const { templateId } = this.props
hank committed
35 36 37 38 39 40
    if (templateId) {
      api.common.getCollectionState(String(templateId)).then(res => {
        this.setState({
          collectionId: res.data,
          collectionState: !!res.data
        })
hank committed
41
      })
hank committed
42
    }
hank committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  }
  toggleCollection() {
    const { collectionState, collectionId } = this.state
    const { templateId } = this.props
    console.log(12313, !this.sendState)
    if (!this.sendState) {
      if (!collectionState) {
        api.common
          .createCollection(String(templateId))
          .then(res => {
            this.setState(
              {
                collectionId: res.data,
                collectionState: !collectionState
              },
              () => {
                this.sendState = false
              }
            )
          })
          .catch(() => {
            this.sendState = false
          })
      } else {
        api.common
          .cancelCollection(String(collectionId))
          .then(() => {
            this.setState(
              {
                collectionState: !collectionState
              },
              () => {
                this.sendState = false
              }
            )
          })
          .catch(() => {
            this.sendState = false
          })
      }

      this.sendState = true
      console.log('toggleCollection')
    }
  }
lirandong committed
88
  render() {
hank committed
89
    let { templateUrl, templateName, templateDescribe, templateShow, templateId } = this.props
hank committed
90
    templateUrl = templateUrl || ''
hank committed
91 92 93 94
    const { collectionState } = this.state
    let collectionUrl = collectionState
      ? 'http://visual-clouds.oss-cn-beijing.aliyuncs.com/miniprogram/collectionSelect.png'
      : 'http://visual-clouds.oss-cn-beijing.aliyuncs.com/miniprogram/collection.png'
lirandong committed
95 96 97
    return (
      <View className="derail-top">
        <View className="temp-top-info">
hank committed
98
          <Swiper
hank committed
99 100 101
            className={
              templateShow === 'HORIZONTAL' ? 'temp-top-img' : 'temp-top-img temp-top-img2'
            }
hank committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
            indicatorColor="#999"
            indicatorActiveColor="#333"
            circular
            indicatorDots
            autoplay
          >
            {templateUrl.split(',').map(item => {
              return (
                <SwiperItem key={item}>
                  <Image
                    className={
                      templateShow === 'HORIZONTAL' ? 'temp-top-img' : 'temp-top-img temp-top-img2'
                    }
                    src={item}
                  />
                </SwiperItem>
              )
            })}
          </Swiper>

lirandong committed
122 123 124 125 126
          <View className="temp-top-text-wrapper">
            <Text className="temp-top-title">{templateName}</Text>
            <View>
              <Text className="temp-top-describe">{templateDescribe}</Text>
            </View>
hank committed
127 128 129 130 131 132 133 134 135
            {templateId ? (
              <Image
                className="temp-top-collection"
                onClick={() => {
                  this.toggleCollection()
                }}
                src={collectionUrl}
              />
            ) : null}
lirandong committed
136 137 138 139 140 141 142 143
          </View>
        </View>
        <View className="temp-top-info-bar" />
      </View>
    )
  }
}
export default DerailTop as ComponentClass<PageOwnProps, PageState>