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 (
- {todo.name} -
+
+ {editable ? ( +
+ + setData("name", e.target.value)} + /> +
+ +
- +
+ ) : + + {todo.name} + + } + {errors.name && errors.name} +
+ +
+ Remover +
) diff --git a/app/frontend/pages/Todos/Show.jsx b/app/frontend/pages/Todos/Show.jsx new file mode 100644 index 0000000..acb63e1 --- /dev/null +++ b/app/frontend/pages/Todos/Show.jsx @@ -0,0 +1,26 @@ +import React from "react"; + +const TodosShow = ({todo}) => { + + return ( +
+
+

+ Tarefa {todo.name} +

+
+ +
+

+ Criado em: {todo.created_at} +

+

+ Atualizado em: {todo.updated_at} +

+
+ +
+ ) +} + +export default TodosShow; \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 1767153..eade30c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - resources :todos + resources :todos, except: [:new, :edit] root "todos#index" end