diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5455975..97c1e9b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,4 @@ class ApplicationController < ActionController::Base - private def serialize(resource, serializer_class) diff --git a/app/controllers/todos_controller.rb b/app/controllers/todos_controller.rb index 4baef23..0569805 100644 --- a/app/controllers/todos_controller.rb +++ b/app/controllers/todos_controller.rb @@ -1,5 +1,5 @@ class TodosController < ApplicationController - before_action :set_todo, only: [:show, :edit, :update, :destroy] + before_action :set_todo, only: [:show, :update, :destroy] # GET /todos or /todos.json def index @@ -12,14 +12,6 @@ def show render inertia: "Todos/Show", props: { todo: serialize(@todo, TodoSerializer) } end - # GET /todos/new - def new - @todo = Todo.new - end - - # GET /todos/1/edit - def edit; end - # POST /todos or /todos.json def create @todo = Todo.new(todo_params) @@ -33,14 +25,10 @@ def create # PATCH/PUT /todos/1 or /todos/1.json def update - respond_to do |format| - if @todo.update(todo_params) - format.html { redirect_to todo_url(@todo), notice: "Todo was successfully updated." } - format.json { render :show, status: :ok, location: @todo } - else - format.html { render :edit, status: :unprocessable_entity } - format.json { render json: @todo.errors, status: :unprocessable_entity } - end + if @todo.update(todo_params) + redirect_to todos_url(@todo), notice: "Todo was successfully edited." + else + redirect_to todos_url(@todo), inertia: { errors: { name: "Nome inválido" } } end end diff --git a/app/frontend/components/Todo.jsx b/app/frontend/components/Todo.jsx index 7b3143a..4a19398 100644 --- a/app/frontend/components/Todo.jsx +++ b/app/frontend/components/Todo.jsx @@ -1,20 +1,56 @@ -import React from "react"; -import {Link} from "@inertiajs/react"; +import React, {useState} from "react"; +import {Link, useForm} from "@inertiajs/react"; const Todo = ({todo}) => { + const [editable, setEditable] = useState(false); + + const {data, setData, patch, processing, errors} = useForm({ + name: todo.name + }); + + const submit = (e) => { + e.preventDefault() + patch(`/todos/${todo.id}`, {preserveState: true}) + setEditable(false); + } + return (
+ Criado em: {todo.created_at} +
++ Atualizado em: {todo.updated_at} +
+