Skip to content
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
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def edit_todo_item
@todo_item.update(todo_item_params)
end

def reset_todo_items
def reset_todo_item
Todo.update_all(checked: false)
end

Expand Down
31 changes: 27 additions & 4 deletions app/javascript/components/TodoList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
import React, { useEffect } from "react";
import { Container, ListGroup, Form } from "react-bootstrap";
import React, { useEffect, useState } from "react";
import { Container, ListGroup, Form, ProgressBar } from "react-bootstrap";
import { ResetButton } from "./uiComponent";
import axios from "axios";

type TodoItem = {
id: number;
title: string;
checked: boolean;
created_at: Date;
};

type Props = {
todoItems: TodoItem[];
};

const TodoList: React.FC<Props> = ({ todoItems }) => {
const [todoItemsState, setTodoItemsState] = useState(todoItems)
const [progressBar, setProgressBar] = useState(0);
useEffect(() => {
const token = document.querySelector(
"[name=csrf-token]"
Expand All @@ -23,23 +26,41 @@ const TodoList: React.FC<Props> = ({ todoItems }) => {

const checkBoxOnCheck = (
e: React.ChangeEvent<HTMLInputElement>,
todoItemId: number
todoItemId: number,
): void => {
axios.post("/todo", {
id: todoItemId,
checked: e.target.checked,
});
const newState = todoItemsState.map(obj => {
if(obj.id == todoItemId){
return {...obj, checked: !obj.checked}
}else{
return obj;
}
})
setTodoItemsState(newState);
};

const resetButtonOnClick = (): void => {
axios.post("/reset").then(() => location.reload());
};

useEffect(() => {
const progressItem = [];
todoItemsState.map(item => {
if(item.checked){
progressItem.push(item)
}
})
setProgressBar((progressItem.length/todoItemsState.length)*100)
}, [todoItemsState])

return (
<Container>
<h3>2022 Wish List</h3>
<ListGroup>
{todoItems.map((todo) => (
{todoItemsState.map((todo) => (
<ListGroup.Item key={todo.id}>
<Form.Check
type="checkbox"
Expand All @@ -49,6 +70,8 @@ const TodoList: React.FC<Props> = ({ todoItems }) => {
/>
</ListGroup.Item>
))}
<br />
<ProgressBar variant="success" now={progressBar}/>
<ResetButton onClick={resetButtonOnClick}>Reset</ResetButton>
</ListGroup>
</Container>
Expand Down
2 changes: 1 addition & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.