import { View, Image, Text } from "@tarojs/components"; import './FoodTimelineItem.scss' import Taro from "@tarojs/taro"; import { useTranslation } from "react-i18next"; import { editFoodJournal } from "@/services/foodJournal"; import { useEffect, useState } from "react"; import LimitPickers from "@/components/input/LimitPickers"; import Modal from "@/components/layout/Modal"; import { ColorType } from "@/context/themes/color"; import { useSelector } from "react-redux"; export default function Component(props: { data: any, delete: Function, preview: Function }) { const common = useSelector((state: any) => state.common); const [detail, setDetail] = useState(props.data) const [count, setCount] = useState(0) const [showModal, setShowModal] = useState(false) const [isStart, setIsStart] = useState(false) const { t } = useTranslation() useEffect(() => { setDetail(props.data) }, [props.data]) function preview() { // Taro.previewImage({ // current: props.data.cover, // urls: [props.data.cover] // }) props.preview() } function showActionSheet() { Taro.showActionSheet({ itemList: [t('feature.common.action_sheet.delete'), '餐前时间', '餐后时间'] }) .then(res => { switch (res.tapIndex) { case 0: Taro.showModal({ title: t('feature.common.modal.delete_item_title'), content: t('feature.common.modal.delete_item_content'), success: function (res) { if (res.confirm) { props.delete(); } } }) break; case 1: { setIsStart(true) setShowModal(true) } break; case 2: { setIsStart(false) setShowModal(true) } break; } }) .catch(err => { console.log(err.errMsg) }) } function updateTag(index) { var tag = common.meal_tags[index] editFoodJournal({ meal_tag: { code: tag.code } }, detail.id).then(res => { var obj = detail // obj.tags = [tag] obj.meal_tag = { code: tag.code, label: tag.label } setDetail(obj) setCount(count + 1) }).catch(e => { }) } function updateTime(e) { var date = new Date(e) var year = date.getFullYear() var month = date.getMonth() + 1 var day = date.getDate() var obj: any; obj = { start: { date: (year + '') + (month < 10 ? '0' + month : month) + (day < 10 ? '0' + day : day), timestamp: e } } if (!isStart) { obj = { end: { date: (year + '') + (month < 10 ? '0' + month : month) + (day < 10 ? '0' + day : day), timestamp: e } } } editFoodJournal({ ...obj }, detail.id).then(res => { setDetail(res) setCount(count + 1) }).catch(e => { }) } function getTime(t) { var date = new Date(t) var hour = date.getHours() var minute = date.getMinutes() return (hour < 10 ? '0' + hour : hour) + ':' + (minute < 10 ? '0' + minute : minute) } return {/* */} { common.meal_tags.map((item, index) => { return updateTag(index)}>{item.label} }) } {detail.meal_tag && detail.meal_tag.label} {detail.start && getTime(detail.start.timestamp)} { detail.end.timestamp && {'-' + getTime(detail.end.timestamp)} } {detail.start.date} { showModal && { setShowModal(false) }} confirm={() => { setShowModal(false) }}> { setShowModal(false) }} onChange={(e) => { updateTime(e) setShowModal(false) }} /> } }