guide_eat.tsx 14 KB

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