Advertisement
fatalryuu

Untitled

Oct 2nd, 2023
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React, { useEffect, useRef, useState } from 'react';
  2. import CalendarMonthIcon from '@mui/icons-material/CalendarMonth';
  3. import { Box, Checkbox, Grid, Typography } from '@mui/material';
  4. import { convertDateToStringWithoutTime } from 'common/helpers/convertDateToString';
  5.  
  6. import { ProgressBar } from './ProgressBar/ProgressBar';
  7. import {
  8.     buttonStyles,
  9.     buttonTextStyles,
  10.     seeMoreStyles,
  11.     taskDateStyles,
  12.     taskDescriptionStyles,
  13.     taskNameStyles,
  14.     taskWrapperStyles,
  15. } from './styles';
  16.  
  17. interface TaskProps {
  18.     name: string;
  19.     description: string;
  20.     deadline: Date;
  21.     progress: number;
  22.     isDone: boolean;
  23.     isNew: boolean;
  24. }
  25.  
  26. const TWO_DAYS_IN_MILLISECONDS = 2 * 24 * 60 * 60 * 1000;
  27.  
  28. const getDateColor = (deadline: Date) => {
  29.     const now = new Date();
  30.     const timeLeft = deadline.getTime() - now.getTime();
  31.     if (timeLeft < 0) {
  32.         return 'red';
  33.     } else if (timeLeft <= TWO_DAYS_IN_MILLISECONDS) {
  34.         return 'orange';
  35.     } else {
  36.         return 'gray';
  37.     }
  38. };
  39.  
  40. export const Task: React.FC<TaskProps> = ({ name, description, deadline, progress, isDone, isNew }) => {
  41.     const [progressState, setProgressState] = useState(progress);
  42.     const [isTaskDone, setIsTaskDone] = useState(isDone);
  43.     const [isTaskNew, setIsTaskNew] = useState(isNew);
  44.     const [isSliderVisible, setIsSliderVisible] = useState(false);
  45.     const taskRef = useRef<HTMLDivElement>();
  46.     const date = convertDateToStringWithoutTime(deadline);
  47.     const [taskWidth, setTaskWidth] = useState(taskRef.current?.clientWidth ?? 0);
  48.  
  49.     // adaptive design
  50.     useEffect(() => {
  51.         const updateTaskWidth = () => {
  52.             setTaskWidth(taskRef.current?.clientWidth ?? 0);
  53.         };
  54.         setTaskWidth(taskRef.current?.clientWidth ?? 0);
  55.         window.addEventListener('resize', updateTaskWidth);
  56.         return () => window.removeEventListener('resize', updateTaskWidth);
  57.     }, []);
  58.  
  59.     useEffect(() => {
  60.         progressState === 100 && setIsTaskDone(true);
  61.     }, [progressState]);
  62.  
  63.     const handleMouseEnter = () => {
  64.         if (isTaskNew) {
  65.             setIsTaskNew(false);
  66.             // update task in db
  67.         }
  68.     };
  69.  
  70.     const handleBtnClick = () => {
  71.         setIsTaskDone((prev) => !prev);
  72.         // update task in db
  73.     };
  74.  
  75.     const handleSeeMore = () => {
  76.         // open modal
  77.     };
  78.  
  79.     return (
  80.         <Box onMouseEnter={handleMouseEnter} sx={taskWrapperStyles(isTaskNew)} ref={taskRef}>
  81.             {/* top */}
  82.             <Grid container justifyContent="space-between" mb={2}>
  83.                 {/* name */}
  84.                 <Grid item xs={4}>
  85.                     <Typography sx={taskNameStyles}>{name}</Typography>
  86.                 </Grid>
  87.                 {/* date */}
  88.                 <Grid item minWidth="7rem" display="flex" color={isTaskDone ? 'gray' : getDateColor(deadline)}>
  89.                     <Typography sx={taskDateStyles}>{date}</Typography>
  90.                     <CalendarMonthIcon />
  91.                 </Grid>
  92.                 {/* progress */}
  93.                 <ProgressBar
  94.                     progress={progressState}
  95.                     setProgress={setProgressState}
  96.                     isSliderVisible={isSliderVisible}
  97.                     setIsSliderVisible={setIsSliderVisible}
  98.                     isTaskDone={isTaskDone}
  99.                     isTaskNew={isTaskNew}
  100.                 />
  101.                 {/* button */}
  102.                 <Grid item>
  103.                     <Box sx={buttonStyles}>
  104.                         <Checkbox color="success" checked={isTaskDone} onChange={handleBtnClick} />
  105.                         <Typography sx={buttonTextStyles}>Mark as done</Typography>
  106.                     </Box>
  107.                 </Grid>
  108.             </Grid>
  109.             {/* middle */}
  110.             <Grid container justifyContent="space-between">
  111.                 {/* description (48 because of 24px * 2 paddings)*/}
  112.                 <Box sx={taskDescriptionStyles(taskWidth - 48)}>{description}</Box>
  113.                 {/* see more */}
  114.                 <Typography sx={seeMoreStyles} onClick={handleSeeMore}>
  115.                     See more
  116.                 </Typography>
  117.             </Grid>
  118.         </Box>
  119.     );
  120. };
  121.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement