From 2a5ffac5b9ca586073460a02e41b1e2eb2d0e977 Mon Sep 17 00:00:00 2001 From: Bilal Date: Sat, 12 Sep 2020 01:17:52 +0300 Subject: [PATCH 1/2] enable add/remove homie --- client/src/App.js | 13 ++-- client/src/RouteNames.js | 1 + client/src/chips/ListChips.js | 5 +- client/src/common/errorHelpers.js | 7 ++ client/src/homies/Homies.js | 74 ++++++++++++++++++ client/src/homies/NewHomieForm.js | 122 ++++++++++++++++++++++++++++++ 6 files changed, 213 insertions(+), 9 deletions(-) create mode 100644 client/src/common/errorHelpers.js create mode 100644 client/src/homies/Homies.js create mode 100644 client/src/homies/NewHomieForm.js diff --git a/client/src/App.js b/client/src/App.js index 89a5c22..ee9060c 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -5,13 +5,15 @@ import MakeMoneyMove from './cash/MakeMoneyMove'; import Flow from "./homies/Flow"; import Cash from './cash/Cash'; import Chips from './chips/Chips'; +import Homies from './homies/Homies'; import { BrowserRouter as Router, Route } from "react-router-dom"; import RoutableNavItem from './common/RoutableNavItem'; import { CRIB, CHIPS, MAKE_MONEY_MOVE, - HOMIE_FLOW + HOMIE_FLOW, + HOMIES } from './RouteNames'; @@ -24,12 +26,12 @@ function App() { Crib - - Chips + + Homies - - Homies + + Chips @@ -40,6 +42,7 @@ function App() {
+ diff --git a/client/src/RouteNames.js b/client/src/RouteNames.js index 5ce2b05..d956223 100644 --- a/client/src/RouteNames.js +++ b/client/src/RouteNames.js @@ -1,4 +1,5 @@ export const CRIB = '/'; +export const HOMIES = '/homies'; export const CHIPS = '/chips'; export const MAKE_MONEY_MOVE = '/make-money-move'; export const HOMIE_FLOW = '/homie/:homie_id/flow'; diff --git a/client/src/chips/ListChips.js b/client/src/chips/ListChips.js index 8eb92b3..b36bc48 100644 --- a/client/src/chips/ListChips.js +++ b/client/src/chips/ListChips.js @@ -3,6 +3,7 @@ import axios from "axios"; import { Table, Collapsible, CollapsibleItem, Button, TextInput, Row, Col, Select } from "react-materialize"; import M from "materialize-css"; import YesNoModal from "../common/YesNoModal"; +import { errorToast } from "../common/errorHelpers"; const ListChips = (props) => { const [chipsList, setChipsList] = useState([]); @@ -114,10 +115,6 @@ const ListChips = (props) => { } } - - - const errorToast = () => M.toast({ html: "Yo! It ain't workin'" }); - const getChipData = (chipId) => chipsList.find(chip => chip.id === chipId); const checkIfPairExists = (baseChipId, secondaryChipId) => { diff --git a/client/src/common/errorHelpers.js b/client/src/common/errorHelpers.js new file mode 100644 index 0000000..3fc7d84 --- /dev/null +++ b/client/src/common/errorHelpers.js @@ -0,0 +1,7 @@ +import M from "materialize-css"; + +const errorToast = () => M.toast({ html: "Yo! It ain't workin'" }); + +export { + errorToast +} \ No newline at end of file diff --git a/client/src/homies/Homies.js b/client/src/homies/Homies.js new file mode 100644 index 0000000..3e7ffdd --- /dev/null +++ b/client/src/homies/Homies.js @@ -0,0 +1,74 @@ +import React, { useState, useEffect } from 'react'; +import { withRouter } from 'react-router-dom'; +import axios from 'axios'; +import { errorToast } from "../common/errorHelpers"; +import NewHomieForm from './NewHomieForm'; +import { Button } from 'react-materialize'; +import M from 'materialize-css'; +import YesNoModal from "../common/YesNoModal"; + +const Homies = (props) => { + const [homies, setHomies] = useState([]); + + useEffect(() => { + (async() => { + try { + const response = await axios.get(`/api/homies`); + if (response.status === 200 && response.data){ + setHomies(response.data); + }else{ + errorToast(); + } + } catch (e) { + errorToast(); + } + })(); + }, []); + + const deleteHomie = async (id) => { + try { + const response = await axios.delete(`/api/homies/${id}`); + if (response.status === 200 && response.data){ + setHomies(response.data); + M.toast({ html: "See y'a on the other side Homie" }); + }else{ + errorToast(); + } + }catch (e) { + console.log(e); + errorToast(); + } + } + + const homiesData = homies.map((homie, index) => { + return ( +
  • +
    +
    +
    +
    { homie.name }
    +
    { homie.about }
    +
    + + deleteHomie(homie.id)} + triggerNode={
    +
    +
  • + ) + }); + + return ( +
    + + +
      + { homiesData } +
    +
    + ) +} + +export default withRouter(Homies); \ No newline at end of file diff --git a/client/src/homies/NewHomieForm.js b/client/src/homies/NewHomieForm.js new file mode 100644 index 0000000..2e9d9f9 --- /dev/null +++ b/client/src/homies/NewHomieForm.js @@ -0,0 +1,122 @@ +import React, { useState, useEffect } from 'react'; +import { Button, Collapsible, CollapsibleItem, Select, TextInput } from 'react-materialize'; +import axios from 'axios'; +import { errorToast } from "../common/errorHelpers"; +import M from 'materialize-css'; + +const NewHomieForm = (props) => { + const [homieName, setHomieName] = useState(""); + const [aboutHomie, setAboutHomie] = useState(""); + const [homieImportance, setHomieImportance] = useState(""); + const [homieDefaultChip, setHomieDefaultChip] = useState(""); + const [chips, setChips] = useState([]); + const [busy, setBusy] = useState(false); + + useEffect(() => { + (async () => { + try { + const response = await axios.get('/api/chips'); + if (response.status === 200 && response.data) { + setChips(response.data); + }else { + errorToast(); + } + }catch (e) { + errorToast(); + } + })(); + }, []); + + const chipOptions = chips.map((chip, index) => ); + + const disableAddButton = () => { + return homieName.length === 0 || + homieImportance === "" || + homieDefaultChip === "" || + busy; + } + + const clearForm = () => { + setHomieName(""); + setAboutHomie(""); + setHomieImportance(""); + setHomieDefaultChip(""); + + const collapsible = document.getElementById('new-homie-form-container'); + const collapsibleInstance = M.Collapsible.getInstance(collapsible); + + collapsibleInstance.close(0); + } + + const addNewHomie = async () => { + setBusy(true); + const newHomie = { + homie: { + name: homieName, + about: aboutHomie, + importance: parseInt(homieImportance), + chip_id: parseInt(homieDefaultChip) + } + } + + try{ + const response = await axios.post('/api/homies', newHomie); + if (response.status === 200 && response.data) { + props.newHomiesSetter(response.data); + M.toast({ html: 'Welcome to the hood' }); + clearForm(); + }else{ + errorToast(); + } + }catch (e) { + console.log(e.message); + errorToast(); + } + + setBusy(false); + } + + return ( +
    + + Introduce new homie to the Hood}> +
    + setHomieName(e.target.value)} /> + + +
    + + setAboutHomie(e.target.value)} + /> + + + + + + + +
    + + + +
    +
    +
    + ) +} + +export default NewHomieForm; \ No newline at end of file -- 2.47.3 From 013e4d144d3a357460da4f29aea9f03b05c8eea1 Mon Sep 17 00:00:00 2001 From: Bilal Date: Sat, 12 Sep 2020 01:26:56 +0300 Subject: [PATCH 2/2] handle add/remove homies in backend --- app/controllers/chips_controller.rb | 2 +- app/controllers/homies_controller.rb | 36 ++++++++++++------- app/models/homie.rb | 8 ++--- ...200911162613_add_default_chip_to_homies.rb | 5 +++ ..._default_value_for_importance_in_homies.rb | 5 +++ db/schema.rb | 6 ++-- 6 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 db/migrate/20200911162613_add_default_chip_to_homies.rb create mode 100644 db/migrate/20200911171657_change_default_value_for_importance_in_homies.rb diff --git a/app/controllers/chips_controller.rb b/app/controllers/chips_controller.rb index 2f70b67..b19e27a 100644 --- a/app/controllers/chips_controller.rb +++ b/app/controllers/chips_controller.rb @@ -1,6 +1,6 @@ class ChipsController < ApplicationController def index - json_response Chip.all.order(:name).to_json(include: :base_chip_values) + json_response Chip.where(enabled: true).order(:name).to_json(include: :base_chip_values) end def create diff --git a/app/controllers/homies_controller.rb b/app/controllers/homies_controller.rb index 57a1386..38fc39e 100644 --- a/app/controllers/homies_controller.rb +++ b/app/controllers/homies_controller.rb @@ -1,29 +1,39 @@ class HomiesController < ApplicationController def index - json_response(Homie.all.order(:created_at)) + json_response(Homie.all.order(importance: :desc, name: :asc)) end def create - homie = Homie.new(homie_params) - if homie.save - json_response(homie) - else - error_response(:bad_request) - end + homie = Homie.new(homie_params) + if homie.save + index + else + error_response(:bad_request) + end end - + + def destroy + id = params[:id].to_i + if Homie.destroy(id) + index + else + error_response(:bad_request) + end + end + def cash importance = params[:importance].to_i json_response(Homie.cash(importance)) end private - def homie_params + + def homie_params params.require(:homie).permit( - :name, - :importance, - :about + :name, + :importance, + :about, + :chip_id ) end - end diff --git a/app/models/homie.rb b/app/models/homie.rb index 9fe94fa..f09ff31 100644 --- a/app/models/homie.rb +++ b/app/models/homie.rb @@ -1,13 +1,13 @@ class Homie < ApplicationRecord has_many :money_moves + belongs_to :chip def self.cash(importance) - totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount) + totals = Homie.all.joins(:money_moves).group(:id).order(:id).sum(:amount) - result = [] Homie.where(["importance > ?", importance]).map do |homie| - total = totals.fetch(homie.id, 0) - { homie: homie, amount: total } + total = totals.fetch(homie.id, 0) + { homie: homie, amount: total } end end end diff --git a/db/migrate/20200911162613_add_default_chip_to_homies.rb b/db/migrate/20200911162613_add_default_chip_to_homies.rb new file mode 100644 index 0000000..bd0eb9a --- /dev/null +++ b/db/migrate/20200911162613_add_default_chip_to_homies.rb @@ -0,0 +1,5 @@ +class AddDefaultChipToHomies < ActiveRecord::Migration[5.2] + def change + add_reference :homies, :chip + end +end diff --git a/db/migrate/20200911171657_change_default_value_for_importance_in_homies.rb b/db/migrate/20200911171657_change_default_value_for_importance_in_homies.rb new file mode 100644 index 0000000..47e929c --- /dev/null +++ b/db/migrate/20200911171657_change_default_value_for_importance_in_homies.rb @@ -0,0 +1,5 @@ +class ChangeDefaultValueForImportanceInHomies < ActiveRecord::Migration[5.2] + def change + change_column_default :homies, :importance, 100 + end +end diff --git a/db/schema.rb b/db/schema.rb index babee0e..52ccf15 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2020_08_27_142428) do +ActiveRecord::Schema.define(version: 2020_09_11_171657) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -37,10 +37,12 @@ ActiveRecord::Schema.define(version: 2020_08_27_142428) do create_table "homies", force: :cascade do |t| t.text "name", null: false - t.integer "importance", default: 5, null: false + t.integer "importance", default: 100, null: false t.text "about" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.bigint "chip_id" + t.index ["chip_id"], name: "index_homies_on_chip_id" end create_table "money_moves", force: :cascade do |t| -- 2.47.3