From 2a5ffac5b9ca586073460a02e41b1e2eb2d0e977 Mon Sep 17 00:00:00 2001 From: Bilal Date: Sat, 12 Sep 2020 01:17:52 +0300 Subject: [PATCH] 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