schedules_time.tsx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import Modal from "@/components/layout/Modal.weapp";
  2. import TimePicker from "@/features/common/TimePicker";
  3. import { getScenario, getThemeColor } from "@/features/health/hooks/health_hooks";
  4. import { createSchedule, getSchedules } from "@/services/health";
  5. import { View, Text } from "@tarojs/components";
  6. import Taro from "@tarojs/taro";
  7. import { useEffect, useState } from "react";
  8. import { useSelector } from "react-redux";
  9. import './edit.scss'
  10. export default function SchedulesTime() {
  11. const health = useSelector((state: any) => state.health);
  12. const [scenario, setScenario] = useState(getScenario(health.windows, health.mode))
  13. const [showTimePicker, setShowTimePicker] = useState(false)
  14. const [list, setList] = useState<any>([])
  15. const [selItem, setSelItem] = useState<any>(null)
  16. useEffect(() => {
  17. schedules()
  18. }, [])
  19. function schedules() {
  20. getSchedules({ window: health.mode, is_all_day: false }).then(res => {
  21. if ((res as any).data && (res as any).data.length > 0) {
  22. setList((res as any).data)
  23. }
  24. }).catch(e => {
  25. })
  26. }
  27. function changeTime(item) {
  28. setSelItem(item)
  29. setShowTimePicker(true)
  30. }
  31. function modalContent() {
  32. const strTime = selItem.time
  33. var title = selItem.title
  34. var color = getThemeColor(health.mode)
  35. return <TimePicker time={strTime}
  36. color={color}
  37. title={title}
  38. confirm={(e) => {
  39. confirmPickerTime(e)
  40. }}
  41. cancel={() => {
  42. setShowTimePicker(false)
  43. }} />
  44. }
  45. function confirmPickerTime(e) {
  46. console.log(e)
  47. setShowTimePicker(false)
  48. selItem.time = e;
  49. setList([...list])
  50. }
  51. function save() {
  52. var array: any = []
  53. list.map((item) => {
  54. array.push({
  55. id: item.id,
  56. time: item.time,
  57. event: item.event,
  58. title: item.title
  59. })
  60. })
  61. createSchedule({
  62. schedules: array
  63. }).then(res => {
  64. global.refreshWindow()
  65. global.refreshSchedules()
  66. if (process.env.TARO_ENV == 'weapp') {
  67. Taro.navigateBack()
  68. }
  69. })
  70. }
  71. if (list.length == 0) return <View />
  72. return <View>
  73. {
  74. list.map((item, index) => {
  75. return <View className="edit_item_cell" key={index} onClick={() => changeTime(item)}>
  76. <Text className="cell_index">{index + 1}</Text>
  77. <Text>{item.title}</Text>
  78. <View style={{flex:1}}/>
  79. <Text>{item.time}</Text>
  80. {
  81. index <= list.length - 1 && <View className='edit_item_cell_line' />
  82. }
  83. </View>
  84. })
  85. }
  86. <View className="edit_footer_btn" style={{ color: getThemeColor(health.mode), backgroundColor: getThemeColor(health.mode) + '33' }} onClick={save}>完成</View>
  87. {
  88. showTimePicker && <Modal
  89. testInfo={null}
  90. dismiss={() => {
  91. setShowTimePicker(false)
  92. }}
  93. confirm={() => { }}>
  94. {
  95. modalContent()
  96. }
  97. </Modal>
  98. }
  99. </View>
  100. }