MetricModalChoose.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import { View, Text, ScrollView } from '@tarojs/components'
  2. import './MetricModalChoose.scss'
  3. import { alphaToHex } from '@/utils/tools'
  4. import { useTranslation } from 'react-i18next'
  5. import { useEffect, useState } from 'react'
  6. import { IconRadio, IconRadioCheck } from '@/components/basic/Icons'
  7. import Taro from '@tarojs/taro'
  8. import { useSelector } from 'react-redux'
  9. export default function Component(props: {
  10. themeColor: string, cancel: Function, confirm: Function, array: any, limit: any,
  11. orders: any, newOrders: any
  12. }) {
  13. const color = props.themeColor ? props.themeColor : '#ff0000'
  14. const user = useSelector((state: any) => state.user);
  15. const [list, setList] = useState(props.array)
  16. const [oldList, setOldList] = useState(props.orders)
  17. const [newList, setNewList] = useState(props.newOrders)
  18. const min = props.limit.min
  19. const max = props.limit.max
  20. const { t } = useTranslation()
  21. var alpha = alphaToHex(0.4)
  22. function cancel() {
  23. props.cancel()
  24. }
  25. function confirm() {
  26. // props.confirm(orderList)
  27. var array: any = []
  28. for (var i = 0; i < oldList.length; i++) {
  29. if (oldList[i].is_following) {
  30. array.push(oldList[i])
  31. }
  32. }
  33. for (var i = 0; i < newList.length; i++) {
  34. array.push(newList[i])
  35. }
  36. console.log(oldList,newList,array)
  37. debugger
  38. props.confirm(oldList,newList,array,list)
  39. }
  40. function tapItem(obj) {
  41. var check = !obj.is_following
  42. var count = 0;
  43. oldList.map(item => {
  44. if (item.is_following) {
  45. count++;
  46. }
  47. })
  48. newList.map(item => {
  49. if (item.is_following) {
  50. count++;
  51. }
  52. })
  53. if (check) {
  54. if (max == count) {
  55. Taro.showToast({
  56. icon: 'none',
  57. title:`高级会员可选择${count}项以上`
  58. // title: t('feature.common.toast.max_metric_count',{count:count}),
  59. })
  60. return
  61. }
  62. }
  63. else {
  64. if (min == count) {
  65. Taro.showToast({
  66. icon: 'none',
  67. title: t('feature.common.toast.min_metric_count',{count:count}),
  68. })
  69. return
  70. }
  71. }
  72. var isFind = false;
  73. oldList.map(item => {
  74. if (obj.code == item.code) {
  75. isFind = true;
  76. item.is_following = check
  77. }
  78. return item
  79. })
  80. if (!isFind) {
  81. if (check) {
  82. obj.is_following = true
  83. newList.push(obj)
  84. } else {
  85. for (var i = 0; i < newList.length; i++) {
  86. if (newList[i].code == obj.code) {
  87. newList.splice(i, 1)
  88. }
  89. }
  90. }
  91. }
  92. for (var i = 0; i < list.length; i++) {
  93. for (var j = 0; j < list[i].items.length; j++) {
  94. var temp = list[i].items[j]
  95. if (obj.code == temp.code) {
  96. temp.is_following = check
  97. }
  98. }
  99. }
  100. setList(JSON.parse(JSON.stringify(list)))
  101. setOldList(JSON.parse(JSON.stringify(oldList)))
  102. setNewList(JSON.parse(JSON.stringify(newList)))
  103. }
  104. function codeIndex(code) {
  105. for (var i = 0; i < oldList.length; i++) {
  106. if (oldList[i].code == code) {
  107. return `序号:${i + 1}`;
  108. }
  109. }
  110. // for (var i = 0; i < newList.length; i++) {
  111. // if (newList[i].code == code) {
  112. // return i + 1 + oldList.length;
  113. // }
  114. // }
  115. return ''
  116. }
  117. function codeNewIndex(code){
  118. for (var i = 0; i < newList.length; i++) {
  119. if (newList[i].code == code) {
  120. return `序号:${i + 1 + oldList.length}`;
  121. }
  122. }
  123. return ''
  124. }
  125. function codeOldIndex(code){
  126. for (var i = 0; i < oldList.length; i++) {
  127. if (oldList[i].code == code) {
  128. return `序号:${i + 1}`;
  129. }
  130. }
  131. return ''
  132. }
  133. return <View className='modal_content'>
  134. <View className='modal_title_view'>
  135. <Text className='modal_title1'>{t('feature.track_something.metric.choose_metric')}</Text>
  136. <Text className='modal_subtitle'>按需选择自己常用的指标</Text>
  137. </View>
  138. <ScrollView className='modal_detail' scrollY>
  139. <View>
  140. {
  141. list.map((item, index) => {
  142. return <View className='modal_group' key={index}>
  143. <Text className='modal_group_title'>{item.name}</Text>
  144. {
  145. item.items.map((obj, i) => {
  146. return <View className='modal_item' onClick={() => tapItem(obj)}>
  147. {
  148. obj.is_following ? <IconRadioCheck width={16} color='#fff' /> : <IconRadio width={16} color='#fff' />
  149. }
  150. <Text className='modal_item_text' style={{ color: color, marginLeft: 10 }}>{obj.name}</Text>
  151. <View style={{ flex: 1 }} />
  152. {
  153. user.test_user && <Text>{obj.is_following ? codeIndex(obj.code) : ''}</Text>
  154. }
  155. {
  156. user.test_user && <Text style={{color:'green'}}>{obj.is_following ? codeNewIndex(obj.code) : ''}</Text>
  157. }
  158. {
  159. user.test_user && <Text style={{color:'red'}}>{!obj.is_following && codeOldIndex(obj.code)}</Text>
  160. }
  161. <View className='px1SeperateHeight'/>
  162. </View>
  163. })
  164. }
  165. </View>
  166. })
  167. }
  168. </View>
  169. </ScrollView>
  170. <View className='modal_operate'>
  171. <View className='modal_btn' style={{ backgroundColor: color + alpha }} onClick={cancel}>
  172. <Text className='modal_cancel_text' style={{ color: color }}>取消</Text>
  173. </View>
  174. <View className='btn_space' />
  175. <View className='modal_btn' style={{ backgroundColor: color }} onClick={confirm}>
  176. <Text className='modal_confirm_text' style={{ color: '#000' }}>下一步</Text>
  177. </View>
  178. </View>
  179. </View>
  180. }