MetricModalChoose.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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: t('feature.common.toast.max_metric_count',{count:count}),
  58. })
  59. return
  60. }
  61. }
  62. else {
  63. if (min == count) {
  64. Taro.showToast({
  65. icon: 'none',
  66. title: t('feature.common.toast.min_metric_count',{count:count}),
  67. })
  68. return
  69. }
  70. }
  71. var isFind = false;
  72. oldList.map(item => {
  73. if (obj.code == item.code) {
  74. isFind = true;
  75. item.is_following = check
  76. }
  77. return item
  78. })
  79. if (!isFind) {
  80. if (check) {
  81. obj.is_following = true
  82. newList.push(obj)
  83. } else {
  84. for (var i = 0; i < newList.length; i++) {
  85. if (newList[i].code == obj.code) {
  86. newList.splice(i, 1)
  87. }
  88. }
  89. }
  90. }
  91. for (var i = 0; i < list.length; i++) {
  92. for (var j = 0; j < list[i].items.length; j++) {
  93. var temp = list[i].items[j]
  94. if (obj.code == temp.code) {
  95. temp.is_following = check
  96. }
  97. }
  98. }
  99. setList(JSON.parse(JSON.stringify(list)))
  100. setOldList(JSON.parse(JSON.stringify(oldList)))
  101. setNewList(JSON.parse(JSON.stringify(newList)))
  102. }
  103. function codeIndex(code) {
  104. for (var i = 0; i < oldList.length; i++) {
  105. if (oldList[i].code == code) {
  106. return `序号:${i + 1}`;
  107. }
  108. }
  109. // for (var i = 0; i < newList.length; i++) {
  110. // if (newList[i].code == code) {
  111. // return i + 1 + oldList.length;
  112. // }
  113. // }
  114. return ''
  115. }
  116. function codeNewIndex(code){
  117. for (var i = 0; i < newList.length; i++) {
  118. if (newList[i].code == code) {
  119. return `序号:${i + 1 + oldList.length}`;
  120. }
  121. }
  122. return ''
  123. }
  124. function codeOldIndex(code){
  125. for (var i = 0; i < oldList.length; i++) {
  126. if (oldList[i].code == code) {
  127. return `序号:${i + 1}`;
  128. }
  129. }
  130. return ''
  131. }
  132. return <View className='modal_content'>
  133. <View className='modal_title_view'>
  134. <Text className='modal_title1'>{t('feature.track_something.metric.choose_metric')}</Text>
  135. <Text className='modal_subtitle'>按需选择自己常用的指标</Text>
  136. </View>
  137. <ScrollView className='modal_detail' scrollY>
  138. <View>
  139. {
  140. list.map((item, index) => {
  141. return <View className='modal_group' key={index}>
  142. <Text className='modal_group_title'>{item.name}</Text>
  143. {
  144. item.items.map((obj, i) => {
  145. return <View className='modal_item' onClick={() => tapItem(obj)}>
  146. {
  147. obj.is_following ? <IconRadioCheck width={16} color='#fff' /> : <IconRadio width={16} color='#fff' />
  148. }
  149. <Text className='modal_item_text' style={{ color: color, marginLeft: 10 }}>{obj.name}</Text>
  150. <View style={{ flex: 1 }} />
  151. {
  152. user.test_user && <Text>{obj.is_following ? codeIndex(obj.code) : ''}</Text>
  153. }
  154. {
  155. user.test_user && <Text style={{color:'green'}}>{obj.is_following ? codeNewIndex(obj.code) : ''}</Text>
  156. }
  157. {
  158. user.test_user && <Text style={{color:'red'}}>{!obj.is_following && codeOldIndex(obj.code)}</Text>
  159. }
  160. <View className='px1SeperateHeight'/>
  161. </View>
  162. })
  163. }
  164. </View>
  165. })
  166. }
  167. </View>
  168. </ScrollView>
  169. <View className='modal_operate'>
  170. <View className='modal_btn' style={{ backgroundColor: color + alpha }} onClick={cancel}>
  171. <Text className='modal_cancel_text' style={{ color: color }}>取消</Text>
  172. </View>
  173. <View className='btn_space' />
  174. <View className='modal_btn' style={{ backgroundColor: color }} onClick={confirm}>
  175. <Text className='modal_confirm_text' style={{ color: '#000' }}>下一步</Text>
  176. </View>
  177. </View>
  178. </View>
  179. }