FoodTimeline.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { View } from '@tarojs/components'
  2. import './FoodTimeline.scss'
  3. import FoodTimelineItem from './FoodTimelineItem'
  4. import { useEffect, useState } from 'react'
  5. import { delFoodJournal } from '@/services/foodJournal'
  6. import Taro from '@tarojs/taro'
  7. export default function Component(props: { array: any, refresh: Function, forceRefresh: Function }) {
  8. const [list, setList] = useState(props.array)
  9. useEffect(() => {
  10. setList(props.array)
  11. }, [props.array, props.array.length])
  12. function del(index) {
  13. delFoodJournal(list[index].id).then(res => {
  14. var temps = list.splice(index, 1)
  15. setList(temps)
  16. props.refresh()
  17. }).catch(e => {
  18. })
  19. }
  20. // function preview(index){
  21. // var urls:any = []
  22. // list.map(item=>{
  23. // urls.push(item.cover)
  24. // })
  25. // Taro.previewImage({
  26. // current: list[index].cover,
  27. // urls: urls
  28. // })
  29. // }
  30. function preview(index) {
  31. var urls: any = []
  32. list.map(item => {
  33. urls.push({
  34. url: item.media[0].url,
  35. type: item.media[0].url.indexOf('mp4') != -1 ? 'video' : 'image'
  36. })
  37. })
  38. Taro.previewMedia({
  39. current: index,
  40. sources: urls
  41. })
  42. }
  43. function updateItem(index, data) {
  44. list[index] = data
  45. setList(list)
  46. }
  47. return <View style={{ flexDirection: 'column', display: 'flex' }}>
  48. {
  49. list.map((item, index) => {
  50. return <FoodTimelineItem data={item} index={index} key={index}
  51. delete={() => del(index)}
  52. preview={() => preview(index)}
  53. update={(data) => updateItem(index, data)}
  54. forceRefresh={() => props.forceRefresh()}
  55. refresh={() => {props.refresh();console.log('oppppp');debugger}}
  56. />
  57. })
  58. }
  59. </View>
  60. }