Merge branch 'allow-adding-and-removing-homies' into 'master'
Allow adding and removing homies See merge request saburly/gangsta/roraccounting!12
This commit was merged in pull request #12.
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
class ChipsController < ApplicationController
|
class ChipsController < ApplicationController
|
||||||
def index
|
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
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
|
|||||||
@@ -1,29 +1,39 @@
|
|||||||
class HomiesController < ApplicationController
|
class HomiesController < ApplicationController
|
||||||
def index
|
def index
|
||||||
json_response(Homie.all.order(:created_at))
|
json_response(Homie.all.order(importance: :desc, name: :asc))
|
||||||
end
|
end
|
||||||
|
|
||||||
def create
|
def create
|
||||||
homie = Homie.new(homie_params)
|
homie = Homie.new(homie_params)
|
||||||
if homie.save
|
if homie.save
|
||||||
json_response(homie)
|
index
|
||||||
else
|
else
|
||||||
error_response(:bad_request)
|
error_response(:bad_request)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def destroy
|
||||||
|
id = params[:id].to_i
|
||||||
|
if Homie.destroy(id)
|
||||||
|
index
|
||||||
|
else
|
||||||
|
error_response(:bad_request)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def cash
|
def cash
|
||||||
importance = params[:importance].to_i
|
importance = params[:importance].to_i
|
||||||
json_response(Homie.cash(importance))
|
json_response(Homie.cash(importance))
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def homie_params
|
|
||||||
|
def homie_params
|
||||||
params.require(:homie).permit(
|
params.require(:homie).permit(
|
||||||
:name,
|
:name,
|
||||||
:importance,
|
:importance,
|
||||||
:about
|
:about,
|
||||||
|
:chip_id
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
class Homie < ApplicationRecord
|
class Homie < ApplicationRecord
|
||||||
has_many :money_moves
|
has_many :money_moves
|
||||||
|
belongs_to :chip
|
||||||
|
|
||||||
def self.cash(importance)
|
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|
|
Homie.where(["importance > ?", importance]).map do |homie|
|
||||||
total = totals.fetch(homie.id, 0)
|
total = totals.fetch(homie.id, 0)
|
||||||
{ homie: homie, amount: total }
|
{ homie: homie, amount: total }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -5,13 +5,15 @@ import MakeMoneyMove from './cash/MakeMoneyMove';
|
|||||||
import Flow from "./homies/Flow";
|
import Flow from "./homies/Flow";
|
||||||
import Cash from './cash/Cash';
|
import Cash from './cash/Cash';
|
||||||
import Chips from './chips/Chips';
|
import Chips from './chips/Chips';
|
||||||
|
import Homies from './homies/Homies';
|
||||||
import { BrowserRouter as Router, Route } from "react-router-dom";
|
import { BrowserRouter as Router, Route } from "react-router-dom";
|
||||||
import RoutableNavItem from './common/RoutableNavItem';
|
import RoutableNavItem from './common/RoutableNavItem';
|
||||||
import {
|
import {
|
||||||
CRIB,
|
CRIB,
|
||||||
CHIPS,
|
CHIPS,
|
||||||
MAKE_MONEY_MOVE,
|
MAKE_MONEY_MOVE,
|
||||||
HOMIE_FLOW
|
HOMIE_FLOW,
|
||||||
|
HOMIES
|
||||||
} from './RouteNames';
|
} from './RouteNames';
|
||||||
|
|
||||||
|
|
||||||
@@ -24,12 +26,12 @@ function App() {
|
|||||||
Crib
|
Crib
|
||||||
</RoutableNavItem>
|
</RoutableNavItem>
|
||||||
|
|
||||||
<RoutableNavItem href={CHIPS}>
|
<RoutableNavItem href={HOMIES}>
|
||||||
Chips
|
Homies
|
||||||
</RoutableNavItem>
|
</RoutableNavItem>
|
||||||
|
|
||||||
<RoutableNavItem>
|
<RoutableNavItem href={CHIPS}>
|
||||||
Homies
|
Chips
|
||||||
</RoutableNavItem>
|
</RoutableNavItem>
|
||||||
|
|
||||||
<RoutableNavItem href={MAKE_MONEY_MOVE}>
|
<RoutableNavItem href={MAKE_MONEY_MOVE}>
|
||||||
@@ -40,6 +42,7 @@ function App() {
|
|||||||
|
|
||||||
<div className="autoscrolling">
|
<div className="autoscrolling">
|
||||||
<Route exact path={CRIB} component={Cash} />
|
<Route exact path={CRIB} component={Cash} />
|
||||||
|
<Route exact path={HOMIES} component={Homies} />
|
||||||
<Route path={HOMIE_FLOW} component={Flow} />
|
<Route path={HOMIE_FLOW} component={Flow} />
|
||||||
<Route path={CHIPS} component={Chips} />
|
<Route path={CHIPS} component={Chips} />
|
||||||
<Route path={MAKE_MONEY_MOVE} component={MakeMoneyMove} />
|
<Route path={MAKE_MONEY_MOVE} component={MakeMoneyMove} />
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
export const CRIB = '/';
|
export const CRIB = '/';
|
||||||
|
export const HOMIES = '/homies';
|
||||||
export const CHIPS = '/chips';
|
export const CHIPS = '/chips';
|
||||||
export const MAKE_MONEY_MOVE = '/make-money-move';
|
export const MAKE_MONEY_MOVE = '/make-money-move';
|
||||||
export const HOMIE_FLOW = '/homie/:homie_id/flow';
|
export const HOMIE_FLOW = '/homie/:homie_id/flow';
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import axios from "axios";
|
|||||||
import { Table, Collapsible, CollapsibleItem, Button, TextInput, Row, Col, Select } from "react-materialize";
|
import { Table, Collapsible, CollapsibleItem, Button, TextInput, Row, Col, Select } from "react-materialize";
|
||||||
import M from "materialize-css";
|
import M from "materialize-css";
|
||||||
import YesNoModal from "../common/YesNoModal";
|
import YesNoModal from "../common/YesNoModal";
|
||||||
|
import { errorToast } from "../common/errorHelpers";
|
||||||
|
|
||||||
const ListChips = (props) => {
|
const ListChips = (props) => {
|
||||||
const [chipsList, setChipsList] = useState([]);
|
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 getChipData = (chipId) => chipsList.find(chip => chip.id === chipId);
|
||||||
|
|
||||||
const checkIfPairExists = (baseChipId, secondaryChipId) => {
|
const checkIfPairExists = (baseChipId, secondaryChipId) => {
|
||||||
|
|||||||
7
client/src/common/errorHelpers.js
Normal file
7
client/src/common/errorHelpers.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
import M from "materialize-css";
|
||||||
|
|
||||||
|
const errorToast = () => M.toast({ html: "Yo! It ain't workin'" });
|
||||||
|
|
||||||
|
export {
|
||||||
|
errorToast
|
||||||
|
}
|
||||||
74
client/src/homies/Homies.js
Normal file
74
client/src/homies/Homies.js
Normal file
@@ -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 (
|
||||||
|
<li key={index}>
|
||||||
|
<div className="collapsible-header">
|
||||||
|
<div className="flex-row opposite-sides-content">
|
||||||
|
<div className="flex-col">
|
||||||
|
<div>{ homie.name }</div>
|
||||||
|
<div className="grey-text">{ homie.about }</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<YesNoModal
|
||||||
|
body={"Maan, y'a sure about this?"}
|
||||||
|
yesAction={() => deleteHomie(homie.id)}
|
||||||
|
triggerNode={<Button flat node="button" icon="delete" />} />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="container">
|
||||||
|
<NewHomieForm newHomiesSetter={setHomies} />
|
||||||
|
|
||||||
|
<ul className="collapsible">
|
||||||
|
{ homiesData }
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default withRouter(Homies);
|
||||||
122
client/src/homies/NewHomieForm.js
Normal file
122
client/src/homies/NewHomieForm.js
Normal file
@@ -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) => <option key={index} value={chip.id}>{ chip.name }</option>);
|
||||||
|
|
||||||
|
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 (
|
||||||
|
<div>
|
||||||
|
<Collapsible id="new-homie-form-container">
|
||||||
|
<CollapsibleItem header={<strong>Introduce new homie to the Hood</strong>}>
|
||||||
|
<div className="input-field col s12">
|
||||||
|
<input id="homie-name" type="text" className="validate" required="required" value={homieName} onChange={(e) => setHomieName(e.target.value)} />
|
||||||
|
<label className="required" htmlFor="homie-name">Homie name</label>
|
||||||
|
<span className="helper-text" data-error="Yo! Introduce us the new homie" />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<TextInput
|
||||||
|
id="aboutHomie"
|
||||||
|
label="About"
|
||||||
|
value={aboutHomie}
|
||||||
|
onChange={(e) => setAboutHomie(e.target.value)}
|
||||||
|
/>
|
||||||
|
|
||||||
|
<label className="required">Homie importance: </label>
|
||||||
|
<Select value={homieImportance} onChange={(e) => setHomieImportance(e.target.value)} name="homieImportance">
|
||||||
|
<option disabled value="">Set homie importance</option>
|
||||||
|
<option value={100}>Homie for life</option>
|
||||||
|
<option value={75}>Real homie</option>
|
||||||
|
<option value={50}>Alright</option>
|
||||||
|
<option value={25}>Foo homie</option>
|
||||||
|
<option value={0}>Guy from the hood</option>
|
||||||
|
</Select>
|
||||||
|
|
||||||
|
<label className="required">Homie default chip: </label>
|
||||||
|
<Select value={homieDefaultChip} onChange={(e) => setHomieDefaultChip(e.target.value)} name="defaultChip">
|
||||||
|
<option disabled value="">Set default chip</option>
|
||||||
|
{ chipOptions }
|
||||||
|
</Select>
|
||||||
|
|
||||||
|
<br/>
|
||||||
|
|
||||||
|
<Button disabled={disableAddButton()} onClick={addNewHomie}>Add to the hood</Button>
|
||||||
|
|
||||||
|
</CollapsibleItem>
|
||||||
|
</Collapsible>
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default NewHomieForm;
|
||||||
5
db/migrate/20200911162613_add_default_chip_to_homies.rb
Normal file
5
db/migrate/20200911162613_add_default_chip_to_homies.rb
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
class AddDefaultChipToHomies < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
add_reference :homies, :chip
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
class ChangeDefaultValueForImportanceInHomies < ActiveRecord::Migration[5.2]
|
||||||
|
def change
|
||||||
|
change_column_default :homies, :importance, 100
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -10,7 +10,7 @@
|
|||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# 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
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
@@ -37,10 +37,12 @@ ActiveRecord::Schema.define(version: 2020_08_27_142428) do
|
|||||||
|
|
||||||
create_table "homies", force: :cascade do |t|
|
create_table "homies", force: :cascade do |t|
|
||||||
t.text "name", null: false
|
t.text "name", null: false
|
||||||
t.integer "importance", default: 5, null: false
|
t.integer "importance", default: 100, null: false
|
||||||
t.text "about"
|
t.text "about"
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
|
t.bigint "chip_id"
|
||||||
|
t.index ["chip_id"], name: "index_homies_on_chip_id"
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "money_moves", force: :cascade do |t|
|
create_table "money_moves", force: :cascade do |t|
|
||||||
|
|||||||
Reference in New Issue
Block a user