TitleBar.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { View, Text } from "@tarojs/components";
  2. import './TitleBar.scss'
  3. import { useEffect, useState } from "react";
  4. import trackTimeService, { machine } from "@/store/trackTimeMachine";
  5. import { TimeFormatter } from "@/utils/time_format";
  6. import Taro from "@tarojs/taro";
  7. import { recordCheck } from "@/services/trackTimeDuration";
  8. import { useTranslation } from "react-i18next";
  9. import { useSelector } from "react-redux";
  10. export default function Component() {
  11. const { t } = useTranslation()
  12. const [checkData, setCheckData] = useState(null)
  13. const user = useSelector((state: any) => state.user);
  14. useEffect(() => {
  15. if (machine.context.checkData) {
  16. setCheckData(machine.context.checkData as any);
  17. }
  18. }, [machine.context.checkData]);
  19. useEffect(() => {
  20. trackTimeService.onTransition(_ => {
  21. if (machine.context.checkData) {
  22. setCheckData(machine.context.checkData as any);
  23. }
  24. });
  25. }, []);
  26. function more() {
  27. if (user.isLogin == false) {
  28. Taro.navigateTo({
  29. url: '/pages/ChooseAuth'
  30. })
  31. return
  32. }
  33. var state = trackTimeService.getSnapshot().value
  34. if ((state as any).FAST_SLEEP === 'WAIT_FOR_START' ||
  35. (state as any).FAST === 'WAIT_FOR_START' ||
  36. (state as any).SLEEP === 'WAIT_FOR_START') {
  37. waitActionSheet()
  38. }
  39. else if ((state as any).FAST_SLEEP === 'ONGOING1' ||
  40. (state as any).FAST_SLEEP === 'ONGOING2') {
  41. endFastActionSheet()
  42. }
  43. else if ((state as any).FAST_SLEEP === 'ONGOING3') {
  44. Taro.showToast({
  45. title: '暂无更多操作',
  46. icon: 'none',
  47. duration: 2000
  48. })
  49. }
  50. }
  51. function waitActionSheet() {
  52. Taro.showActionSheet({
  53. itemList: [t('feature.track_time_duration.action_sheet.change_schedule'), t('feature.track_time_duration.action_sheet.switch_scenario')]
  54. })
  55. .then(res => {
  56. console.log(res.tapIndex)
  57. switch (res.tapIndex) {
  58. case 0:
  59. Taro.navigateTo({
  60. url: '/pages/SetSchedule'
  61. })
  62. break;
  63. case 1:
  64. Taro.navigateTo({
  65. url: '/pages/ChooseScenario'
  66. })
  67. break;
  68. }
  69. })
  70. .catch(err => {
  71. console.log(err.errMsg)
  72. })
  73. }
  74. function endFastActionSheet() {
  75. Taro.showActionSheet({
  76. itemList: [t('feature.track_time_duration.action_sheet.end_fast')]
  77. })
  78. .then(res => {
  79. console.log(res.tapIndex)
  80. switch (res.tapIndex) {
  81. case 0:
  82. {
  83. const start_time = new Date().getTime();
  84. // const duration = 8 * 3600 * 1000;
  85. const extra = {
  86. set_time: start_time - 20 * 1000,
  87. confirm_time: start_time + 50 * 1000,
  88. }
  89. recordCheck({
  90. action: 'FAST_END',
  91. real_check_time: start_time,
  92. extra: extra
  93. }).then(res => {
  94. trackTimeService.send({ type: 'END_FAST' });
  95. console.log(res);
  96. trackTimeService.send({ type: 'RESET' });
  97. trackTimeService.send({ type: global.scenario });
  98. });
  99. }
  100. break;
  101. }
  102. })
  103. .catch(err => {
  104. console.log(err.errMsg)
  105. })
  106. }
  107. if (!user.isLogin) {
  108. return <View className="detail" onClick={more}>
  109. <View className="detail_item">
  110. <View className="title_bg">
  111. <Text className="title">断食</Text>
  112. </View>
  113. <Text className="time">16:00</Text>
  114. </View>
  115. </View>
  116. }
  117. if (!checkData) {
  118. return <View />
  119. }
  120. return <View className="detail" onClick={more}>
  121. {
  122. ((checkData as any).current_record.scenario == 'FAST' || (checkData as any).current_record.scenario == 'FAST_SLEEP') &&
  123. <View className="detail_item">
  124. <View className="title_bg">
  125. <Text className="title">断食</Text>
  126. {
  127. ((checkData as any).current_record.status == 'ONGOING' ||
  128. (checkData as any).current_record.status == 'ONGOING1' ||
  129. (checkData as any).current_record.status == 'ONGOING2' ||
  130. (checkData as any).current_record.status == 'ONGOING3') ? <View className="badge" /> : <View />
  131. }
  132. </View>
  133. <Text className="time">{TimeFormatter.calculateTimeDifference((checkData as any).current_record.fast.target_start_time,
  134. (checkData as any).current_record.fast.target_end_time, true)}</Text>
  135. </View>
  136. }
  137. {
  138. ((checkData as any).current_record.scenario == 'SLEEP' || (checkData as any).current_record.scenario == 'FAST_SLEEP') &&
  139. <View className="detail_item">
  140. <View className="title_bg">
  141. <Text className="title">睡眠</Text>
  142. {
  143. ((checkData as any).current_record.status == 'ONGOING' ||
  144. (checkData as any).current_record.status == 'ONGOING2') ? <View className="badge" style={{ backgroundColor: '#00FFFF' }} /> : <View />
  145. }
  146. </View>
  147. {
  148. (checkData as any).current_record.status == 'ONGOING3' ? <Text className="time">已完成</Text> : <Text className="time">{TimeFormatter.calculateTimeDifference((checkData as any).current_record.sleep.target_start_time,
  149. (checkData as any).current_record.sleep.target_end_time, true)}</Text>
  150. }
  151. </View>
  152. }
  153. </View>
  154. }