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
7 changes: 5 additions & 2 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ def landing

def edit_todo_item
@todo_item.update(todo_item_params)
render json: @todo_item
end

def reset_todo_items
Todo.update_all(checked: false)
@todos = Todo.all.order(:id)
render json: @todos
end

private

def todo_item_params
params.permit(:id, :title, :checked)
params.require(:home).permit(:id, :title, :checked)
end

def set_todo_item
@todo_item = Todo.find(todo_item_params[:id])
@todo_item = Todo.find(params[:id])
end
end
20 changes: 17 additions & 3 deletions app/javascript/components/TodoList/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from "react";
import React, { useState, useEffect } from "react";
import { Container, ListGroup, Form } from "react-bootstrap";
import { ResetButton } from "./uiComponent";
import axios from "axios";
Expand All @@ -14,6 +14,7 @@ type Props = {
};

const TodoList: React.FC<Props> = ({ todoItems }) => {
const [todoList, setTodoList] = useState([...todoItems]);
useEffect(() => {
const token = document.querySelector(
"[name=csrf-token]"
Expand All @@ -25,21 +26,34 @@ const TodoList: React.FC<Props> = ({ todoItems }) => {
e: React.ChangeEvent<HTMLInputElement>,
todoItemId: number
): void => {
const checked = e.target.checked;
axios.post("/todo", {
id: todoItemId,
checked: e.target.checked,
}).then(() => {
setTodoList(prevList => {
const updatedList = prevList.map(todo => {
if (todo.id === todoItemId) {
todo.checked = checked;
}
return todo;
})
return updatedList;
});
});
};

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

return (
<Container>
<h3>2022 Wish List</h3>
<ListGroup>
{todoItems.map((todo) => (
{todoList.map((todo) => (
<ListGroup.Item key={todo.id}>
<Form.Check
type="checkbox"
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
Rails.application.routes.draw do
root to: "home#landing"
post "todo", to: "home#edit_todo_item"
post "reset", to: "home#reset_todo_item"
post "reset", to: "home#reset_todo_items"
end