enable add/remove homie
This commit is contained in:
@@ -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
|
||||
</RoutableNavItem>
|
||||
|
||||
<RoutableNavItem href={CHIPS}>
|
||||
Chips
|
||||
<RoutableNavItem href={HOMIES}>
|
||||
Homies
|
||||
</RoutableNavItem>
|
||||
|
||||
<RoutableNavItem>
|
||||
Homies
|
||||
<RoutableNavItem href={CHIPS}>
|
||||
Chips
|
||||
</RoutableNavItem>
|
||||
|
||||
<RoutableNavItem href={MAKE_MONEY_MOVE}>
|
||||
@@ -40,6 +42,7 @@ function App() {
|
||||
|
||||
<div className="autoscrolling">
|
||||
<Route exact path={CRIB} component={Cash} />
|
||||
<Route exact path={HOMIES} component={Homies} />
|
||||
<Route path={HOMIE_FLOW} component={Flow} />
|
||||
<Route path={CHIPS} component={Chips} />
|
||||
<Route path={MAKE_MONEY_MOVE} component={MakeMoneyMove} />
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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) => {
|
||||
|
||||
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;
|
||||
Reference in New Issue
Block a user