guide_active.tsx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. import { View, Text, Image } from "@tarojs/components";
  2. import './guide.scss'
  3. import '@/_health/pages/schedules.scss'
  4. import NewHeader, { NewHeaderType } from "../components/new_header";
  5. import { useEffect, useState } from "react";
  6. import { createSchedule, delSchedule, getLabelsEvent, getSchedules } from "@/services/health";
  7. import { setFooter, setSchedules } from "@/store/health";
  8. import { useDispatch, useSelector } from "react-redux";
  9. import Card from "../components/card";
  10. import { rpxToPx } from "@/utils/tools";
  11. import { getScenario, getThemeColor } from "@/features/health/hooks/health_hooks";
  12. import Modal from "@/components/layout/Modal.weapp";
  13. import TimePicker from "@/features/common/TimePicker";
  14. import NewButton, { NewButtonType } from "../base/new_button";
  15. import { MainColorType } from "@/context/themes/color";
  16. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  17. import { IconAdd } from "@/components/basic/Icons";
  18. import AddLabel from "../components/add_label";
  19. import ScheduleItem from "../components/schedule_item";
  20. import { useTranslation } from "react-i18next";
  21. import OnBoard from "../components/onboard";
  22. import showAlert from "@/components/basic/Alert";
  23. import CellFooter, { CellFooterType } from "../base/cell_footer";
  24. import CellFooterText from "../base/cell_footer_text";
  25. import StatusIndicator, { StatusType } from "../base/status_indicator";
  26. import Taro from "@tarojs/taro";
  27. let useRoute;
  28. let useNavigation;
  29. let scenario = '';
  30. let guideIndex = 0
  31. let timer;
  32. if (process.env.TARO_ENV == 'rn') {
  33. useRoute = require("@react-navigation/native").useRoute
  34. useNavigation = require("@react-navigation/native").useNavigation
  35. }
  36. export default function GuideActive() {
  37. const systemInfo: any = Taro.getWindowInfo();
  38. const navigationBarHeight = systemInfo.statusBarHeight + 44;
  39. const user = useSelector((state: any) => state.user);
  40. const health = useSelector((state: any) => state.health);
  41. const [list, setList] = useState<any>(health.schedules)
  42. const [errors, setErrors] = useState<any>([])
  43. const [highlight, setHighlight] = useState(true)
  44. const [showModal, setShowModal] = useState(false)
  45. const [labels, setLabels] = useState<any>([])
  46. const [posting, setPosting] = useState(false)
  47. const [count, setCount] = useState(0)
  48. const { t } = useTranslation()
  49. let navigation, showActionSheetWithOptions;
  50. let router
  51. if (useNavigation) {
  52. navigation = useNavigation()
  53. }
  54. const dispatch = useDispatch()
  55. const selMode = 'ACTIVE'
  56. useEffect(() => {
  57. getLabelsEvent({ window: 'ACTIVE' }).then(res => {
  58. setLabels((res as any).labels)
  59. })
  60. setTimeout(() => {
  61. setHighlight(false)
  62. }, 2000)
  63. return () => {
  64. clearInterval(timer)
  65. }
  66. }, [])
  67. useEffect(() => {
  68. var items = list.filter(item => item.window == 'ACTIVE')
  69. if (items.length > 0) {
  70. clearInterval(timer)
  71. }
  72. else {
  73. clearInterval(timer)
  74. timer = setInterval(() => {
  75. setCount(count => count + 1)
  76. }, 1000)
  77. }
  78. }, [list])
  79. function check(array, tapDone = false) {
  80. if (tapDone) {
  81. if (posting) return
  82. setPosting(true)
  83. }
  84. createSchedule({
  85. schedules: array,
  86. only_check: true,
  87. return_all: true,
  88. op_page: 'SCHEDULE_WALKTHROUGH_4', sort_by: 'EVENT'
  89. }).then(res => {
  90. if ((res as any).result) {
  91. dispatch(setSchedules((res as any).schedules))
  92. dispatch(setFooter((res as any).footer))
  93. setList((res as any).schedules)
  94. if (tapDone) {
  95. jumpPage('./guide_full')
  96. }
  97. setErrors([])
  98. }
  99. else {
  100. setList((res as any).schedules)
  101. dispatch(setFooter((res as any).footer))
  102. setErrors((res as any).error_messages ? (res as any).error_messages : [])
  103. }
  104. setPosting(false)
  105. }).catch(e => {
  106. setPosting(false)
  107. })
  108. }
  109. function add() {
  110. const scenario = getScenario(health.windows, 'ACTIVE')
  111. var items = list.filter(item => item.window == 'ACTIVE')
  112. if (scenario.access.max && scenario.access.max <= items.length) {
  113. showAlert({
  114. title: t('health.become_member_title'),
  115. content: t('health.become_member_desc', { count: scenario.access.max, type: t('health.member_activity') }),
  116. cancelText: t('health.not_now'),
  117. confirmText: t('health.choose_plan'),
  118. showCancel: true,
  119. confirm: () => {
  120. jumpPage('/pages/store/product_list', 'ProductList', navigation)
  121. }
  122. })
  123. return;
  124. }
  125. setShowModal(true)
  126. }
  127. function isDisable(obj) {
  128. if (highlight) return true
  129. if ((obj.window == 'SLEEP') && !obj.is_conflict) {
  130. return true
  131. }
  132. return false
  133. }
  134. function footerTitle() {
  135. if (health.footer) {
  136. return health.footer.active.title
  137. }
  138. return ''
  139. }
  140. function footerDesc() {
  141. if (health.footer) {
  142. return health.footer.active.description
  143. }
  144. return ''
  145. }
  146. function items() {
  147. var items = list.filter(item => item.window == 'ACTIVE')
  148. if (items.length == 0) {
  149. if (labels.length == 0) return <View />
  150. var seconds = new Date().getSeconds()
  151. if (seconds % 1 == 0) {
  152. guideIndex++;
  153. if (guideIndex >= labels.length) {
  154. guideIndex = 0
  155. }
  156. }
  157. return <Card>
  158. <OnBoard title={labels[guideIndex].title}
  159. desc={labels[guideIndex].time_label}
  160. color={MainColorType.active}
  161. btnTitle={t('health.add_my_first_activity')}
  162. onClick={() => {
  163. add()
  164. }}
  165. />
  166. </Card>
  167. }
  168. return <Card>
  169. {
  170. errors.map((item1, index) => {
  171. return <View key={index} className='error_tip'>{item1}</View>
  172. })
  173. }
  174. {
  175. items.map((obj, i) => {
  176. return <ScheduleItem
  177. index={i}
  178. count={items.length + 1}
  179. key={i * 100}
  180. obj={obj}
  181. highlight={highlight ? obj.window == 'ACTIVE' : false}
  182. showLine={true}
  183. errors={errors}
  184. selMode={selMode}
  185. disable={isDisable(obj)}
  186. gray={isDisable(obj)}
  187. days={obj.plus_days != 0 ? obj.plus_days : null}
  188. hideReminderIcon={true}
  189. tapSpecificTime={(detail) => {
  190. // debugger
  191. // obj.time = detail.time
  192. // obj.specific_time = !detail.is_full_day
  193. // obj.time_label = detail.label
  194. var array = JSON.parse(JSON.stringify(list))
  195. var index = -1
  196. array.map((temp, j) => {
  197. if (obj.id == temp.id){
  198. index = j
  199. }
  200. else if (obj.title==temp.title){
  201. index = j
  202. }
  203. })
  204. if (index >= 0) {
  205. array[index].time = detail.time
  206. array[index].specific_time = !detail.is_full_day
  207. array[index].time_label = detail.label
  208. array[index].op_ms = new Date().getTime()
  209. setList(array)
  210. }
  211. // checkData(array)
  212. }}
  213. onDelete={() => {
  214. const scenario = getScenario(health.windows, 'ACTIVE')
  215. if (scenario.access.min >= items.length) {
  216. var type = ''
  217. if (global.language == 'en') {
  218. type = items.length == 1 ? 'activity' : 'activities'
  219. }
  220. else {
  221. type = '活动'
  222. }
  223. Taro.showToast({
  224. title: t('health.keep_at_least', { number: items.length, type: type }),
  225. icon: 'none'
  226. })
  227. return
  228. }
  229. if (errors.length > 0) {
  230. Taro.showToast({
  231. title: t('health.resolve_conflict'),
  232. icon: 'none'
  233. })
  234. return
  235. }
  236. delSchedule(obj.id).then(res => {
  237. var temps = JSON.parse(JSON.stringify(health.schedules))
  238. var temps2 = temps.filter(item => item.id != obj.id)
  239. dispatch(setSchedules(temps2))
  240. dispatch(setFooter((res as any).footer))
  241. var array = JSON.parse(JSON.stringify(list))
  242. for (var j = 0; j < array.length; j++) {
  243. if (array[j].id == obj.id) {
  244. array.splice(j, 1)
  245. }
  246. }
  247. setList(array)
  248. check(array)
  249. global.refreshWindow()
  250. })
  251. }}
  252. onChange={(time) => {
  253. var array = JSON.parse(JSON.stringify(list))
  254. array.map(item => {
  255. if ((item.id && item.id == obj.id) || (!item.id && item.title == obj.title)) {
  256. item.time = time
  257. item.op_ms = new Date().getTime()
  258. }
  259. })
  260. setList(array)
  261. check(array)
  262. }}
  263. />
  264. })
  265. }
  266. <View className='item_add'
  267. onClick={() => add()}>
  268. <StatusIndicator type={StatusType.img} text={t('health.add_active')}
  269. space={rpxToPx(12)}
  270. color={MainColorType.active} fontColor={MainColorType.active} fontSize={rpxToPx(34)} >
  271. <IconAdd color="#fff" width={rpxToPx(20)} />
  272. </StatusIndicator>
  273. <View style={{ flex: 1 }} />
  274. </View>
  275. </Card>
  276. }
  277. function fastSleepItems() {
  278. var items = list.filter(item => item.window == 'SLEEP')
  279. return <Card>
  280. {
  281. items.map((obj, i) => {
  282. return <ScheduleItem
  283. index={i}
  284. count={items.length}
  285. key={i * 100}
  286. obj={obj}
  287. highlight={false}
  288. showLine={i < items.length - 1}
  289. errors={errors}
  290. selMode={selMode}
  291. disable={isDisable(obj)}
  292. gray={isDisable(obj)}
  293. days={obj.plus_days != 0 ? obj.plus_days : null}
  294. hideReminderIcon={true}
  295. // onDelete={() => {
  296. // const scenario = getScenario(health.windows, 'ACTIVE')
  297. // if (scenario.access.min >= items.length) {
  298. // showAlert({
  299. // title: '删除',
  300. // content: '最少保留' + items.length + '个',
  301. // showCancel: true,
  302. // confirm: () => {
  303. // }
  304. // })
  305. // return
  306. // }
  307. // var array = JSON.parse(JSON.stringify(list))
  308. // for (var j = 0; j < array.length; j++) {
  309. // if (array[j].id == obj.id) {
  310. // array.splice(j, 1)
  311. // }
  312. // }
  313. // setList(array)
  314. // check(array)
  315. // global.refreshWindow()
  316. // }}
  317. onChange={(time) => {
  318. var array = JSON.parse(JSON.stringify(list))
  319. array.map(item => {
  320. if ((item.id && item.id == obj.id) || (!item.id && item.title == obj.title)) {
  321. item.time = time
  322. item.op_ms = new Date().getTime()
  323. }
  324. })
  325. setList(array)
  326. check(array)
  327. }}
  328. />
  329. })
  330. }
  331. </Card>
  332. }
  333. return <View style={{ flex: 1, display: 'flex', flexDirection: 'column', height: '100vh' }}>
  334. <View style={{ height: navigationBarHeight, backgroundColor: MainColorType.g05, flexShrink: 0 }} />
  335. <View className="navi-bar" style={{ height: navigationBarHeight, zIndex: 1000, backgroundColor: MainColorType.g05 }}>
  336. <View style={{
  337. position: 'absolute',
  338. left: 0,
  339. right: 0,
  340. bottom: 0,
  341. height: 44,
  342. display: 'flex',
  343. alignItems: 'center',
  344. justifyContent: 'center'
  345. }}>
  346. <Image src={require('@assets/_health/navi_back.png')} style={{
  347. position: 'absolute',
  348. width: rpxToPx(92),
  349. height: rpxToPx(64),
  350. left: 0,
  351. top: 22 - rpxToPx(32)
  352. }}
  353. onClick={() => {
  354. Taro.navigateBack()
  355. }}
  356. />
  357. <Image src={user.avatar} mode="aspectFill" style={{
  358. background: user.isLogin ? 'transparent' : '#fff',
  359. width: rpxToPx(64),
  360. height: rpxToPx(64),
  361. borderRadius: rpxToPx(32)
  362. }} />
  363. </View>
  364. </View>
  365. <NewHeader type={NewHeaderType.center_subtitle} title={t('health.guide_3_title')} subtitle={t('health.guide_3_desc')} />
  366. {
  367. items()
  368. }
  369. <CellFooter type={CellFooterType.left}>
  370. <View style={{ display: 'flex', flexDirection: 'column' }}>
  371. <CellFooterText text={footerTitle()} isBold />
  372. <CellFooterText text={footerDesc()} />
  373. </View>
  374. </CellFooter>
  375. <View style={{ height: 20, flexShrink: 0 }} />
  376. {
  377. fastSleepItems()
  378. }
  379. <View style={{ height: 300, flexShrink: 0 }} />
  380. <View style={{ flex: 1 }} />
  381. <View className="main_footer">
  382. <NewButton
  383. type={NewButtonType.fill}
  384. title={t('health.next')}
  385. disable={errors.length > 0}
  386. color={MainColorType.active}
  387. width={rpxToPx(646)}
  388. height={rpxToPx(96)}
  389. onClick={() => {
  390. check(list, true)
  391. }}
  392. />
  393. </View>
  394. {
  395. showModal && <AddLabel labels={labels}
  396. window='ACTIVE'
  397. disMiss={() => setShowModal(false)}
  398. onlyCheck={true}
  399. schedules={list}
  400. op_page='SCHEDULE_WALKTHROUGH_4'
  401. confirm={(res) => {
  402. setShowModal(false)
  403. if ((res as any).result) {
  404. dispatch(setSchedules((res as any).schedules))
  405. dispatch(setFooter((res as any).footer))
  406. setList((res as any).schedules)
  407. setErrors([])
  408. }
  409. else {
  410. setList((res as any).schedules)
  411. dispatch(setFooter((res as any).footer))
  412. setErrors((res as any).error_messages ? (res as any).error_messages : [])
  413. }
  414. }}
  415. color={MainColorType.active} />
  416. }
  417. </View>
  418. }