add_label.tsx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { View, Text, Input, Switch } from "@tarojs/components";
  2. import './add_label.scss'
  3. import { useEffect, useState } from "react";
  4. import Modal from "@/components/layout/Modal.weapp";
  5. import TimePicker from "@/features/common/TimePicker";
  6. import dayjs from "dayjs";
  7. import { MainColorType } from "@/context/themes/color";
  8. import { createSchedule, getLabelsTime, getSchedules } from "@/services/health";
  9. import Taro from "@tarojs/taro";
  10. import { useSelector } from "react-redux";
  11. import { getThemeColor } from "@/features/health/hooks/health_hooks";
  12. export default function AddLabel(props: { labels: any, defaultValue?: string, disMiss?: any }) {
  13. const [showTimePicker, setShowTimePicker] = useState(false)
  14. const health = useSelector((state: any) => state.health);
  15. const [value, setValue] = useState(props.defaultValue ?? '')
  16. const [isFullday, setIsFullday] = useState(false)
  17. const [timeLabels, setTimeLabels] = useState<any>([])
  18. const [strTime, setStrTime] = useState('')
  19. // function timeContent() {
  20. // return <Modal
  21. // testInfo={null}
  22. // dismiss={() => {
  23. // setShowTimePicker(false)
  24. // }}
  25. // confirm={() => { }}>
  26. // {
  27. // pickerContent()
  28. // }
  29. // </Modal>
  30. // }
  31. useEffect(() => {
  32. getData()
  33. }, [])
  34. function getData() {
  35. getLabelsTime({}).then(res => {
  36. setTimeLabels((res as any).labels)
  37. })
  38. }
  39. function pickerContent() {
  40. const strTime = dayjs().format('HH:mm')
  41. return <TimePicker time={strTime}
  42. color={getThemeColor(health.mode)}
  43. title='开始时间'
  44. confirm={(e) => {
  45. confirmPickerTime(e)
  46. }}
  47. cancel={() => {
  48. setShowTimePicker(false)
  49. }} />
  50. }
  51. function confirmPickerTime(e) {
  52. console.log(e)
  53. createSchedule({
  54. schedules: [{
  55. event: health.mode == 'EAT' ? 'EAT_CUSTOM' : 'ACTIVE_CUSTOM',
  56. title: value,
  57. time: e, is_all_day: false,
  58. }]
  59. }).then(res => {
  60. global.refreshWindow()
  61. global.refreshSchedules()
  62. // if (process.env.TARO_ENV == 'weapp') {
  63. // Taro.navigateBack()
  64. // }
  65. })
  66. }
  67. function done() {
  68. createSchedule({
  69. schedules: [{
  70. event: health.mode == 'EAT' ? 'EAT_CUSTOM' : 'ACTIVE_CUSTOM',
  71. title: value,
  72. time: dayjs().format('HH:mm'),
  73. is_all_day: isFullday,
  74. time_label: isFullday ? strTime : null
  75. }]
  76. }).then(res => {
  77. if (global.refreshWindow) {
  78. global.refreshWindow()
  79. }
  80. if (global.refreshSchedules) {
  81. global.refreshSchedules()
  82. }
  83. setShowTimePicker(false)
  84. if (props.disMiss) {
  85. props.disMiss()
  86. }
  87. // if (process.env.TARO_ENV == 'weapp') {
  88. // Taro.navigateBack()
  89. // }
  90. })
  91. }
  92. function timeContent() {
  93. return <View>
  94. <View className="header1">Set Time</View>
  95. <View style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between' }}>
  96. <Text>全天时间</Text>
  97. <Switch
  98. color={getThemeColor(health.mode)}
  99. onChange={(e) => {
  100. setIsFullday(e.detail.value)
  101. // item.reminder = e.detail.value
  102. // setList([...list])
  103. }} />
  104. </View>
  105. {
  106. isFullday ? <View>
  107. <Input className="input_lb" placeholder="选择或输入时间标签" value={strTime} onInput={(e: any) => {
  108. setStrTime(e.target.value)
  109. }} />
  110. <View className="label_bg">
  111. {
  112. timeLabels.map((item, index) => {
  113. return <View className="label" key={index} onClick={() => setStrTime(item.title)}>{item.title}</View>
  114. })
  115. }
  116. </View>
  117. </View> :
  118. <View style={{ flexDirection: 'column', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
  119. <Text>{dayjs().format('HH:mm')}</Text>
  120. <Text>Picker placeholder</Text>
  121. </View>
  122. }
  123. <View className="pop_footer" style={{ backgroundColor: getThemeColor(health.mode) }} onClick={() => done()}>完成</View>
  124. </View>
  125. }
  126. return <View>
  127. <View className="header1">标记为</View>
  128. <View className="body1">
  129. <Input className="input_lb" placeholder="选择或输入标签" value={value} onInput={(e: any) => {
  130. setValue(e.target.value)
  131. }} />
  132. <View className="label_bg">
  133. {
  134. props.labels.map((item, index) => {
  135. return <View className="label" key={index} onClick={() => setValue(item.title)}>{item.title}</View>
  136. })
  137. }
  138. </View>
  139. </View>
  140. <View className="pop_footer" style={{ backgroundColor: getThemeColor(health.mode) }} onClick={() => setShowTimePicker(true)}>下一步</View>
  141. {/* {
  142. showTimePicker && timeContent()
  143. } */}
  144. {
  145. showTimePicker && <Modal testInfo={null}
  146. dismiss={() => {
  147. setShowTimePicker(false)
  148. }}
  149. confirm={() => { }}>
  150. {
  151. timeContent()
  152. }
  153. </Modal>
  154. }
  155. </View>
  156. }