ClockHeader.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 { getPlans, updateRecord } from "@/services/trackTimeDuration";
  15. import showActionSheet from "@/components/basic/ActionSheet";
  16. import { chooseMode, initTarget, updateStep } from "@/store/set_target";
  17. let GradientText
  18. let useNavigation;
  19. let useActionSheet;
  20. if (process.env.TARO_ENV == 'rn') {
  21. GradientText = require('@/components/basic/GradientText').default
  22. useNavigation = require("@react-navigation/native").useNavigation
  23. useActionSheet = require('@expo/react-native-action-sheet').useActionSheet
  24. }
  25. export default function ClockHeader(props: { homeData: any }) {
  26. const user = useSelector((state: any) => state.user);
  27. const time = useSelector((state: any) => state.time);
  28. const ring = useSelector((state: any) => state.ring);
  29. const [fastPickerValue, setFastPickerValue] = useState([0, 0])
  30. const [sleepPickerValue, setSleepPickerValue] = useState([0, 0])
  31. const [fastDuration, setFastDuration] = useState<number>(0);
  32. const [sleepDuration, setSleepDuration] = useState<number>(0);
  33. const [showDurationPicker, setShowDurationPicker] = useState(false)
  34. const [showEditPicker, setShowEditPicker] = useState(false)
  35. const [currentRecord, setCurrentRecord] = useState(null)
  36. const [isFast, setIsFast] = useState(true)
  37. const common = useSelector((state: any) => state.common);
  38. const durationPickerRef = useRef(null)
  39. const isFastFirst = true;
  40. const dispatch = useDispatch();
  41. const { t } = useTranslation()
  42. let navigation, showActionSheetWithOptions;
  43. if (useNavigation) {
  44. navigation = useNavigation()
  45. showActionSheetWithOptions = useActionSheet()
  46. }
  47. useEffect(() => {
  48. if (props.homeData) {
  49. setCurrentRecord(props.homeData.current_record)
  50. getStateDetail()
  51. }
  52. }, [props.homeData])
  53. function getStateDetail() {
  54. var current_record = props.homeData.current_record
  55. if (current_record.fast) {
  56. var fastCount = current_record.fast.target_end_time - current_record.fast.target_start_time
  57. setFastDuration(fastCount)
  58. setFastPickerValue(durationIndex(current_record.fast.target_start_time, current_record.fast.target_end_time, common))
  59. }
  60. if (current_record.sleep) {
  61. var sleepCount = current_record.sleep.target_end_time - current_record.sleep.target_start_time
  62. setSleepDuration(sleepCount)
  63. setSleepPickerValue(durationIndex(current_record.sleep.target_start_time, current_record.sleep.target_end_time, common))
  64. }
  65. }
  66. function tapAddBtn() {
  67. showActionSheet({
  68. showActionSheetWithOptions: showActionSheetWithOptions,
  69. itemList: [
  70. t('feature.track_time_duration.action_sheet.switch_scenario'),
  71. t('feature.track_time_duration.action_sheet.suggest_schedule'),
  72. t('feature.track_time_duration.action_sheet.change_schedule')
  73. ],
  74. success: (res) => {
  75. switch (res) {
  76. case 0:
  77. if (time.status != 'WAIT_FOR_START') {
  78. Taro.showToast({
  79. title: t('feature.common.toast.ongoing'),
  80. icon: 'none'
  81. })
  82. return;
  83. }
  84. jumpPage('/pages/clock/ChooseScenario?trigger_event=SETUP_AFTER_ACCOUNT_CREATION', 'ChooseScenario', navigation, {
  85. trigger_event: 'SETUP_AFTER_ACCOUNT_CREATION'
  86. })
  87. break;
  88. case 1:
  89. if (time.status != 'WAIT_FOR_START') {
  90. Taro.showToast({
  91. title: t('feature.common.toast.ongoing'),
  92. icon: 'none'
  93. })
  94. return;
  95. }
  96. getPlans().then(res => {
  97. const data = res as { scenarios: any[] };
  98. console.log(data)
  99. var targets: any = {}
  100. data.scenarios.forEach((item) => {
  101. if (item.name == 'FAST') {
  102. if (item.schedule.fast.duration.prev_input) {
  103. item.schedule.fast.duration.last_value = item.schedule.fast.duration.init_value
  104. }
  105. targets.fast = item
  106. }
  107. else if (item.name == 'SLEEP') {
  108. targets.sleep = item
  109. }
  110. })
  111. dispatch(initTarget(targets))
  112. dispatch(updateStep({ step: 0 }))
  113. dispatch(chooseMode({ isMixed: ring.current_record.scenario == 'FAST_SLEEP' }))
  114. jumpPage(`/pages/clock/Suggest?trigger_event=SETUP_AFTER_ACCOUNT_CREATION`, 'Suggest', navigation, {
  115. trigger_event: 'SETUP_AFTER_ACCOUNT_CREATION'
  116. })
  117. })
  118. break;
  119. case 2:
  120. if (time.status != 'WAIT_FOR_START') {
  121. Taro.showToast({
  122. title: t('feature.common.toast.ongoing'),
  123. icon: 'none'
  124. })
  125. return;
  126. }
  127. getPlans().then(res => {
  128. const data = res as { scenarios: any[] };
  129. console.log(data)
  130. var targets: any = {}
  131. data.scenarios.forEach((item) => {
  132. if (item.name == 'FAST') {
  133. if (item.schedule.fast.duration.prev_input) {
  134. item.schedule.fast.duration.last_value = item.schedule.fast.duration.init_value
  135. }
  136. targets.fast = item
  137. }
  138. else if (item.name == 'SLEEP') {
  139. targets.sleep = item
  140. }
  141. })
  142. dispatch(initTarget(targets))
  143. dispatch(updateStep({ step: 2 }))
  144. dispatch(chooseMode({ isMixed: ring.current_record.scenario == 'FAST_SLEEP' }))
  145. jumpPage(`/pages/clock/SetGoal?upgrade=true&isSelf=1&trigger_event=SETUP_AFTER_ACCOUNT_CREATION`, 'SetGoal', navigation, {
  146. trigger_event: 'SETUP_AFTER_ACCOUNT_CREATION',
  147. upgrade: true,
  148. isSelf: 1
  149. })
  150. })
  151. break;
  152. }
  153. }
  154. })
  155. }
  156. function durationPickerContent() {
  157. var color = getColor(currentRecord)
  158. var title = getDurationTitle(currentRecord, t)
  159. return <View style={{ color: '#fff', backgroundColor: 'transparent' }}>
  160. <PickerViews ref={durationPickerRef}
  161. onChange={durationChange}
  162. items={durationDatas(common)}
  163. value={isFast ? fastPickerValue : sleepPickerValue}
  164. themeColor={color}
  165. title={title}
  166. showBtns={true}
  167. onCancel={() => {
  168. setShowDurationPicker(false)
  169. }} />
  170. </View>
  171. }
  172. function editPickerContent() {
  173. return <View style={{ color: '#fff', backgroundColor: 'transparent' }}>
  174. <PickerViews ref={durationPickerRef}
  175. onChange={durationChange}
  176. items={durationDatas(common)}
  177. value={isFast ? fastPickerValue : sleepPickerValue}
  178. themeColor={isFast ? ColorType.fast : ColorType.sleep}
  179. title={isFast ? t('feature.track_time_duration.action_sheet.edit_fasting_goal') :
  180. t('feature.track_time_duration.action_sheet.edit_sleeping_goal')}
  181. showBtns={true}
  182. onCancel={() => {
  183. setShowEditPicker(false)
  184. }} />
  185. </View>
  186. }
  187. function durationChange(e) {
  188. // debugger
  189. var count = (e[0] + common.duration.min) * 60 + e[1] * common.duration.step
  190. // var count = (e[0] + 1) * 60 + e[1] * 5
  191. if (showDurationPicker) {
  192. global.changeTargetDuration(count, isFast)
  193. }
  194. else {
  195. var params: any = {}
  196. if (isFast) {
  197. params = {
  198. fast: {
  199. target_duration: count * 60 * 1000
  200. }
  201. }
  202. }
  203. else {
  204. params = {
  205. sleep: {
  206. target_duration: count * 60 * 1000
  207. }
  208. }
  209. }
  210. updateRecord({
  211. ...params
  212. }, (currentRecord as any).id).then(res => {
  213. global.indexPageRefresh()
  214. if (global.refreshStreaks){
  215. global.refreshStreaks()
  216. }
  217. }).catch(e => {
  218. })
  219. }
  220. setShowDurationPicker(false)
  221. setShowEditPicker(false)
  222. }
  223. function modalContent() {
  224. if (showDurationPicker || showEditPicker) {
  225. return <Modal
  226. testInfo={null}
  227. dismiss={() => {
  228. setShowDurationPicker(false)
  229. setShowEditPicker(false)
  230. }}
  231. confirm={() => { }}>
  232. {
  233. showDurationPicker ? durationPickerContent() : editPickerContent()
  234. }
  235. </Modal>
  236. }
  237. return <View />
  238. }
  239. /*function headerView() {
  240. return <TitleView title={t('page.metric.title')} showAddBtn={loaded && !showErrorPage ? true : false} onClick={addBtnClick}>
  241. </TitleView>
  242. }*/
  243. return <View>
  244. <TitleView title={t('page.clock.title')} onClick={tapAddBtn} showAddBtn={user.isLogin ? true : false}>
  245. </TitleView>
  246. {
  247. modalContent()
  248. }
  249. </View>
  250. }