ClockHeader.tsx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import { useTranslation } from "react-i18next";
  2. import TitleView from "./TitleView";
  3. import Taro from "@tarojs/taro";
  4. import { setScenario, setStep } from "@/store/scenario";
  5. import { updateScenario } from "@/store/time";
  6. import { jumpPage } from "../hooks/Common";
  7. import { useDispatch, useSelector } from "react-redux";
  8. import { useEffect, useRef, useState } from "react";
  9. import { durationDatas, durationIndex, getColor, getDurationTitle } from "../hooks/Console";
  10. import { PageContainer, View } from "@tarojs/components";
  11. import PickerViews from "@/components/input/PickerViews";
  12. import Modal from "@/components/layout/Modal.weapp";
  13. import { ColorType } from "@/context/themes/color";
  14. import { updateRecord } from "@/services/trackTimeDuration";
  15. let GradientText
  16. let useNavigation;
  17. if (process.env.TARO_ENV == 'rn') {
  18. GradientText = require('@/components/basic/GradientText').default
  19. useNavigation = require("@react-navigation/native").useNavigation
  20. }
  21. export default function ClockHeader(props: { homeData: any }) {
  22. const user = useSelector((state: any) => state.user);
  23. const time = useSelector((state: any) => state.time);
  24. const [fastPickerValue, setFastPickerValue] = useState([0, 0])
  25. const [sleepPickerValue, setSleepPickerValue] = useState([0, 0])
  26. const [fastDuration, setFastDuration] = useState<number>(0);
  27. const [sleepDuration, setSleepDuration] = useState<number>(0);
  28. const [showDurationPicker, setShowDurationPicker] = useState(false)
  29. const [showEditPicker, setShowEditPicker] = useState(false)
  30. const [currentRecord, setCurrentRecord] = useState(null)
  31. const [isFast, setIsFast] = useState(true)
  32. const common = useSelector((state: any) => state.common);
  33. const durationPickerRef = useRef(null)
  34. const isFastFirst = true;
  35. const dispatch = useDispatch();
  36. const { t } = useTranslation()
  37. let navigation;
  38. if (useNavigation) {
  39. navigation = useNavigation()
  40. }
  41. useEffect(() => {
  42. if (props.homeData) {
  43. setCurrentRecord(props.homeData.current_record)
  44. getStateDetail()
  45. }
  46. }, [props.homeData])
  47. function getStateDetail() {
  48. var current_record = props.homeData.current_record
  49. if (current_record.fast){
  50. var fastCount = current_record.fast.target_end_time - current_record.fast.target_start_time
  51. setFastDuration(fastCount)
  52. setFastPickerValue(durationIndex(current_record.fast.target_start_time, current_record.fast.target_end_time, common))
  53. }
  54. if (current_record.sleep){
  55. var sleepCount = current_record.sleep.target_end_time - current_record.sleep.target_start_time
  56. setSleepDuration(sleepCount)
  57. setSleepPickerValue(durationIndex(current_record.sleep.target_start_time, current_record.sleep.target_end_time, common))
  58. }
  59. }
  60. function tapAddBtn() {
  61. Taro.showActionSheet({
  62. itemList: [
  63. t('feature.track_time_duration.action_sheet.switch_scenario'),
  64. t('feature.track_time_duration.action_sheet.change_schedule')
  65. ]
  66. }).then(res => {
  67. switch (res.tapIndex) {
  68. case 1:
  69. if (time.status != 'WAIT_FOR_START') {
  70. Taro.showToast({
  71. title: t('feature.common.toast.ongoing'),
  72. icon: 'none'
  73. })
  74. return;
  75. }
  76. if (time.scenario == 'FAST_SLEEP') {
  77. dispatch(setStep(isFastFirst ? 'fast' : 'sleep'))
  78. }
  79. else if (time.scenario == 'SLEEP') {
  80. dispatch(setStep('sleep'))
  81. }
  82. else {
  83. dispatch(setStep('fast'))
  84. }
  85. jumpPage('/pages/clock/SetSchedule', 'SetSchedule', navigation)
  86. break;
  87. case 0:
  88. if (time.status != 'WAIT_FOR_START') {
  89. Taro.showToast({
  90. title: t('feature.common.toast.ongoing'),
  91. icon: 'none'
  92. })
  93. return;
  94. }
  95. jumpPage('/pages/clock/ChooseScenario', 'ChooseScenario', navigation)
  96. break;
  97. }
  98. })
  99. .catch(err => {
  100. console.log(err.errMsg)
  101. })
  102. }
  103. function tapAddBtn2() {
  104. var data = props.homeData.fast_sleep
  105. var status = data.current_record.status
  106. var itemList: any = []
  107. itemList.push(t('feature.track_time_duration.action_sheet.set_new_goals'))
  108. // if (status == 'WAIT_FOR_START') {
  109. // itemList.push(t('feature.track_time_duration.action_sheet.set_new_goals'))
  110. // itemList.push(t('feature.track_time_duration.action_sheet.set_fast_goal'))
  111. // itemList.push(t('feature.track_time_duration.action_sheet.set_sleep_goal'))
  112. // }
  113. // else if (status == 'ONGOING1') {
  114. // itemList.push(t('feature.track_time_duration.action_sheet.set_sleep_goal'))
  115. // itemList.push(t('feature.track_time_duration.action_sheet.edit_fasting_goal'))
  116. // }
  117. // else if (status == 'ONGOING2') {
  118. // itemList.push(t('feature.track_time_duration.action_sheet.edit_sleeping_goal'))
  119. // itemList.push(t('feature.track_time_duration.action_sheet.edit_fasting_goal'))
  120. // }
  121. // else if (status == 'ONGOING3') {
  122. // itemList.push(t('feature.track_time_duration.action_sheet.edit_fasting_goal'))
  123. // }
  124. Taro.showActionSheet({
  125. itemList: itemList
  126. }).then(res => {
  127. switch (res.tapIndex) {
  128. case 0:
  129. {
  130. if (status == 'WAIT_FOR_START') {
  131. dispatch(setStep('fast'))
  132. dispatch(updateScenario(data.current_record))
  133. dispatch(setScenario(data.scenario));
  134. jumpPage('/pages/clock/SetSchedule', 'SetSchedule', navigation)
  135. }
  136. else {
  137. Taro.showToast({
  138. title: t('feature.common.toast.ongoing'),
  139. icon: 'none'
  140. })
  141. }
  142. // else if (status == 'ONGOING1') {
  143. // setIsFast(false)
  144. // setShowDurationPicker(true)
  145. // }
  146. // else if (status == 'ONGOING2') {
  147. // setIsFast(false)
  148. // setShowEditPicker(true)
  149. // }
  150. // else if (status == 'ONGOING3') {
  151. // setIsFast(true)
  152. // setShowEditPicker(true)
  153. // }
  154. }
  155. break;
  156. case 1:
  157. {
  158. if (status == 'WAIT_FOR_START') {
  159. setIsFast(true)
  160. setShowDurationPicker(true)
  161. }
  162. else if (status == 'ONGOING1') {
  163. setIsFast(true)
  164. setShowEditPicker(true)
  165. }
  166. else if (status == 'ONGOING2') {
  167. setIsFast(true)
  168. setShowEditPicker(true)
  169. }
  170. }
  171. break;
  172. case 2: {
  173. if (status == 'WAIT_FOR_START') {
  174. Taro.showToast({
  175. title: t('feature.track_time_duration.common.start_fasting_first'),
  176. icon: 'none'
  177. })
  178. }
  179. }
  180. }
  181. })
  182. }
  183. function durationPickerContent() {
  184. var color = getColor(currentRecord)
  185. var title = getDurationTitle(currentRecord, t)
  186. return <View style={{ color: '#fff', backgroundColor: 'transparent' }}>
  187. <PickerViews ref={durationPickerRef}
  188. onChange={durationChange}
  189. items={durationDatas(common)}
  190. value={isFast ? fastPickerValue : sleepPickerValue}
  191. themeColor={color}
  192. title={title}
  193. showBtns={true}
  194. onCancel={() => {
  195. setShowDurationPicker(false)
  196. }} />
  197. </View>
  198. }
  199. function editPickerContent() {
  200. return <View style={{ color: '#fff', backgroundColor: 'transparent' }}>
  201. <PickerViews ref={durationPickerRef}
  202. onChange={durationChange}
  203. items={durationDatas(common)}
  204. value={isFast ? fastPickerValue : sleepPickerValue}
  205. themeColor={isFast ? ColorType.fast : ColorType.sleep}
  206. title={isFast ? t('feature.track_time_duration.action_sheet.edit_fasting_goal') :
  207. t('feature.track_time_duration.action_sheet.edit_sleeping_goal')}
  208. showBtns={true}
  209. onCancel={() => {
  210. setShowEditPicker(false)
  211. }} />
  212. </View>
  213. }
  214. function durationChange(e) {
  215. // debugger
  216. var count = (e[0] + common.duration.min) * 60 + e[1] * common.duration.step
  217. // var count = (e[0] + 1) * 60 + e[1] * 5
  218. if (showDurationPicker) {
  219. global.changeTargetDuration(count, isFast)
  220. }
  221. else {
  222. var params: any = {}
  223. if (isFast) {
  224. params = {
  225. fast: {
  226. target_duration: count * 60 * 1000
  227. }
  228. }
  229. }
  230. else {
  231. params = {
  232. sleep: {
  233. target_duration: count * 60 * 1000
  234. }
  235. }
  236. }
  237. updateRecord({
  238. ...params
  239. }, (currentRecord as any).id).then(res => {
  240. global.indexPageRefresh()
  241. }).catch(e => {
  242. })
  243. }
  244. setShowDurationPicker(false)
  245. setShowEditPicker(false)
  246. }
  247. function modalContent() {
  248. if (showDurationPicker || showEditPicker) {
  249. if (process.env.TARO_ENV == 'weapp') {
  250. return <Modal
  251. testInfo={null}
  252. dismiss={() => {
  253. setShowDurationPicker(false)
  254. setShowEditPicker(false)
  255. }}
  256. confirm={() => { }}>
  257. {
  258. showDurationPicker ? durationPickerContent() : editPickerContent()
  259. }
  260. </Modal>
  261. }
  262. else if (process.env.TARO_ENV == 'rn') {
  263. return <PageContainer style={{ backgroundColor: '#1c1c1c' }}
  264. // overlayStyle='background-color:rgba(0,0,0,0.9)'
  265. // custom-style='background-color:#1c1c1c'
  266. overlayStyle={{ backgroundColor: 'rgba(0,0,0,0.9)' }}
  267. customStyle={{ backgroundColor: '#1c1c1c' }}
  268. closeOnSlideDown={false}
  269. onBeforeEnter={() => {
  270. Taro.hideTabBar();
  271. }}
  272. onBeforeLeave={() => {
  273. Taro.showTabBar();
  274. }}
  275. onClick={() => { alert('b') }}
  276. onClickOverlay={() => { alert('a') }}
  277. onAfterLeave={() => { setShowDurationPicker(false); setShowEditPicker(false) }}
  278. show={showDurationPicker} round={true} overlay={true} position='bottom'
  279. >
  280. {
  281. showDurationPicker ? durationPickerContent() : editPickerContent()
  282. }
  283. </PageContainer>
  284. }
  285. }
  286. return <View />
  287. }
  288. /*function headerView() {
  289. return <TitleView title={t('page.metric.title')} showAddBtn={loaded && !showErrorPage ? true : false} onClick={addBtnClick}>
  290. </TitleView>
  291. }*/
  292. return <View>
  293. <TitleView title={t('page.clock.title')} onClick={tapAddBtn} showAddBtn={user.isLogin ? true : false}>
  294. </TitleView>
  295. {
  296. modalContent()
  297. }
  298. </View>
  299. }