IndexConsoleMuti.tsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. import { View, Text, Image, PickerView, PickerViewColumn } from "@tarojs/components";
  2. import { useTranslation } from "react-i18next";
  3. import { AtActivityIndicator } from "taro-ui";
  4. import { alphaToHex, rpxToPx } from "@/utils/tools";
  5. import { ColorType } from "@/context/themes/color";
  6. import "@/components/layout/Modal.scss";
  7. import './IndexConsoleMuti.scss';
  8. import { useRef, useState } from "react";
  9. import { batchClocks, endFast, getLocalPush } from "../actions/TrackTimeActions";
  10. import dayjs from "dayjs";
  11. import Modal from "@/components/layout/Modal.weapp";
  12. import ConsolePicker from "./ConsolePicker";
  13. import Taro from "@tarojs/taro";
  14. import { TimeFormatter } from "@/utils/time_format";
  15. let min = 0
  16. let max = 0
  17. let defaultTimestamp = 0
  18. export default function IndexConsoleMuti(props: { status: string, event: string, close: Function, scenario: string }) {
  19. const { t } = useTranslation();
  20. const isLoading = false;
  21. var color = ColorType.fast;
  22. var alpha = alphaToHex(0.4)
  23. const [fastStart, setFastStart] = useState<any>(null)
  24. const [fastEnd, setFastEnd] = useState<any>(null)
  25. const [sleepStart, setSleepStart] = useState<any>(null)
  26. const [sleepEnd, setSleepEnd] = useState<any>(null)
  27. const [showPicker, setShowPicker] = useState(false)
  28. const [operateType, setOperateType] = useState('startFast')
  29. const [commitLoading, setCommitLoading] = useState(false)
  30. const pickerRef = useRef(null)
  31. function cancel() {
  32. props.close()
  33. }
  34. function confirm() {
  35. if (commitLoading) {
  36. return;
  37. }
  38. var fast: any = {}
  39. var sleep: any = {}
  40. switch (props.event) {
  41. case 'start_sleep':
  42. if (!fastStart) {
  43. Taro.showToast({
  44. icon: 'none',
  45. title: '请选择开始断食时间'
  46. })
  47. return
  48. }
  49. if (!sleepStart) {
  50. Taro.showToast({
  51. icon: 'none',
  52. title: '请选择就寝时间'
  53. })
  54. return
  55. }
  56. // fast.real_start_set_time = fastStart.set_time
  57. // fast.real_start_confirm_time = fastStart.confirm_time
  58. // fast.real_start_time = fastStart.time
  59. // fast.real_start_date = fastStart.date
  60. // sleep.real_start_set_time = sleepStart.set_time
  61. // sleep.real_start_confirm_time = sleepStart.confirm_time
  62. // sleep.real_start_time = sleepStart.time
  63. // sleep.real_start_date = sleepStart.date
  64. fast.real_start = {
  65. time: fastStart.time,
  66. date: fastStart.date,
  67. extra: {
  68. set_time: fastStart.set_time,
  69. confirm_time: fastStart.confirm_time
  70. }
  71. }
  72. sleep.real_start = {
  73. time: sleepStart.time,
  74. date: sleepStart.date,
  75. extra: {
  76. set_time: sleepStart.set_time,
  77. confirm_time: sleepStart.confirm_time
  78. }
  79. }
  80. break;
  81. case 'end_sleep':
  82. if (props.status == 'WAIT_FOR_START') {
  83. if (!fastStart) {
  84. Taro.showToast({
  85. icon: 'none',
  86. title: '请选择开始断食时间'
  87. })
  88. return
  89. }
  90. // fast.real_start_set_time = fastStart.set_time
  91. // fast.real_start_confirm_time = fastStart.confirm_time
  92. // fast.real_start_time = fastStart.time
  93. // fast.real_start_date = fastStart.date
  94. fast.real_start = {
  95. time: fastStart.time,
  96. date: fastStart.date,
  97. extra: {
  98. set_time: fastStart.set_time,
  99. confirm_time: fastStart.confirm_time
  100. }
  101. }
  102. }
  103. if (!sleepStart) {
  104. Taro.showToast({
  105. icon: 'none',
  106. title: '请选择就寝时间'
  107. })
  108. return
  109. }
  110. if (!sleepEnd) {
  111. Taro.showToast({
  112. icon: 'none',
  113. title: '请选择起床时间'
  114. })
  115. return
  116. }
  117. // sleep.real_start_set_time = sleepStart.set_time
  118. // sleep.real_start_confirm_time = sleepStart.confirm_time
  119. // sleep.real_start_time = sleepStart.time
  120. // sleep.real_start_date = sleepStart.date
  121. // sleep.real_end_set_time = sleepEnd.set_time
  122. // sleep.real_end_confirm_time = sleepEnd.confirm_time
  123. // sleep.real_end_time = sleepEnd.time
  124. // sleep.real_end_date = sleepEnd.date
  125. sleep.real_start = {
  126. time: sleepStart.time,
  127. date: sleepStart.date,
  128. extra: {
  129. set_time: sleepStart.set_time,
  130. confirm_time: sleepStart.confirm_time
  131. }
  132. }
  133. sleep.real_end = {
  134. time: sleepEnd.time,
  135. date: sleepEnd.date,
  136. extra: {
  137. set_time: sleepEnd.set_time,
  138. confirm_time: sleepEnd.confirm_time
  139. }
  140. }
  141. break;
  142. case 'end_fast':
  143. if (props.status == 'WAIT_FOR_START') {
  144. if (!fastStart) {
  145. Taro.showToast({
  146. icon: 'none',
  147. title: '请选择开始断食时间'
  148. })
  149. return
  150. }
  151. if (!sleepStart && props.scenario == 'FAST_SLEEP') {
  152. Taro.showToast({
  153. icon: 'none',
  154. title: '请选择就寝时间'
  155. })
  156. return
  157. }
  158. // fast.real_start_set_time = fastStart.set_time
  159. // fast.real_start_confirm_time = fastStart.confirm_time
  160. // fast.real_start_time = fastStart.time
  161. // fast.real_start_date = fastStart.date
  162. fast.real_start = {
  163. time: fastStart.time,
  164. date: fastStart.date,
  165. extra: {
  166. set_time: fastStart.set_time,
  167. confirm_time: fastStart.confirm_time
  168. }
  169. }
  170. if (props.scenario == 'FAST_SLEEP') {
  171. // sleep.real_start_set_time = sleepStart.set_time
  172. // sleep.real_start_confirm_time = sleepStart.confirm_time
  173. // sleep.real_start_time = sleepStart.time
  174. // sleep.real_start_date = sleepStart.date
  175. sleep.real_start = {
  176. time: sleepStart.time,
  177. date: sleepStart.date,
  178. extra: {
  179. set_time: sleepStart.set_time,
  180. confirm_time: sleepStart.confirm_time
  181. }
  182. }
  183. }
  184. }
  185. else if (props.status == 'ONGOING1') {
  186. if (!sleepStart) {
  187. Taro.showToast({
  188. icon: 'none',
  189. title: '请选择就寝时间'
  190. })
  191. return
  192. }
  193. // sleep.real_start_set_time = sleepStart.set_time
  194. // sleep.real_start_confirm_time = sleepStart.confirm_time
  195. // sleep.real_start_time = sleepStart.time
  196. // sleep.real_start_date = sleepStart.date
  197. sleep.real_start = {
  198. time: sleepStart.time,
  199. date: sleepStart.date,
  200. extra: {
  201. set_time: sleepStart.set_time,
  202. confirm_time: sleepStart.confirm_time
  203. }
  204. }
  205. }
  206. if (!sleepEnd && props.scenario == 'FAST_SLEEP') {
  207. Taro.showToast({
  208. icon: 'none',
  209. title: '请选择起床时间'
  210. })
  211. return
  212. }
  213. if (!fastEnd) {
  214. Taro.showToast({
  215. icon: 'none',
  216. title: '请选择结束断食时间'
  217. })
  218. return
  219. }
  220. if (props.scenario == 'FAST_SLEEP') {
  221. // sleep.real_end_set_time = sleepEnd.set_time
  222. // sleep.real_end_confirm_time = sleepEnd.confirm_time
  223. // sleep.real_end_time = sleepEnd.time
  224. // sleep.real_end_date = sleepEnd.date
  225. sleep.real_end = {
  226. time: sleepEnd.time,
  227. date: sleepEnd.date,
  228. extra: {
  229. set_time: sleepEnd.set_time,
  230. confirm_time: sleepEnd.confirm_time
  231. }
  232. }
  233. }
  234. // fast.real_end_set_time = fastEnd.set_time
  235. // fast.real_end_confirm_time = fastEnd.confirm_time
  236. // fast.real_end_time = fastEnd.time
  237. // fast.real_end_date = fastEnd.date
  238. fast.real_end = {
  239. time: fastEnd.time,
  240. date: fastEnd.date,
  241. extra: {
  242. set_time: fastEnd.set_time,
  243. confirm_time: fastEnd.confirm_time
  244. }
  245. }
  246. break;
  247. }
  248. setCommitLoading(true)
  249. var params: any = {
  250. }
  251. if (props.scenario == 'FAST_SLEEP') {
  252. params = {
  253. fast,
  254. sleep
  255. }
  256. }
  257. else {
  258. params = {
  259. fast
  260. }
  261. }
  262. params.showAlert = true
  263. batchClocks(params).then(res => {
  264. setCommitLoading(false)
  265. getLocalPush()
  266. global.indexPageRefresh()
  267. if ((res as any).current_record.status == "WAIT_FOR_START") {
  268. global.scrollToLatest()
  269. global.swiperDayNightRefresh()
  270. if (global.refreshNight) {
  271. global.refreshNight()
  272. }
  273. if (global.refreshDay) {
  274. global.refreshDay()
  275. }
  276. }
  277. global.refrehWeekly()
  278. global.refreshStreaks()
  279. if (props.event == 'end_fast')
  280. global.checkAccess((res as any).access)
  281. cancel()
  282. }).catch(e => {
  283. setCommitLoading(false)
  284. })
  285. }
  286. function tapItem(index: number) {
  287. setShowPicker(true);
  288. switch (index) {
  289. case 0:
  290. setOperateType('startFast')
  291. break;
  292. case 1:
  293. setOperateType('startSleep')
  294. break;
  295. case 2:
  296. setOperateType('endSleep')
  297. break;
  298. case 3:
  299. setOperateType('endFast')
  300. break;
  301. }
  302. }
  303. function startFastCell() {
  304. return <View className="new_item_cell" onClick={() => tapItem(0)}>
  305. <Text className="new_item_cell_title">{t('feature.track_time_duration.common.start_fast')}</Text>
  306. <View style={{ flex: 1 }} />
  307. <Text className="new_item_cell_desc">{fastStart ? TimeFormatter.dateTimeFormate(fastStart.time, true) : t('feature.track_time_duration.log_timing.pick_time')}</Text>
  308. <Image className="cell_arrow" src={require('@/assets/images/arrow3.png')} />
  309. </View>
  310. }
  311. function startSleepCell() {
  312. return <View className="new_item_cell" onClick={() => tapItem(1)}>
  313. <Text className="new_item_cell_title">{t('feature.track_time_duration.common.start_sleep')}</Text>
  314. <View style={{ flex: 1 }} />
  315. <Text className="new_item_cell_desc">{sleepStart ? TimeFormatter.dateTimeFormate(sleepStart.time, true) : t('feature.track_time_duration.log_timing.pick_time')}</Text>
  316. <Image className="cell_arrow" src={require('@/assets/images/arrow3.png')} />
  317. </View>
  318. }
  319. function endSleepCell() {
  320. return <View className="new_item_cell" onClick={() => tapItem(2)}>
  321. <Text className="new_item_cell_title">{t('feature.track_time_duration.common.end_sleep')}</Text>
  322. <View style={{ flex: 1 }} />
  323. <Text className="new_item_cell_desc">{sleepEnd ? TimeFormatter.dateTimeFormate(sleepEnd.time, true) : t('feature.track_time_duration.log_timing.pick_time')}</Text>
  324. <Image className="cell_arrow" src={require('@/assets/images/arrow3.png')} />
  325. </View>
  326. }
  327. function endFastCell() {
  328. return <View className="new_item_cell" onClick={() => tapItem(3)}>
  329. <Text className="new_item_cell_title">{t('feature.track_time_duration.common.end_fast')}</Text>
  330. <View style={{ flex: 1 }} />
  331. <Text className="new_item_cell_desc">{fastEnd ? TimeFormatter.dateTimeFormate(fastEnd.time, true) : t('feature.track_time_duration.log_timing.pick_time')}</Text>
  332. <Image className="cell_arrow" src={require('@/assets/images/arrow3.png')} />
  333. </View>
  334. }
  335. function modalContent() {
  336. return <Modal
  337. testInfo={null}
  338. dismiss={() => {
  339. setShowPicker(false)
  340. }}
  341. confirm={() => { }}>
  342. {
  343. timePickerContent()
  344. }
  345. </Modal>
  346. }
  347. function timePickerContent() {
  348. defaultTimestamp = new Date().getTime()
  349. min = defaultTimestamp - 1 * 24 * 3600 * 1000
  350. max = defaultTimestamp
  351. global.pauseIndexTimer = true
  352. var title = t('feature.track_time_duration.console.real_fast_start_dt')
  353. if (operateType == 'endFast') {
  354. title = t('feature.track_time_duration.console.real_fast_end_dt')
  355. }
  356. else if (operateType == 'startSleep') {
  357. title = t('feature.track_time_duration.console.real_sleep_start_dt')
  358. }
  359. else if (operateType == 'endSleep') {
  360. title = t('feature.track_time_duration.console.real_sleep_end_dt')
  361. }
  362. var color = operateType == 'startFast' || operateType == 'endFast' ? ColorType.fast : ColorType.sleep
  363. var endTimestamp = min
  364. var duration = 20000
  365. return <View className="modal_content">
  366. <ConsolePicker ref={pickerRef}
  367. themeColor={color}
  368. title={title}
  369. onCancel={() => {
  370. setShowPicker(false)
  371. global.pauseIndexTimer = false
  372. }}
  373. min={min}
  374. max={max}
  375. current={defaultTimestamp}
  376. duration={duration}
  377. endTimestamp={endTimestamp}
  378. isFast={operateType == 'startFast' || operateType == 'endFast'}
  379. isEnd={operateType == 'endFast' || operateType == 'endSleep'}
  380. isTimeout={false}
  381. isLoading={false}
  382. hidenEndTime={true}
  383. onChange={(e) => {
  384. setShowPicker(false)
  385. global.pauseIndexTimer = false
  386. if (operateType == 'startFast') {
  387. setFastStart({
  388. set_time: defaultTimestamp,
  389. confirm_time: new Date().getTime(),
  390. time: e,
  391. date: dayjs(e).format('YYYYMMDD')
  392. })
  393. }
  394. else if (operateType == 'endFast') {
  395. setFastEnd({
  396. set_time: defaultTimestamp,
  397. confirm_time: new Date().getTime(),
  398. time: e,
  399. date: dayjs(e).format('YYYYMMDD')
  400. })
  401. }
  402. else if (operateType == 'startSleep') {
  403. setSleepStart({
  404. set_time: defaultTimestamp,
  405. confirm_time: new Date().getTime(),
  406. time: e,
  407. date: dayjs(e).format('YYYYMMDD')
  408. })
  409. }
  410. else {
  411. setSleepEnd({
  412. set_time: defaultTimestamp,
  413. confirm_time: new Date().getTime(),
  414. time: e,
  415. date: dayjs(e).format('YYYYMMDD')
  416. })
  417. }
  418. }}
  419. />
  420. </View>
  421. }
  422. return <View className="modal_content">
  423. <View style={{
  424. marginLeft: rpxToPx(46),
  425. height: rpxToPx(120), display: 'flex',
  426. flexDirection: 'row', alignItems: 'center',
  427. marginTop: rpxToPx(36),
  428. marginBottom: rpxToPx(48)
  429. }}>
  430. <Text style={{ color: '#fff', fontWeight: 'bold', fontSize: rpxToPx(48) }}>{t('feature.track_time_duration.log_timing.log_timings')}</Text>
  431. </View>
  432. <View style={{
  433. display: 'flex',
  434. flexDirection: 'column',
  435. backgroundColor: '#262626',
  436. borderRadius: rpxToPx(24),
  437. marginLeft: rpxToPx(46),
  438. marginRight: rpxToPx(46),
  439. marginBottom: rpxToPx(60)
  440. }}>
  441. {
  442. props.event == 'start_sleep' && startFastCell()
  443. }
  444. {
  445. props.event == 'start_sleep' && <View className="new_item_cell_line" />
  446. }
  447. {
  448. props.event == 'start_sleep' && startSleepCell()
  449. }
  450. {
  451. props.event == 'end_sleep' && props.status == 'WAIT_FOR_START' && startFastCell()
  452. }
  453. {
  454. props.event == 'end_sleep' && props.status == 'WAIT_FOR_START' && <View className="new_item_cell_line" />
  455. }
  456. {
  457. props.event == 'end_sleep' && startSleepCell()
  458. }
  459. {
  460. props.event == 'end_sleep' && <View className="new_item_cell_line" />
  461. }
  462. {
  463. props.event == 'end_sleep' && endSleepCell()
  464. }
  465. {
  466. props.event == 'end_fast' && props.status == 'WAIT_FOR_START' && startFastCell()
  467. }
  468. {
  469. props.event == 'end_fast' && props.status == 'WAIT_FOR_START' && <View className="new_item_cell_line" />
  470. }
  471. {
  472. props.event == 'end_fast' && props.scenario == 'FAST_SLEEP' && (props.status == 'ONGOING1' || props.status == 'WAIT_FOR_START') && startSleepCell()
  473. }
  474. {
  475. props.event == 'end_fast' && props.scenario == 'FAST_SLEEP' && (props.status == 'ONGOING1' || props.status == 'WAIT_FOR_START') && <View className="new_item_cell_line" />
  476. }
  477. {
  478. props.event == 'end_fast' && props.scenario == 'FAST_SLEEP' && (props.status == 'ONGOING1' || props.status == 'ONGOING2' || props.status == 'WAIT_FOR_START') && endSleepCell()
  479. }
  480. {
  481. props.event == 'end_fast' && props.scenario == 'FAST_SLEEP' && (props.status == 'ONGOING1' || props.status == 'ONGOING2' || props.status == 'WAIT_FOR_START') && <View className="new_item_cell_line" />
  482. }
  483. {
  484. props.event == 'end_fast' && endFastCell()
  485. }
  486. </View>
  487. <View className='modal_operate'>
  488. <View className='modal_btn' style={{ backgroundColor: color + alpha }} onClick={cancel}>
  489. <Text className='modal_cancel_text' style={{ color: color, fontWeight: 'bold' }}>{t('feature.common.picker_cancel_btn')}</Text>
  490. </View>
  491. <View className='btn_space' />
  492. <View className='modal_btn' style={{ backgroundColor: color, flexDirection: 'row', alignItems: 'center', justifyContent: 'center', opacity: commitLoading ? 0.6 : 1 }} onClick={confirm}>
  493. {
  494. commitLoading && <View style={{ display: 'flex', overflow: 'hidden', height: 20, marginRight: 5 }}><AtActivityIndicator mode="center" color="#000" /></View>
  495. }
  496. <Text className='modal_confirm_text' style={{ color: '#000', fontWeight: 'bold' }}>{t('feature.common.picker_confirm_btn')}</Text>
  497. </View>
  498. </View>
  499. {
  500. showPicker && modalContent()
  501. }
  502. </View>
  503. }