Skip to content

title change event reaction kind of works, taking a lunch break #214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 94 additions & 13 deletions src/app/(home)/tasks/page.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

// React imports
import React, { useState, useMemo, useEffect } from 'react';
import React, { useState, useMemo, useEffect, useReducer } from 'react';

// Material UI imports
import { useTheme } from '@mui/material/styles';
Expand All @@ -19,13 +19,15 @@ import { DragDropContext } from '@hello-pangea/dnd';
import TaskHeaderCard from "@/components/Cards/TaskHeaderCard";
import TaskCard from "@/components/Cards/TaskCard";

import { Box } from "@mui/material";
import { Box, Skeleton } from "@mui/material";
import { Description } from '@mui/icons-material';
export default function Lists ()
{
const client = generateClient({ authMode: 'userPool' });

// State management
const [tasks, setTasks] = useState([]);
const [tasks, setTasks] = useState(new Map());
const [isLoading, setIsLoading] = useState(true);

// Use theme from Material UI
const theme = useTheme();
Expand All @@ -34,18 +36,32 @@ export default function Lists ()
{
client.models.Tasks.list().then(({ data, errors }) =>
{
errors ? console.error(errors) :
setTasks(data);
let newMap = new Map();
if(errors) {
console.error(errors);
} else {
for(const task of data) {
newMap.set(task.id, task);
}

setIsLoading(false);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to be raising this flag here?

setTasks(newMap);
}
});
}

useEffect(() => fetchTasks(), []);
useEffect(() => {fetchTasks()}, []);

const handleTaskDelete = async (id) =>
{
let newMap = new Map(tasks);
const { data, errors } = await client.models.Tasks.delete({ id: id });
errors ? console.error(errors) :
fetchTasks();
if (errors) {
console.log(errors);
} else {
newMap.delete(id);
setTasks(newMap);
}
console.log('deleted');
}

Expand All @@ -58,22 +74,87 @@ export default function Lists ()
important: false,
done: false
})
errors ? console.error(errors) :
fetchTasks();

if (errors) {
console.log(errors);
} else {
let newMap = (new Map(tasks)).set(data.id, data);
setTasks(newMap);
}
console.log('added a task');
}

const handleTitleChange = async (id, title) => {
const { errors, data } = await client.models.Tasks.update({
"id": id,
"title": title,
})

if (errors) {
console.log(errors);
} else {
let newMap = (new Map(tasks)).set(data.id, data);
setTasks(newMap);
}
console.log(`updated task ${title}'s title`);
}

const handleDescriptionChange = async (id, description) => {
const { errors, data } = await client.models.Tasks.update({
"id": id,
"details": description,
})

if (errors) {
console.log(errors);
} else {
let newMap = (new Map(tasks)).set(data.id, data);
setTasks(newMap);
}
console.log(`updated task ${description}'s desc`);
}

const handleImportantChange = async (id, important) => {
const { errors, data } = await client.models.Tasks.update({
"id": id,
"important": important
})

if (errors) {
console.log(errors);
} else {
let newMap = (new Map(tasks)).set(data.id, data);
setTasks(newMap);
}
console.log(`updated task ${data.title}'s importance`);
}

const handleDateChange = async (id, date) => {
const { errors, data } = await client.models.Tasks.update({
"id": id,
"date": date
})

if (errors) {
console.log(errors);
} else {
let newMap = (new Map(tasks)).set(data.id, data);
setTasks(newMap);
}
console.log(`updated task ${data.title}'s date`);
}

