guide_sleep.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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, getSchedules } from "@/services/health";
  7. import { 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 { 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 ScheduleItem from "../components/schedule_item";
  18. import { useTranslation } from "react-i18next";
  19. export default function GuideSleep() {
  20. const health = useSelector((state: any) => state.health);
  21. const [errors, setErrors] = useState<any>([])
  22. const [highlight, setHighlight] = useState(true)
  23. const [list, setList] = useState<any>(health.schedules)
  24. const { t } = useTranslation()
  25. const dispatch = useDispatch()
  26. const selMode = 'SLEEP'
  27. useEffect(() => {
  28. setTimeout(() => {
  29. setHighlight(false)
  30. }, 2000)
  31. }, [])
  32. function check(array, tapDone = false) {
  33. createSchedule({
  34. schedules: array,
  35. only_check: true,
  36. return_all: true,
  37. op_page: 'SCHEDULE_WALKTHROUGH_2', sort_by: 'EVENT'
  38. }).then(res => {
  39. if ((res as any).result) {
  40. dispatch(setSchedules((res as any).schedules))
  41. setErrors([])
  42. if (tapDone) {
  43. jumpPage('./guide_eat')
  44. }
  45. }
  46. else {
  47. setList((res as any).schedules)
  48. setErrors((res as any).error_messages ? (res as any).error_messages : [])
  49. }
  50. })
  51. }
  52. function isDisable(obj) {
  53. if (obj.window == 'FAST' && !obj.is_conflict) {
  54. return true
  55. }
  56. return false
  57. }
  58. function footerTitle() {
  59. if (health.footer) {
  60. return health.footer.sleep.title
  61. }
  62. return ''
  63. }
  64. function footerDesc() {
  65. if (health.footer) {
  66. return health.footer.sleep.description
  67. }
  68. return ''
  69. }
  70. function items() {
  71. var items = list.filter(item => item.window == 'FAST' || item.window == 'SLEEP')
  72. return <Card>
  73. {
  74. errors.map((item1, index) => {
  75. return <View key={index} className='error_tip'>{item1}</View>
  76. })
  77. }
  78. {
  79. items.map((obj, i) => {
  80. return <ScheduleItem
  81. key={i * 100}
  82. obj={obj}
  83. highlight={highlight}
  84. showLine={i < items.length - 1}
  85. errors={errors}
  86. selMode={selMode}
  87. disable={isDisable(obj)}
  88. gray={isDisable(obj)}
  89. days={obj.plus_days != 0 ? obj.plus_days : null}
  90. onChange={(time) => {
  91. var array = JSON.parse(JSON.stringify(list))
  92. array.map(item => {
  93. if (item.id == obj.id) {
  94. item.time = time
  95. item.op_ms = new Date().getTime()
  96. }
  97. })
  98. setList(array)
  99. check(array)
  100. }}
  101. />
  102. })
  103. }
  104. </Card>
  105. }
  106. return <View style={{ flex: 1, display: 'flex', flexDirection: 'column', height: '100vh' }}>
  107. <NewHeader type={NewHeaderType.center_subtitle} title={t('health.guide_1_title')} subtitle={t('health.guide_1_desc')} />
  108. {
  109. items()
  110. }
  111. <View className="cell_footer_title bold h24" style={{ color: MainColorType.g02 }}>{footerTitle()}</View>
  112. <View className="cell_footer_desc h24" style={{ color: MainColorType.g02 }}>{footerDesc()}</View>
  113. <View style={{ flex: 1 }} />
  114. <View className="main_footer">
  115. <NewButton
  116. type={NewButtonType.fill}
  117. title="下一步"
  118. disable={errors.length > 0}
  119. color={MainColorType.sleep}
  120. width={rpxToPx(646)}
  121. height={rpxToPx(96)}
  122. onClick={() => {
  123. check(list, true)
  124. }}
  125. />
  126. </View>
  127. </View>
  128. }