diff --git a/app/controllers/characters_controller.rb b/app/controllers/characters_controller.rb index 4316fa7..3756299 100644 --- a/app/controllers/characters_controller.rb +++ b/app/controllers/characters_controller.rb @@ -1,20 +1,20 @@ class CharactersController < ApplicationController - before_action :set_character, only: [:destroy] + before_action :set_character, only: [:update, :destroy] rescue_from ActiveRecord::RecordInvalid, with: :render_unprocessable_entity_response rescue_from ActiveRecord::RecordNotFound, with: :render_not_found_response # GET /characters def index # Get all characters belonging to the current user - @character = @current_user.characters + @characters = @current_user.characters # Checks to see if a character exists with the current user - if @character.blank? === true + if @characters.blank? === true # If no character exists, return the render_not_found message render_not_found_response else # Render the characters in JSON format with a "ok" status code 200 - render json: @character, status: :ok + render json: @characters, status: :ok end end @@ -53,7 +53,13 @@ def create # Render the new character in JSON format with a "created" status code 201 render json: @character, status: 201 - end + end + + # PATCH/PUT /activities/1 + def update + @character.update!(character_params) + render json: @character, status: :accepted + end # DELETE /characters/:id def destroy diff --git a/client/src/Components/CharacterCard.js b/client/src/Components/CharacterCard.js index 61eca6e..1aa7ce7 100644 --- a/client/src/Components/CharacterCard.js +++ b/client/src/Components/CharacterCard.js @@ -1,8 +1,11 @@ +import { useState } from "react" import { useNavigate, useLocation } from "react-router-dom" import axios from 'axios' // component that renders the details of a single character function CharacterCard({ onDelete }) { + const [editable, setEditable] = useState(false) + const [points, setPoints] = useState(0) // useLocation hook to access the state passed in from CharacterList component and save it to a variable const { state } = useLocation() @@ -24,7 +27,55 @@ function CharacterCard({ onDelete }) { }) navigate('/characters') } - + + const handleSubtract = (stat) => { + if (points < 30 && character.stats[stat] > 0) { + setPoints(points + 1) + console.log(points) + character.stats[stat] = character.stats[stat] - 1 + } + } + + const handleAdd = (stat) => { + if (points > 0) { + setPoints(points - 1) + character.stats[stat] = character.stats[stat] + 1 + } + } + + const handleUpdate = () => { + if (points === 0) { + axios.patch(`/characters/${character.id}`, { stats: character.stats }) + } else { + alert("Please spend the rest of your points.") + } + } + + const handleEditable = () => { + setEditable(!editable) + } + + console.log(character) + + const stats = Object.keys(character.stats).map((stat) => ( +
Character's Stats: -
Available Points: {points}
+