Console.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. import { recordCheck } from "@/services/trackTimeDuration";
  2. import { View, Text, PickerView } from "@tarojs/components";
  3. import trackTimeService, { machine } from "@/store/trackTimeMachine"
  4. import { useEffect, useState } from "react";
  5. import { TimeFormatter } from "@/utils/time_format";
  6. import TimePickers from '@/components/TimePickers'
  7. import { useSelector } from "react-redux";
  8. import Taro from "@tarojs/taro";
  9. import { AtFloatLayout } from "taro-ui";
  10. import LimitPickers from '@/components/LimitPickers';
  11. import { endFast, endSleep, startFast, startSleep } from "../actions/TrackTimeActions";
  12. import { pickerDurations } from "../hooks/Console";
  13. import PickerViews from "@/components/PickerViews";
  14. export default function Component() {
  15. const [key, setKey] = useState('');
  16. const [value, setValue] = useState('');
  17. const user = useSelector((state: any) => state.user);
  18. const [isFast, setIsFast] = useState(true);
  19. const [fastValues, setFastValues] = useState<number[]>([0, 0]);
  20. const [sleepValues, setSleepValues] = useState<number[]>([0, 0]);
  21. const [fastDuration, setFastDuration] = useState<number>(0);
  22. const [sleepDuration, setSleepDuration] = useState<number>(0);
  23. const [fastStr, setFastStr] = useState('00:00');
  24. const [sleepStr, setSleepStr] = useState('00:00');
  25. const [isOpen, setIsOpen] = useState(false);
  26. const [resetPicker, setResetPicker] = useState(false);
  27. const [fastPickerValue, setFastPickerValue] = useState([0])
  28. const [sleepPickerValue, setSleepPickerValue] = useState([0])
  29. // const pickerDurations = pickerDurations();
  30. // console.log(pickerDurations())
  31. useEffect(() => {
  32. getStateDetail();
  33. }, [machine.context.checkData]);
  34. useEffect(() => {
  35. getStateDetail();
  36. }, [machine.context.currentStatus])
  37. useEffect(() => {
  38. trackTimeService.onTransition(state => {
  39. if ((state.value as any).FAST_SLEEP) {
  40. setKey('FAST_SLEEP');
  41. setValue((state.value as any).FAST_SLEEP);
  42. }
  43. if ((state.value as any).FAST) {
  44. setKey('FAST');
  45. setValue((state.value as any).FAST);
  46. }
  47. if ((state.value as any).SLEEP) {
  48. setKey('SLEEP');
  49. setValue((state.value as any).SLEEP);
  50. }
  51. });
  52. }, []);
  53. function getStateDetail() {
  54. var state = trackTimeService.getSnapshot().value
  55. if ((state as any).FAST_SLEEP) {
  56. setKey('FAST_SLEEP');
  57. setValue((state as any).FAST_SLEEP);
  58. if ((state as any).FAST_SLEEP == 'WAIT_FOR_START' || (state as any).FAST_SLEEP == 'ONGOING3') {
  59. setIsFast(true);
  60. }
  61. else if ((state as any).FAST_SLEEP == 'ONGOING1') {
  62. setIsFast(false);
  63. }
  64. }
  65. if ((state as any).FAST) {
  66. setKey('FAST');
  67. setValue((state as any).FAST);
  68. setIsFast(true);
  69. }
  70. if ((state as any).SLEEP) {
  71. setKey('SLEEP');
  72. setValue((state as any).SLEEP);
  73. setIsFast(false);
  74. }
  75. var checkData = machine.context.checkData;
  76. if (checkData) {
  77. var current_record = (checkData as any).current_record;
  78. if (current_record.fast) {
  79. var fastTime = TimeFormatter.formateHourMinute(current_record.fast.target_start_time,
  80. current_record.fast.target_end_time);
  81. setFastValues(fastTime.split(':').map(x => parseInt(x)));
  82. setFastStr(fastTime);
  83. var fastCount = current_record.fast.target_end_time - current_record.fast.target_start_time
  84. setFastDuration(fastCount)
  85. setFastPickerValue([fastCount / 60000 / 5 - 12])
  86. }
  87. if (current_record.sleep) {
  88. var sleepTime = TimeFormatter.formateHourMinute(current_record.sleep.target_start_time,
  89. current_record.sleep.target_end_time);
  90. setSleepValues(sleepTime.split(':').map(x => parseInt(x)));
  91. setSleepStr(sleepTime);
  92. var sleepCount = current_record.sleep.target_end_time - current_record.sleep.target_start_time
  93. setSleepDuration(sleepCount)
  94. setSleepPickerValue([sleepCount / 60000 / 5 - 12])
  95. }
  96. }
  97. }
  98. function showPicker() {
  99. setIsOpen(true)
  100. }
  101. function hidePicker() {
  102. setIsOpen(false)
  103. setTimeout(() => {
  104. setResetPicker(true)
  105. }, 500)
  106. setTimeout(() => {
  107. setResetPicker(false)
  108. }, 1000)
  109. }
  110. function layoutContent() {
  111. var limit = new Date().getTime() - 7 * 3600 * 1000 * 24;
  112. var current_record = machine.context.checkData ? (machine.context.checkData as any).current_record : null;
  113. if (current_record && current_record.last_real_check_time)
  114. limit = current_record.last_real_check_time
  115. if (resetPicker) {
  116. return <View />
  117. }
  118. return <View>
  119. <LimitPickers limit={limit} onCancel={() => { setIsOpen(false) }} onChange={(e) => {
  120. console.log(new Date(e))
  121. pickerConfirm(e)
  122. hidePicker()
  123. }} />
  124. </View>
  125. }
  126. function pickerConfirm(t: number) {
  127. if (isFast) {
  128. if (value == 'WAIT_FOR_START') {
  129. // const duration = fastValues[0] * 3600 * 1000 + fastValues[1] * 60 * 1000
  130. startFast(t, fastDuration);
  131. }
  132. else {
  133. endFast(t)
  134. }
  135. }
  136. else {
  137. if (value == 'WAIT_FOR_START' || value == 'ONGOING1') {
  138. // const duration = sleepValues[0] * 3600 * 1000 + sleepValues[1] * 60 * 1000
  139. startSleep(t, sleepDuration);
  140. }
  141. else {
  142. endSleep(t)
  143. }
  144. }
  145. }
  146. function mixedBtns() {
  147. return <View>
  148. {
  149. (value == 'WAIT_FOR_START' || value == 'DONE') &&
  150. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>Start Fast</Text>
  151. }
  152. {
  153. (value == 'ONGOING'/* ||value == 'ONGOING1' || value == 'ONGOING2'*/ || value == 'ONGOING3') &&
  154. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>End Fast</Text>
  155. }
  156. {
  157. value == 'ONGOING1' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
  158. }
  159. {
  160. value == 'ONGOING2' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text>
  161. }
  162. </View>
  163. }
  164. function fastBtns() {
  165. return <View>
  166. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>{value == 'ONGOING' ? 'End Fast' : 'Start Fast'}</Text>
  167. </View>
  168. }
  169. function sleepBtns() {
  170. return <View>
  171. {
  172. value == 'ONGOING' ? <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text> :
  173. <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
  174. }
  175. </View>
  176. }
  177. const handlePickerChange = (e: string) => {
  178. var [hour, minute] = e.split(':').map(x => parseInt(x))
  179. isFast ? setFastValues([hour, minute]) : setSleepValues([hour, minute]);
  180. };
  181. function durationChange(e) {
  182. var count = e[0] * 5 + 60;
  183. isFast ? setFastDuration(count * 60000) : setSleepDuration(count * 60000);
  184. isFast ? setFastPickerValue(e[0]) : setSleepPickerValue(e[0]);
  185. }
  186. function login() {
  187. Taro.navigateTo({
  188. url: '/pages/ChooseAuth'
  189. })
  190. }
  191. if (!user.isLogin) {
  192. return <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
  193. <PickerViews onChange={() => { }} items={[pickerDurations()]} value={[12 * 15]} />
  194. {/* <TimePickers time={isFast ? fastStr : sleepStr} content="" change={handlePickerChange} isPickerView={true} /> */}
  195. <Text style={{ color: '#AAFF00' }} onClick={login}>Start Fast</Text>
  196. </View>
  197. }
  198. return (
  199. <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
  200. {
  201. machine.context.checkData && (machine.context.checkData as any).current_record.last_real_check_time &&
  202. <Text style={{ width: '100%', textAlign: 'center',color:'red' }}>
  203. {TimeFormatter.formateTime((machine.context.checkData as any).current_record.last_real_check_time)}
  204. </Text>
  205. }
  206. {
  207. value == 'ONGOING' && <View>
  208. <Text>{(machine.context.checkData as any).current_record.scenario == 'FAST' ?
  209. TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time) :
  210. TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
  211. </View>
  212. }
  213. {
  214. (value == 'ONGOING1' || value == 'WAIT_FOR_START') &&
  215. <PickerViews onChange={durationChange} items={[pickerDurations()]} value={isFast ? [fastPickerValue] : [sleepPickerValue]} />
  216. // <TimePickers time={isFast ? fastStr : sleepStr} content="" change={handlePickerChange} isPickerView={true} />
  217. }
  218. {
  219. machine.context.checkData && value == 'ONGOING2' && <View>
  220. <Text>{TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
  221. </View>
  222. }
  223. {
  224. machine.context.checkData && value == 'ONGOING3' && <View>
  225. <Text>{TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time)}</Text>
  226. </View>
  227. }
  228. <View>
  229. {
  230. key === 'FAST_SLEEP' && mixedBtns()
  231. }
  232. {
  233. key === 'FAST' && fastBtns()
  234. }
  235. {
  236. key === 'SLEEP' && sleepBtns()
  237. }
  238. </View>
  239. <AtFloatLayout
  240. isOpened={isOpen}
  241. onClose={() => {
  242. setIsOpen(false)
  243. }}
  244. title="">
  245. {
  246. layoutContent()
  247. }
  248. </AtFloatLayout>
  249. </View>
  250. )
  251. }