Console.tsx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 LimitPickers from '@/components/LimitPickers';
  10. import { endFast, endSleep, startFast, startSleep } from "../actions/TrackTimeActions";
  11. import { durationDatas, durationIndex, pickerDurations } from "../hooks/Console";
  12. import PickerViews from "@/components/PickerViews";
  13. import Modal from "@/components/Modal";
  14. import Stepper from "@/components/Stepper";
  15. export default function Component() {
  16. const [key, setKey] = useState('');
  17. const [value, setValue] = useState('');
  18. const user = useSelector((state: any) => state.user);
  19. const common = useSelector((state: any) => state.common);
  20. const [isFast, setIsFast] = useState(true);
  21. const [fastValues, setFastValues] = useState<number[]>([0, 0]);
  22. const [sleepValues, setSleepValues] = useState<number[]>([0, 0]);
  23. const [fastDuration, setFastDuration] = useState<number>(0);
  24. const [sleepDuration, setSleepDuration] = useState<number>(0);
  25. const [fastStr, setFastStr] = useState('00:00');
  26. const [sleepStr, setSleepStr] = useState('00:00');
  27. const [isOpen, setIsOpen] = useState(false);
  28. const [showModal, setShowModal] = useState(false);
  29. const [fastPickerValue, setFastPickerValue] = useState([0, 0])
  30. const [sleepPickerValue, setSleepPickerValue] = useState([0, 0])
  31. const [isOpenDurationPicker, setIsOpenDurationPicker] = useState(false)
  32. // const [pickerValue, setPickerValue] = useState([0,0])
  33. // const pickerDurations = pickerDurations();
  34. // console.log(pickerDurations())
  35. useEffect(() => {
  36. getStateDetail();
  37. }, [machine.context.checkData]);
  38. useEffect(() => {
  39. getStateDetail();
  40. }, [machine.context.currentStatus])
  41. useEffect(() => {
  42. trackTimeService.onTransition(state => {
  43. if ((state.value as any).FAST_SLEEP) {
  44. setKey('FAST_SLEEP');
  45. setValue((state.value as any).FAST_SLEEP);
  46. }
  47. if ((state.value as any).FAST) {
  48. setKey('FAST');
  49. setValue((state.value as any).FAST);
  50. }
  51. if ((state.value as any).SLEEP) {
  52. setKey('SLEEP');
  53. setValue((state.value as any).SLEEP);
  54. }
  55. });
  56. }, []);
  57. function getStateDetail() {
  58. var state = trackTimeService.getSnapshot().value
  59. if ((state as any).FAST_SLEEP) {
  60. setKey('FAST_SLEEP');
  61. setValue((state as any).FAST_SLEEP);
  62. if ((state as any).FAST_SLEEP == 'WAIT_FOR_START' || (state as any).FAST_SLEEP == 'ONGOING3') {
  63. setIsFast(true);
  64. }
  65. else if ((state as any).FAST_SLEEP == 'ONGOING1') {
  66. setIsFast(false);
  67. }
  68. }
  69. if ((state as any).FAST) {
  70. setKey('FAST');
  71. setValue((state as any).FAST);
  72. setIsFast(true);
  73. }
  74. if ((state as any).SLEEP) {
  75. setKey('SLEEP');
  76. setValue((state as any).SLEEP);
  77. setIsFast(false);
  78. }
  79. var checkData = machine.context.checkData;
  80. if (checkData) {
  81. var current_record = (checkData as any).current_record;
  82. if (current_record.fast) {
  83. var fastTime = TimeFormatter.formateHourMinute(current_record.fast.target_start_time,
  84. current_record.fast.target_end_time);
  85. setFastValues(fastTime.split(':').map(x => parseInt(x)));
  86. setFastStr(fastTime);
  87. var fastCount = current_record.fast.target_end_time - current_record.fast.target_start_time
  88. setFastDuration(fastCount)
  89. setFastPickerValue(durationIndex(current_record.fast.target_start_time, current_record.fast.target_end_time, common))
  90. // setFastPickerValue([fastCount / 60000 / 5 - 12])
  91. }
  92. if (current_record.sleep) {
  93. var sleepTime = TimeFormatter.formateHourMinute(current_record.sleep.target_start_time,
  94. current_record.sleep.target_end_time);
  95. setSleepValues(sleepTime.split(':').map(x => parseInt(x)));
  96. setSleepStr(sleepTime);
  97. var sleepCount = current_record.sleep.target_end_time - current_record.sleep.target_start_time
  98. setSleepDuration(sleepCount)
  99. setSleepPickerValue(durationIndex(current_record.sleep.target_start_time, current_record.sleep.target_end_time, common))
  100. // setSleepPickerValue([sleepCount / 60000 / 5 - 12])
  101. }
  102. }
  103. }
  104. function showPicker() {
  105. setShowModal(true)
  106. setIsOpen(true)
  107. global.set_time = new Date().getTime()
  108. }
  109. function hidePicker() {
  110. setIsOpen(false)
  111. setTimeout(() => {
  112. setShowModal(false)
  113. }, 1000)
  114. }
  115. function layoutContent() {
  116. var limit = new Date().getTime() - 7 * 3600 * 1000 * 24;
  117. var current_record = machine.context.checkData ? (machine.context.checkData as any).current_record : null;
  118. if (current_record && current_record.last_real_check_time)
  119. limit = current_record.last_real_check_time
  120. return <View style={{ backgroundColor: '#fff' }}>
  121. <LimitPickers limit={limit} limitDay={7} onCancel={hidePicker} onChange={(e) => {
  122. console.log(new Date(e))
  123. pickerConfirm(e)
  124. hidePicker()
  125. }} />
  126. </View>
  127. }
  128. function pickerConfirm(t: number) {
  129. if (isFast) {
  130. if (value == 'WAIT_FOR_START') {
  131. // const duration = fastValues[0] * 3600 * 1000 + fastValues[1] * 60 * 1000
  132. startFast(t, fastDuration);
  133. }
  134. else {
  135. endFast(t)
  136. }
  137. }
  138. else {
  139. if (value == 'WAIT_FOR_START' || value == 'ONGOING1') {
  140. // const duration = sleepValues[0] * 3600 * 1000 + sleepValues[1] * 60 * 1000
  141. startSleep(t, sleepDuration);
  142. }
  143. else {
  144. endSleep(t)
  145. }
  146. }
  147. }
  148. function mixedBtns() {
  149. return <View>
  150. {
  151. (value == 'WAIT_FOR_START' || value == 'DONE') &&
  152. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>Start Fast</Text>
  153. }
  154. {
  155. (value == 'ONGOING'/* ||value == 'ONGOING1' || value == 'ONGOING2'*/ || value == 'ONGOING3') &&
  156. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>End Fast</Text>
  157. }
  158. {
  159. value == 'ONGOING1' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
  160. }
  161. {
  162. value == 'ONGOING2' && <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text>
  163. }
  164. </View>
  165. }
  166. function fastBtns() {
  167. return <View>
  168. <Text style={{ color: '#AAFF00' }} onClick={showPicker}>{value == 'ONGOING' ? 'End Fast' : 'Start Fast'}</Text>
  169. </View>
  170. }
  171. function sleepBtns() {
  172. return <View>
  173. {
  174. value == 'ONGOING' ? <Text style={{ color: '#00ffff' }} onClick={showPicker}>End Sleep</Text> :
  175. <Text style={{ color: '#00ffff' }} onClick={showPicker}>Start Sleep</Text>
  176. }
  177. </View>
  178. }
  179. const handlePickerChange = (e: string) => {
  180. var [hour, minute] = e.split(':').map(x => parseInt(x))
  181. isFast ? setFastValues([hour, minute]) : setSleepValues([hour, minute]);
  182. };
  183. function durationChange(e) {
  184. var count = (e[0] + common.duration.min) * 60 + e[1] * common.duration.step
  185. isFast ? setFastDuration(count * 60000) : setSleepDuration(count * 60000);
  186. isFast ? setFastPickerValue(e) : setSleepPickerValue(e)
  187. setIsOpenDurationPicker(false)
  188. }
  189. function login() {
  190. Taro.navigateTo({
  191. url: '/pages/ChooseAuth'
  192. })
  193. }
  194. function durationFormate() {
  195. if (isFast) {
  196. var t = fastDuration / 60000
  197. var hour = Math.floor(t / 60)
  198. var minute = Math.floor(t % 60)
  199. return `${hour > 0 ? hour + '小时' : ''}${minute > 0 ? minute + '分钟' : ''}`
  200. }
  201. else {
  202. var t = sleepDuration / 60000
  203. var hour = Math.floor(t / 60)
  204. var minute = Math.floor(t % 60)
  205. return `${hour > 0 ? hour + '小时' : ''}${minute > 0 ? minute + '分钟' : ''}`
  206. }
  207. }
  208. function showDurationPicker() {
  209. setIsOpenDurationPicker(true)
  210. }
  211. if (!user.isLogin) {
  212. return <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
  213. <Text>16:00</Text>
  214. {/* <PickerViews onChange={() => { }} items={[pickerDurations()]} value={[12 * 15]} /> */}
  215. {/* <TimePickers time={isFast ? fastStr : sleepStr} content="" change={handlePickerChange} isPickerView={true} /> */}
  216. <Text style={{ color: '#AAFF00' }} onClick={login}>Start Fast</Text>
  217. </View>
  218. }
  219. function durationPickerContent() {
  220. return <View style={{ color: '#000', backgroundColor: '#fff' }}>
  221. <PickerViews onChange={durationChange} items={durationDatas(common)} value={isFast ? fastPickerValue : sleepPickerValue} height={200} showBtns={true} onCancel={() => { setIsOpenDurationPicker(false) }} />
  222. </View>
  223. }
  224. return (
  225. <View style={{ display: 'flex', flexDirection: 'column', width: '100%', alignItems: 'center' }}>
  226. {
  227. machine.context.checkData && (machine.context.checkData as any).current_record.last_real_check_time &&
  228. <Text style={{ width: '100%', textAlign: 'center', color: 'red' }}>
  229. {TimeFormatter.formateTime((machine.context.checkData as any).current_record.last_real_check_time)}
  230. </Text>
  231. }
  232. {
  233. value == 'ONGOING' && <View>
  234. <Text>{(machine.context.checkData as any).current_record.scenario == 'FAST' ?
  235. TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time) :
  236. TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
  237. </View>
  238. }
  239. {
  240. (value == 'ONGOING1' || value == 'WAIT_FOR_START') && <Stepper style={{ marginBottom: 10 }} child={
  241. <Text onClick={showDurationPicker}>时长:{durationFormate()}</Text>
  242. } minus={() => { console.log('minus')}} plus={() => {console.log('plus') }} disableMinus={false} disablePlus={false} />
  243. // <Text style={{marginBottom:10}} onClick={showDurationPicker}>时长:{durationFormate()}</Text>
  244. }
  245. {
  246. machine.context.checkData && value == 'ONGOING2' && <View>
  247. <Text>{TimeFormatter.countdown((machine.context.checkData as any).current_record.sleep.target_end_time)}</Text>
  248. </View>
  249. }
  250. {
  251. machine.context.checkData && value == 'ONGOING3' && <View>
  252. <Text>{TimeFormatter.countdown((machine.context.checkData as any).current_record.fast.target_end_time)}</Text>
  253. </View>
  254. }
  255. <View>
  256. {
  257. key === 'FAST_SLEEP' && mixedBtns()
  258. }
  259. {
  260. key === 'FAST' && fastBtns()
  261. }
  262. {
  263. key === 'SLEEP' && sleepBtns()
  264. }
  265. </View>
  266. {
  267. showModal && isOpen && <Modal children={layoutContent()} dismiss={() => setIsOpen(false)} />
  268. }
  269. {
  270. isOpenDurationPicker && <Modal children={durationPickerContent()} dismiss={() => setIsOpenDurationPicker(false)} />
  271. }
  272. {/* {
  273. showModal && <AtFloatLayout
  274. isOpened={isOpen}
  275. onClose={() => {
  276. setIsOpen(false)
  277. setShowModal(false)
  278. }}
  279. title="">
  280. {
  281. layoutContent()
  282. }
  283. </AtFloatLayout>
  284. }
  285. <AtFloatLayout
  286. isOpened={isOpenDurationPicker}
  287. onClose={() => {
  288. setIsOpenDurationPicker(false)
  289. }}
  290. title="">
  291. {
  292. durationPickerContent()
  293. }
  294. </AtFloatLayout> */}
  295. </View>
  296. )
  297. }