transporter-app/components/task.js

213 lines
7.8 KiB
JavaScript
Raw Normal View History

2023-12-13 12:20:39 +03:00
import Card from "./card";
import {useEffect, useState} from "react";
2024-06-26 23:24:26 +03:00
import {Pressable, Image, Linking, View} from "react-native";
2023-12-13 12:20:39 +03:00
// Icons
import TickActive from '../assets/ph_check-fill.svg'
import TickDanger from '../assets/tick_danger.svg'
import TickSuccess from '../assets/tick_success.svg'
import Chevron from '../assets/bi_chevron.svg'
2024-06-26 23:24:26 +03:00
import TickError from '../assets/codicon_error.svg'
2023-12-13 12:20:39 +03:00
2024-06-26 23:24:26 +03:00
const Tooltip = ({timeTill}) => {
const d = new Date(timeTill)
return (
<View style={{backgroundColor: "#ecd98d", position: "absolute", top: 0, right: 0, padding:10, borderRadius: 8}}>
<Card.TextSmall>{`${d.getDate()}.${d.getMonth() + 1}.${d.getFullYear()}`}</Card.TextSmall>
<Card.TextSmall>{`${d.toLocaleTimeString()}`}</Card.TextSmall>
</View>
)
}
const Task = ({id, status, title, description, time_till: timeTill, onPress, time_finished: timeFinished, imageType, navi, type}) => {
const [intervalId, setIntervalId] = useState(null);
2023-12-13 12:20:39 +03:00
const [leastTime, setLeastTime] = useState("");
const variantSelection = () => {
2024-06-26 23:24:26 +03:00
if (type && type === 'event') {
switch (status) {
case "active":
return 'default'
case "completed":
return 'success'
}
}
2023-12-13 12:20:39 +03:00
switch (status) {
case "active":
if (new Date().getTime() <= new Date(timeTill)) {
return "default"
}
return "error"
2024-06-26 23:24:26 +03:00
case "uncompleted":
return "error"
2023-12-13 12:20:39 +03:00
case "completed":
if (new Date(timeFinished).getTime() <= new Date(timeTill).getTime()) {
return "success"
}
return "danger"
}
}
const countDays = () => {
const date = new Date(timeTill);
let now = !!timeFinished ? new Date(timeFinished) : new Date();
const diffTime = Math.abs(date - now);
return Math.floor(diffTime / (1000 * 60 * 60 * 24));
}
const countHours = () => {
const date = new Date(timeTill);
let now = !!timeFinished ? new Date(timeFinished) : new Date();
if (now.getTime() <= date.getTime()) {
now = now.getTime() + countDays() * 24 * 60 * 60 * 1000;
} else {
now = now.getTime() - countDays() * 24 * 60 * 60 * 1000;
}
const diffTime = Math.abs(date - now);
return Math.floor(diffTime / (1000 * 60 * 60));
}
const countMinutes = () => {
const date = new Date(timeTill);
let now = !!timeFinished ? new Date(timeFinished) : new Date();
if (now.getTime() <= date.getTime()) {
now = now.getTime() + countDays() * 24 * 60 * 60 * 1000 + countHours() * 60 * 60 * 1000;
} else {
now = now.getTime() - countDays() * 24 * 60 * 60 * 1000 - countHours() * 60 * 60 * 1000;
}
const diffTime = Math.abs(date - now);
return Math.floor(diffTime / (1000 * 60));
}
const absTime = () => {
const query = {
start: (d, idx) => {
const val = +(d.split('').slice(-2).join(''))
if (val > 10 && val < 20) return [`${d} дней`, `${d} часов`, `${d} минут`][idx]
return query['1'](d, idx)
}
,
'1': (d, idx) => {
const val = +(d.split('').slice(-1))
if (val === 1) return [`${d} день`, `${d} час`, `${d} минута`][idx]
return query['2-4'](d, idx)
},
'2-4': (d, idx) => {
const val = +(d.split('').slice(-1))
if (val > 1 && val < 5) return [`${d} дня`, `${d} часа`, `${d} минуты`][idx]
return [`${d} дней`, `${d} часов`, `${d} минут`][idx]
},
}
let days = query.start(`${countDays()}`, 0)
const hours = query.start(`${countHours()}`, 1)
const minutes = query.start(`${countMinutes()}`, 2)
setLeastTime(`${days} ${hours} ${minutes}`)
}
useEffect(() => {
2024-06-26 23:24:26 +03:00
if (intervalId !== null) {
clearInterval(intervalId)
setIntervalId(null)
}
2023-12-13 12:20:39 +03:00
absTime()
if (status !== 'active') return;
2024-06-26 23:24:26 +03:00
setIntervalId(setInterval(() => absTime(), 1000))
}, [timeTill, timeFinished]);
2023-12-13 12:20:39 +03:00
const SelectImage = () => {
if (imageType === 'chevron') {
return <Chevron style={{transform: [{rotate: '270deg'}]}}/>
}
switch (status) {
case "active":
2024-06-26 23:24:26 +03:00
return <TickActive/>
2023-12-13 12:20:39 +03:00
case "completed":
if (new Date(timeFinished).getTime() <= new Date(timeTill).getTime()) {
2024-06-26 23:24:26 +03:00
return <TickSuccess/>
2023-12-13 12:20:39 +03:00
}
2024-06-26 23:24:26 +03:00
return <TickDanger/>
case "uncompleted":
return <TickError/>
2023-12-13 12:20:39 +03:00
}
}
return (
2024-06-26 23:24:26 +03:00
<Pressable onPress={onPress}>
2023-12-13 12:20:39 +03:00
<Card.Block variant={variantSelection()}
2024-06-26 23:24:26 +03:00
image={<SelectImage/>}
tooltip={type !== 'event' && <Tooltip timeTill={timeTill} />}>
2023-12-13 12:20:39 +03:00
<Card.Header>
2024-06-26 23:24:26 +03:00
{title.split("|").map(txt => {
if (txt.replace("<b>", '').replace("</b>", '') !== txt) {
return <Card.TitleNumber>{txt.replace('<b>', '').replace('</b>', '')}</Card.TitleNumber>
}
else {
return <Card.TextSmall>{txt}</Card.TextSmall>
}
})}
2023-12-13 12:20:39 +03:00
</Card.Header>
<Card.Body>
{!!description && <Card.TextSmall>{description}</Card.TextSmall>}
2024-06-26 23:24:26 +03:00
{!!navi && <Pressable style={{
borderColor: "#ECA704",
borderWidth: 2,
borderRadius: 40,
padding: 18,
display: 'flex',
flexDirection: "row",
alignItems: 'center',
marginBottom: 10
}}
onPress={() => Linking.openURL(`yandexnavi://build_route_on_map?lat_to=${navi.lat}&lon_to=${navi.lon}`)}>
<Image source={require('../assets/yandexNavi.png')} width={25} height={25}
style={{marginRight: 10}}></Image>
2023-12-13 12:20:39 +03:00
<Card.TextSmall style={{color: "#000"}}>Открыть в яндекс картах</Card.TextSmall>
</Pressable>}
{
2024-06-26 23:24:26 +03:00
type !== 'event' && status === 'active' && new Date(timeTill) >= new Date() &&
<Card.TextSmall>Осталось времени: </Card.TextSmall>
2023-12-13 12:20:39 +03:00
}
{
2024-06-26 23:24:26 +03:00
type !== 'event' && status === 'active' && new Date(timeTill) < new Date() &&
<Card.TextSmall>Опоздание: </Card.TextSmall>
2023-12-13 12:20:39 +03:00
}
{
2024-06-26 23:24:26 +03:00
type !== 'event' && status === 'completed' && new Date(timeFinished) <= new Date(timeTill) &&
2023-12-13 12:20:39 +03:00
<Card.TextSmall>Завершено раньше на: </Card.TextSmall>
}
{
2024-06-26 23:24:26 +03:00
type !== 'event' && status === 'completed' && new Date(timeFinished) > new Date(timeTill) &&
2023-12-13 12:20:39 +03:00
<Card.TextSmall>Завершено c опозданием на: </Card.TextSmall>
}
2024-06-26 23:24:26 +03:00
{
type !== 'event' && status !== 'uncompleted' && <Card.TitleExtra>{leastTime}</Card.TitleExtra>
}
2023-12-13 12:20:39 +03:00
</Card.Body>
</Card.Block>
</Pressable>
)
}
export default Task;