DurationPicker.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. import { PageContainer, View } from "@tarojs/components"
  2. import { useEffect, useRef, useState } from "react"
  3. import { durationDatas, durationIndex, getColor, getDurationTitle } from "../hooks/Console"
  4. import { useSelector } from "react-redux"
  5. import Taro from "@tarojs/taro"
  6. import { useTranslation } from "react-i18next"
  7. import PickerViews from "@/components/input/PickerViews"
  8. import { ColorType } from "@/context/themes/color"
  9. import { updateRecord } from "@/services/trackTimeDuration"
  10. import Modal from "@/components/layout/Modal.weapp";
  11. export default function DurationPicker(props: { record: any }) {
  12. const [showDurationPicker, setShowDurationPicker] = useState(false)
  13. const [fastPickerValue, setFastPickerValue] = useState([0, 0])
  14. const [sleepPickerValue, setSleepPickerValue] = useState([0, 0])
  15. const [showEditPicker, setShowEditPicker] = useState(false)
  16. const [isFast, setIsFast] = useState(true)
  17. const durationPickerRef = useRef(null)
  18. const { t } = useTranslation()
  19. const common = useSelector((state: any) => state.common);
  20. useEffect(() => {
  21. getStateDetail()
  22. }, [props.record])
  23. function getStateDetail() {
  24. var current_record = props.record
  25. if (current_record.fast)
  26. setFastPickerValue(durationIndex(current_record.fast.target_start_time, current_record.fast.target_end_time, common))
  27. if (current_record.sleep)
  28. setSleepPickerValue(durationIndex(current_record.sleep.target_start_time, current_record.sleep.target_end_time, common))
  29. }
  30. global.showFastPicker = () => {
  31. setFastDuration()
  32. }
  33. global.showSleepPicker = () => {
  34. setSleepDuration()
  35. }
  36. function setFastDuration() {
  37. setIsFast(true)
  38. if (props.record.status == 'WAIT_FOR_START') {
  39. setShowDurationPicker(true)
  40. }
  41. else {
  42. setShowEditPicker(true)
  43. }
  44. }
  45. function setSleepDuration() {
  46. setIsFast(false)
  47. if (props.record.status == 'WAIT_FOR_START') {
  48. Taro.showToast({
  49. title: t('feature.track_time_duration.common.start_fasting_first'),
  50. icon: 'none'
  51. })
  52. return;
  53. }
  54. if (props.record.status == 'WAIT_FOR_START' || props.record.status == 'ONGOING1') {
  55. setShowDurationPicker(true)
  56. }
  57. else {
  58. setShowEditPicker(true)
  59. }
  60. }
  61. function durationPickerContent() {
  62. var color = getColor(props.record)
  63. var title = getDurationTitle(props.record, t)
  64. return <View style={{ color: '#fff', backgroundColor: 'transparent' }}>
  65. <PickerViews ref={durationPickerRef}
  66. onChange={durationChange}
  67. items={durationDatas(common)}
  68. value={isFast ? fastPickerValue : sleepPickerValue}
  69. themeColor={color}
  70. title={title}
  71. showBtns={true}
  72. onCancel={() => {
  73. setShowDurationPicker(false)
  74. }} />
  75. </View>
  76. }
  77. function editPickerContent() {
  78. return <View style={{ color: '#fff', backgroundColor: 'transparent' }}>
  79. <PickerViews ref={durationPickerRef}
  80. onChange={durationChange}
  81. items={durationDatas(common)}
  82. value={isFast ? fastPickerValue : sleepPickerValue}
  83. themeColor={isFast ? ColorType.fast : ColorType.sleep}
  84. title={isFast ? t('feature.track_time_duration.action_sheet.edit_fasting_goal') :
  85. t('feature.track_time_duration.action_sheet.edit_sleeping_goal')}
  86. showBtns={true}
  87. onCancel={() => {
  88. setShowEditPicker(false)
  89. }} />
  90. </View>
  91. }
  92. function durationChange(e) {
  93. // debugger
  94. var count = (e[0] + common.duration.min) * 60 + e[1] * common.duration.step
  95. // var count = (e[0] + 1) * 60 + e[1] * 5
  96. if (showDurationPicker) {
  97. global.changeTargetDuration(count, isFast)
  98. }
  99. else {
  100. var params: any = {}
  101. if (isFast) {
  102. params = {
  103. fast: {
  104. target_duration: count * 60 * 1000
  105. }
  106. }
  107. }
  108. else {
  109. params = {
  110. sleep: {
  111. target_duration: count * 60 * 1000
  112. }
  113. }
  114. }
  115. updateRecord({
  116. ...params
  117. }, props.record.id).then(res => {
  118. global.indexPageRefresh()
  119. }).catch(e => {
  120. })
  121. }
  122. setShowDurationPicker(false)
  123. setShowEditPicker(false)
  124. }
  125. function modalContent() {
  126. if (showDurationPicker || showEditPicker) {
  127. return <Modal
  128. testInfo={null}
  129. dismiss={() => {
  130. setShowDurationPicker(false)
  131. setShowEditPicker(false)
  132. }}
  133. confirm={() => { }}>
  134. {
  135. showDurationPicker ? durationPickerContent() : editPickerContent()
  136. }
  137. </Modal>
  138. }
  139. return <View />
  140. }
  141. return <View style={{ width: 0, height: 0 }}>
  142. {
  143. modalContent()
  144. }
  145. </View>
  146. }