guide_eat.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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 showAlert from "@/components/basic/Alert";
  22. import CellFooter, { CellFooterType } from "../base/cell_footer";
  23. import CellFooterText from "../base/cell_footer_text";
  24. import StatusIndicator, { StatusType } from "../base/status_indicator";
  25. let useRoute;
  26. let useNavigation;
  27. let scenario = '';
  28. if (process.env.TARO_ENV == 'rn') {
  29. useRoute = require("@react-navigation/native").useRoute
  30. useNavigation = require("@react-navigation/native").useNavigation
  31. }
  32. export default function GuideEat() {
  33. const health = useSelector((state: any) => state.health);
  34. const [list, setList] = useState<any>(health.schedules)
  35. const [errors, setErrors] = useState<any>([])
  36. const [highlight, setHighlight] = useState(true)
  37. const [showModal, setShowModal] = useState(false)
  38. const [labels, setLabels] = useState<any>([])
  39. const { t } = useTranslation()
  40. let navigation, showActionSheetWithOptions;
  41. let router
  42. if (useNavigation) {
  43. navigation = useNavigation()
  44. }
  45. const dispatch = useDispatch()
  46. const selMode = 'EAT'
  47. useEffect(() => {
  48. getLabelsEvent({ window: 'EAT' }).then(res => {
  49. setLabels((res as any).labels)
  50. })
  51. setTimeout(() => {
  52. setHighlight(false)
  53. }, 2000)
  54. }, [])
  55. function check(array, tapDone = false) {
  56. createSchedule({
  57. schedules: array,
  58. only_check: true,
  59. return_all: true,
  60. op_page: 'SCHEDULE_WALKTHROUGH_3', sort_by: 'EVENT'
  61. }).then(res => {
  62. if ((res as any).result) {
  63. dispatch(setSchedules((res as any).schedules))
  64. dispatch(setFooter((res as any).footer))
  65. setList((res as any).schedules)
  66. setErrors([])
  67. if (tapDone) {
  68. jumpPage('./guide_active')
  69. }
  70. }
  71. else {
  72. setList((res as any).schedules)
  73. dispatch(setFooter((res as any).footer))
  74. setErrors((res as any).error_messages ? (res as any).error_messages : [])
  75. }
  76. })
  77. }
  78. function add() {
  79. const scenario = getScenario(health.windows, 'EAT')
  80. var items = list.filter(item => item.window == 'EAT')
  81. if (scenario.access.max <= items.length) {
  82. showAlert({
  83. title: '会员',
  84. content: '会员desc',
  85. showCancel: true,
  86. confirm: () => {
  87. jumpPage('/pages/store/product_list', 'ProductList', navigation)
  88. }
  89. })
  90. return;
  91. }
  92. setShowModal(true)
  93. }
  94. function isDisable(obj) {
  95. if ((obj.window == 'FAST' || obj.window == 'SLEEP') && !obj.is_conflict) {
  96. return true
  97. }
  98. return false
  99. }
  100. function footerTitle() {
  101. if (health.footer) {
  102. return health.footer.eat.title
  103. }
  104. return ''
  105. }
  106. function footerDesc() {
  107. if (health.footer) {
  108. return health.footer.eat.description
  109. }
  110. return ''
  111. }
  112. function items() {
  113. var items = list.filter(item => item.window == 'EAT')
  114. return <Card>
  115. {
  116. errors.map((item1, index) => {
  117. return <View key={index} className='error_tip'>{item1}</View>
  118. })
  119. }
  120. {
  121. items.map((obj, i) => {
  122. return <ScheduleItem
  123. key={i * 100}
  124. obj={obj}
  125. highlight={highlight ? obj.window == 'EAT' : false}
  126. showLine={true}
  127. errors={errors}
  128. selMode={selMode}
  129. disable={isDisable(obj)}
  130. gray={isDisable(obj)}
  131. days={obj.plus_days != 0 ? obj.plus_days : null}
  132. hideReminderIcon={true}
  133. onDelete={() => {
  134. const scenario = getScenario(health.windows, 'EAT')
  135. if (scenario.access.min >= items.length) {
  136. showAlert({
  137. title: '删除',
  138. content: '最少保留' + items.length + '个',
  139. showCancel: true,
  140. confirm: () => {
  141. }
  142. })
  143. return
  144. }
  145. delSchedule(obj.id).then(res => {
  146. dispatch(setFooter((res as any).footer))
  147. var array = JSON.parse(JSON.stringify(list))
  148. for (var j = 0; j < array.length; j++) {
  149. if (array[j].id == obj.id) {
  150. array.splice(j, 1)
  151. }
  152. }
  153. setList(array)
  154. global.refreshWindow()
  155. })
  156. }}
  157. onChange={(time) => {
  158. var array = JSON.parse(JSON.stringify(list))
  159. array.map(item => {
  160. if (item.id == obj.id) {
  161. item.time = time
  162. item.op_ms = new Date().getTime()
  163. }
  164. })
  165. dispatch(setSchedules(array))
  166. check(array)
  167. }}
  168. />
  169. })
  170. }
  171. <View className='item_add'
  172. onClick={() => add()}>
  173. <StatusIndicator type={StatusType.normal} text={t('health.add_meal')}
  174. color={MainColorType.eat} fontColor={MainColorType.eat} fontSize={rpxToPx(34)}/>
  175. <View style={{ flex: 1 }} />
  176. </View>
  177. </Card>
  178. }
  179. function fastSleepItems() {
  180. var items = list.filter(item => item.window == 'FAST' || item.window == 'SLEEP')
  181. return <Card>
  182. {
  183. items.map((obj, i) => {
  184. return <ScheduleItem
  185. key={i * 100}
  186. obj={obj}
  187. highlight={false}
  188. showLine={i < items.length - 1}
  189. errors={errors}
  190. selMode={selMode}
  191. disable={isDisable(obj)}
  192. gray={isDisable(obj)}
  193. days={obj.plus_days != 0 ? obj.plus_days : null}
  194. hideReminderIcon={true}
  195. onDelete={() => {
  196. var array = JSON.parse(JSON.stringify(list))
  197. for (var j = 0; j < array.length; j++) {
  198. if (array[j].id == obj.id) {
  199. array.splice(j, 1)
  200. }
  201. }
  202. setList(array)
  203. check(array)
  204. global.refreshWindow()
  205. }}
  206. onChange={(time) => {
  207. var array = JSON.parse(JSON.stringify(list))
  208. array.map(item => {
  209. if (item.id == obj.id) {
  210. item.time = time
  211. item.op_ms = new Date().getTime()
  212. }
  213. })
  214. setList(array)
  215. check(array)
  216. }}
  217. />
  218. })
  219. }
  220. </Card>
  221. }
  222. return <View style={{ flex: 1, display: 'flex', flexDirection: 'column', height: '100vh' }}>
  223. <NewHeader type={NewHeaderType.center_subtitle} title={t('health.guide_2_title')} subtitle={t('health.guide_2_desc')} />
  224. {
  225. items()
  226. }
  227. <CellFooter type={CellFooterType.left}>
  228. <View style={{ display: 'flex', flexDirection: 'column' }}>
  229. <CellFooterText text={footerTitle()} isBold />
  230. <CellFooterText text={footerDesc()} />
  231. </View>
  232. </CellFooter>
  233. <View style={{ height: 20, flexShrink: 0 }} />
  234. {
  235. fastSleepItems()
  236. }
  237. <View style={{ height: 300, flexShrink: 0 }} />
  238. <View style={{ flex: 1 }} />
  239. <View className="main_footer">
  240. <NewButton
  241. type={NewButtonType.fill}
  242. title="下一步"
  243. disable={errors.length > 0}
  244. color={MainColorType.eat}
  245. width={rpxToPx(646)}
  246. height={rpxToPx(96)}
  247. onClick={() => {
  248. check(list, true)
  249. }}
  250. />
  251. </View>
  252. {
  253. showModal && <Modal testInfo={null}
  254. dismiss={() => {
  255. setShowModal(false)
  256. }}
  257. confirm={() => { }}>
  258. <AddLabel labels={labels}
  259. window='EAT'
  260. disMiss={() => setShowModal(false)}
  261. onlyCheck={true}
  262. schedules={list}
  263. confirm={(res) => {
  264. if ((res as any).result) {
  265. dispatch(setSchedules((res as any).schedules))
  266. dispatch(setFooter((res as any).footer))
  267. setList((res as any).schedules)
  268. setErrors([])
  269. }
  270. else {
  271. setList((res as any).schedules)
  272. dispatch(setFooter((res as any).footer))
  273. setErrors((res as any).error_messages ? (res as any).error_messages : [])
  274. }
  275. }}
  276. color={MainColorType.eat} />
  277. </Modal>
  278. }
  279. </View>
  280. }