guide_eat.tsx 10 KB

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