DragonOsman

AddTask component, Task Scheduler

Sep 4th, 2023 (edited)
3,674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 8.09 KB | Source Code | 0 0
  1. import { useTaskContext,ITask } from "../context/taskContext";
  2. import { useNavigate, Link } from "react-router-dom";
  3. import { ChangeEvent, useState, useEffect } from "react";
  4.  
  5. const AddTask = () => {
  6.   const [{ tasks }, dispatch] = useTaskContext();
  7.   const [title, setTitle] = useState("");
  8.   const [startTime, setStartTime] = useState("");
  9.   const [endTime, setEndTime] = useState("");
  10.   const [isScheduled, setIsScheduled] = useState(false);
  11.   const [isRecurring, setIsRecurring] = useState(false);
  12.   const [isFlexible, setIsFlexible] = useState(false);
  13.   const [daysRecurring, setDaysRecurring] = useState<string[]>([]);
  14.   const [timeString, setTimeString] = useState("");
  15.   const [minutes, setMinutes] = useState(0);
  16.   const [id, setId] = useState(0);
  17.  
  18.   const navigate = useNavigate();
  19.  
  20.   const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  21.  
  22.   useEffect(() => {
  23.     const match = timeString.match(/\d+/);
  24.     const time = match ? parseInt(match[0]) : null;
  25.     if (time) {
  26.       setMinutes(time);
  27.     }
  28.   }, [timeString]);
  29.  
  30.   useEffect(() => {
  31.     const [startTimeStr, endTimeStr] = timeString.split("-");
  32.     setStartTime(new Date(`${new Date().getDate()}T${startTimeStr}`).toString());
  33.     setEndTime(new Date(`${new Date().getDate()}T${endTimeStr}`).toString());
  34.   }, [timeString]);
  35.  
  36.   return (
  37.     <div className="add-task container d-flex
  38.      container-fluid justify-content-center text-center">
  39.       <div className="row">
  40.         <div className="col-auto">
  41.           <Link to="/">
  42.             <i className="fa-solid fa-angle-left"></i>
  43.           </Link>
  44.         </div>
  45.         <div className="col d-flex justify-content-center align-items-center">
  46.           <h3 className="text-center">Add Task</h3>
  47.         </div>
  48.       </div>
  49.       <form
  50.         onSubmit={event => {
  51.           event.preventDefault();
  52.           if (tasks.length > 0) {
  53.             setId((tasks.length - 1) + 1);
  54.           } else if (tasks.length === 0) {
  55.             setId(0);
  56.           }
  57.           dispatch({ type: "ADD_TASK", payload: {
  58.             tasks,
  59.             task: {
  60.               title: title,
  61.               startTime: isFlexible ?
  62.                 new Date(new Date()) :
  63.                 new Date(startTime),
  64.               endTime: isFlexible ?
  65.               new Date(new Date().setMinutes(Number(minutes))) :
  66.               new Date(endTime),
  67.               id,
  68.               isRecurring,
  69.               daysRecurring,
  70.               isCompleted: false,
  71.               flexible: isFlexible,
  72.               scheduled: isScheduled
  73.             }
  74.           } });
  75.           navigate("/");
  76.         }}
  77.         method="post"
  78.       >
  79.         <fieldset>
  80.           <input
  81.             type="text"
  82.             name="task-title"
  83.             id="title"
  84.             title="task title"
  85.             className="task-title form-control title"
  86.             value={title}
  87.             placeholder="Title"
  88.             onChange={event => setTitle(event.target.value)}
  89.           />
  90.           <div className="form-check form-switch d-flex align-items-center">
  91.             <label htmlFor="scheduled-task" className="form-check-label
  92.             mr-2 switch-label">Scheduled Time</label>
  93.             <div className="flex-grow-1"></div>
  94.             <input
  95.               title="scheduled task setting"
  96.               type="checkbox"
  97.               name="scheduled-task"
  98.               id="scheduled"
  99.               className={`form-check-input ${isScheduled ? "bg-dark" : "bg-light"}`}
  100.               role="switch"
  101.               checked={isScheduled}
  102.               data-toggle="toggle"
  103.               onChange={(event: ChangeEvent<HTMLInputElement>) => {
  104.                 setIsScheduled(event.target.checked);
  105.               }}
  106.             />
  107.           </div>
  108.           {isScheduled ? (
  109.             <div className="row">
  110.               <div className="col-auto">
  111.                 <label htmlFor="task-timer" className="form-label timer-label">Time</label>
  112.               </div>
  113.               <div className="col">
  114.                 <input
  115.                   type="text"
  116.                   name="task-timer"
  117.                   title="task time"
  118.                   value={timeString}
  119.                   onChange={event => setTimeString(event.target.value)}
  120.                   className="form-control timer-text"
  121.                 />
  122.               </div>
  123.             </div>
  124.           ) : (
  125.             <div className="row">
  126.               <div className="col-auto">
  127.                 <label htmlFor="task-timer" className="form-label timer-label">Timer</label>
  128.               </div>
  129.               <div className="col">
  130.                 <input
  131.                   type="text"
  132.                   name="task-timer"
  133.                   title="task time"
  134.                   value={timeString}
  135.                   className="form-control timer-text"
  136.                   onChange={event => setTimeString(event.target.value)}
  137.                 />
  138.               </div>
  139.             </div>
  140.           )}
  141.           {!isRecurring && (
  142.             <>
  143.               <div className="form-check form-switch d-flex align-items-center">
  144.               <label htmlFor="flexible-task" className="form-check-label switch
  145.                switch-label mr-2">Flexible</label>
  146.               <div className="flex-grow-1"></div>
  147.               <input
  148.                 title="flexible task setting"
  149.                 type="checkbox"
  150.                 name="flexible-task"
  151.                 id="flexible"
  152.                 className={`form-check-input ${isFlexible ? "bg-dark" : "bg-light"}`}
  153.                 role="switch"
  154.                 checked={isFlexible}
  155.                 data-toggle="toggle"
  156.                 onChange={(event: ChangeEvent<HTMLInputElement>) => {
  157.                   setIsFlexible(event.target.checked);
  158.                 }}
  159.               />
  160.             </div>
  161.           </>
  162.           )}
  163.           <div className="form-check form-switch d-flex align-items-center">
  164.             <label htmlFor="recurring-task" className="form-check-label switch
  165.              switch-label mr-2">Recurring</label>
  166.             <div className="flex-grow-1"></div>
  167.             <input
  168.               title="recurring task setting"
  169.               type="checkbox"
  170.               name="recurring-task"
  171.               id="recurring"
  172.               className={`form-check-input ${isRecurring ? "bg-dark" : "bg-light"}`}
  173.               role="switch"
  174.               checked={isRecurring}
  175.               data-toggle="toggle"
  176.               onChange={(event: ChangeEvent<HTMLInputElement>) => {
  177.                 setIsRecurring(event.target.checked);
  178.               }}
  179.             />
  180.           </div>
  181.           {isRecurring && (
  182.             days.map(day => {
  183.               return (
  184.                 <button
  185.                   type="button"
  186.                   title="day recurring"
  187.                   key={day}
  188.                   className="btn btn-secondary day-btn"
  189.                   onClick={() => {
  190.                     let newDaysRecurring = [...daysRecurring];
  191.                     if (daysRecurring.includes(day)) {
  192.                       const result = window.confirm(
  193.                         `Are you sure you want to remove ${day} from the list?`
  194.                       )
  195.                       ;
  196.                       if (result) {
  197.                         newDaysRecurring = newDaysRecurring.splice(
  198.                           newDaysRecurring.indexOf(day),
  199.                           day.length
  200.                         );
  201.                       }
  202.                     }
  203.                     newDaysRecurring.push(day);
  204.                     setDaysRecurring(newDaysRecurring);
  205.                   }}
  206.                 >
  207.                   {day.charAt(0)}
  208.                 </button>
  209.               );
  210.             })
  211.           )}
  212.         </fieldset>
  213.       <input type="submit" value="Done" className="form-control btn btn-secondary submit-btn" />
  214.       {daysRecurring.map(dayRecurring => (
  215.         <ul className="days-list">
  216.           <li key={dayRecurring}>
  217.             {dayRecurring}
  218.           </li>
  219.         </ul>
  220.       ))}
  221.       </form>
  222.     </div>
  223.   );
  224. };
  225.  
  226. export default AddTask;
Advertisement
Add Comment
Please, Sign In to add comment