1- import { useEffect , useState } from "react" ;
2- import { useRecurringRequest } from "../../libs/Datastore/requestHelpers" ;
3- import { deleteTask , getSubtasks } from "../../libs/Datastore/requests" ;
4- import ProgressSpinner from "../ProgressSpinner/progressSpinner" ;
1+ import useSWR from "swr" ;
2+ import { deleteTask , getSubtasksRequest } from "../../libs/Datastore/requests" ;
53import FetchError from "../FetchError/fetchError" ;
4+ import ProgressSpinner from "../ProgressSpinner/progressSpinner" ;
65import RemoveableListItem from "../RemoveableListItem/removeableListItem" ;
76import StyledCheckbox from "../StyledCheckbox/styledCheckbox" ;
87import jotting from "./jotting.module.css" ;
8+ import Link from "next/link" ;
9+ import Jotting from "../../libs/Jotting" ;
10+ import { useRouter } from "next/router" ;
11+
12+ const fetcher = ( id ) => getSubtasksRequest ( id ) . makeRequest ( new AbortController ( ) ) ;
913
1014/**
1115 * @param { {id : number } } props The info about the task
1216 * @param {number } props.id The id of the parent task
1317 */
1418export default function SubtaskList ( { id, subtasksState } ) {
1519 const [ subtasks , setSubtasks ] = subtasksState ;
20+ const { data, error } = useSWR ( id , fetcher ) ;
21+ const router = useRouter ( ) ;
1622
17- // componentDidUpdate() - recur subtasks
18- useRecurringRequest ( ( ) => {
19- getSubtasks ( id )
20- . then ( ( response ) => {
21- if ( response != subtasks )
22- setSubtasks ( response ) ;
23- } )
24- . catch ( ( ) => { } ) ;
25- } ) ;
23+ if ( error ) return < FetchError itemName = "subtasks" /> ;
24+ if ( ! data )
25+ return < ProgressSpinner /> ;
2626
2727 const handleRemoveClick = ( e ) => {
2828 const itemId = e . target . parentElement . id . substring ( 2 ) ;
@@ -41,28 +41,45 @@ export default function SubtaskList({ id, subtasksState }) {
4141 . catch ( alert ) ;
4242 } ;
4343
44- if ( subtasks instanceof Array )
45- return (
46- < ul className = { jotting . subtasks } >
47- { subtasks . map ( ( item ) => {
48- return (
49- < RemoveableListItem
50- key = { item . id }
51- handleRemoveClick = { handleRemoveClick }
52- removeText = "X"
53- id = { "s-" + item . id }
54- content = { item . title }
44+ return (
45+ < ul className = { jotting . subtasks } >
46+ { data . data . map ( ( item ) => {
47+ return (
48+ < li key = { item . id } className = "removeableListItem" >
49+ < StyledCheckbox
50+ id = { item . id }
51+ prefix = "subtask-completion-box-"
52+ completed = { item . completed == 1 }
53+ />
54+ < button className = { jotting . subtaskTitle } onClick = { ( ) => Jotting . openJotting ( router , "task" , item ) } > { item . title } </ button >
55+ < button
56+ onClick = { handleRemoveClick }
57+ className = "removeItemButton"
5558 >
56- < StyledCheckbox
57- id = { item . id }
58- prefix = "subtask-completion-box-"
59- completed = { item . completed == 1 }
60- />
61- </ RemoveableListItem >
62- ) ;
63- } ) }
64- </ ul >
65- ) ;
66- else if ( subtasks != null ) return < FetchError itemName = "subtasks" /> ;
67- else return < ProgressSpinner /> ;
59+ X
60+ </ button >
61+ < ul className = { jotting . subsubtasks } >
62+ { item . subtasks . map ( grandchild => (
63+ < li key = { grandchild . id } >
64+ < StyledCheckbox
65+ id = { grandchild . id }
66+ prefix = "subtask-completion-box-"
67+ completed = { grandchild . completed == 1 }
68+ />
69+ < button className = { jotting . subtaskTitle } onClick = { ( ) => Jotting . openJotting ( router , "task" , grandchild ) } > { grandchild . title } </ button >
70+ < button
71+ className = "removeItemButton"
72+ > X</ button >
73+ </ li >
74+ ) ) }
75+ </ ul >
76+ </ li >
77+
78+ ) ;
79+ } ) }
80+ </ ul >
81+ ) ;
82+
83+
84+
6885}
0 commit comments