return (
<>
<TaskHeaderCard handleAddTask={handleTaskAddClick} />
<Box>
{
tasks.map((t, index) =>
Array.from(tasks.values()).map((t, index) =>
{
return (
(index === tasks.length - 1) ?
<TaskCard key={t.id} task={t} borderBottomRadius={'20px'} onDeleteClick={() => handleTaskDelete(t.id)} />
: <TaskCard key={t.id} task={t} onDeleteClick={() => handleTaskDelete(t.id)} />
<TaskCard key={t.id} task={t} borderBottomRadius={'20px'} onDeleteClick={() => handleTaskDelete(t.id)} onTitleChange={handleTitleChange} onDescriptionChange={handleDescriptionChange} onImportantChange={handleImportantChange} onDateChange={handleDateChange}/>
: <TaskCard key={t.id} task={t} onDeleteClick={() => handleTaskDelete(t.id)} onTitleChange={handleTitleChange} onDescriptionChange={handleDescriptionChange} onImportantChange={handleImportantChange} onDateChange={handleDateChange}/>
)
})
}
Expand Down
74 changes: 65 additions & 9 deletions src/components/Cards/TaskCard.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import React, { useState } from 'react';
import DatePickerDialog from "../Dialogs/DatePickerDialog";


export default function TaskCard ({ task, borderBottomRadius, onDeleteClick })
export default function TaskCard ({ task, borderBottomRadius, onDeleteClick, onTitleChange, onDescriptionChange, onImportantChange, onDateChange })
{

const { title, details, date, important, done } = task;

const [titleValue, setTitleValue] = useState("");
const [descrptionValue, setDescriptionValue] = useState("");
const [enterPressed, setEnterPressed] = useState(false);
const [isHovering, setIsHovering] = useState(false);
const [anchorEl, setAnchorEl] = useState(null);
const [isImportant, setIsImportant] = useState(important);
Expand All @@ -38,12 +41,65 @@ export default function TaskCard ({ task, borderBottomRadius, onDeleteClick })
const handleMouseLeave = () => setIsHovering(false);
const handleTaskOptionsClose = () => setAnchorEl(null);
const handleTaskOptionsOpen = (event) => setAnchorEl(event.currentTarget);
const handleImportantIconClick = () => setIsImportant(!isImportant);
const handleImportantIconClick = () => toggleImportant();
const handleDateIconClick = () => setOpenCalendarDialog(true);
const handleCloseDialog = () => setOpenCalendarDialog(false);

console.log(dateValue);

const handleCloseDialog = () => handleDateChange();
const handleTitleChange = (event) => setTitleValue(event.target.value);
const handleDescriptionChange = (event) => setDescriptionValue(event.target.value);

const handleTitleKeyDown = (event) => {
if (event.key === 'Enter') {
setEnterPressed(true);
saveTitleValue();
}
};

const handleDescriptionKeyDown = (event) => {
if (event.key === 'Enter') {
setEnterPressed(true);
saveDescriptionValue();
}
};

const handleTitleBlur = () => {
if (!enterPressed) {
saveTitleValue();
}
setEnterPressed(false);
};

const handleDescriptionBlur = () => {
if (!enterPressed) {
saveDescriptionValue();
}
setEnterPressed(false);
};

const toggleImportant = () => {
saveImportantValue();
setIsImportant(!isImportant);
}

const handleDateChange = () => {
saveDateValue();
setOpenCalendarDialog(false)
}

const saveDescriptionValue = () => {
onDescriptionChange(task.id, descrptionValue);
};

const saveTitleValue = () => {
onTitleChange(task.id, titleValue);
};

const saveImportantValue = () => {
onImportantChange(task.id, !isImportant)
}

const saveDateValue = () => {
onDateChange(task.id, dateValue)
}

const TaskOptionsMenu = (
<Menu
Expand Down Expand Up @@ -75,7 +131,7 @@ export default function TaskCard ({ task, borderBottomRadius, onDeleteClick })

<Box sx={{ justifyContent: 'left', display: 'flex', alignItems: 'center', flexDirection: 'row' }} onMouseEnter={handleMouseOver} onMouseLeave={handleMouseLeave}>
<Radio size='small' />
<TextField placeholder='Title' variant='standard' sx={{ width: '100%' }} InputProps={{ disableUnderline: 'true' }} defaultValue={title} />
<TextField placeholder='Title' variant='standard' sx={{ width: '100%' }} InputProps={{ disableUnderline: 'true' }} defaultValue={title} onBlur={handleTitleBlur} onChange={handleTitleChange} onKeyDown={handleTitleKeyDown}/>
{
(isHovering) ?
<>
Expand All @@ -99,7 +155,7 @@ export default function TaskCard ({ task, borderBottomRadius, onDeleteClick })

<Box sx={{ justifyContent: 'left', display: 'flex', alignItems: 'center' }}>
<NotesIcon sx={{ marginLeft: '1.1%' }} />
<TextField placeholder='Details' variant='standard' sx={{ width: '100%', marginLeft: '1%' }} InputProps={{ disableUnderline: 'true' }} defaultValue={details} />
<TextField placeholder='Details' variant='standard' sx={{ width: '100%', marginLeft: '1%' }} InputProps={{ disableUnderline: 'true' }} defaultValue={details} onBlur={handleDescriptionBlur} onChange={handleDescriptionChange} onKeyDown={handleDescriptionKeyDown}/>
</Box>

<Box sx={{ justifyContent: 'left', display: 'flex', alignItems: 'center' }}>
Expand All @@ -115,4 +171,4 @@ export default function TaskCard ({ task, borderBottomRadius, onDeleteClick })
</Box>
</>
);
}
}
Loading