diff --git a/app/controllers/work_controller.rb b/app/controllers/work_controller.rb new file mode 100644 index 0000000..259a863 --- /dev/null +++ b/app/controllers/work_controller.rb @@ -0,0 +1,31 @@ +class WorkController < ApplicationController + def index + if params[:homie_id] + work = Work.where(homie_id: params[:homie_id].to_i).all.order(created_at: :desc) + else + work = Work.all.order(created_at: :desc) + end + + json_response work + end + + def create + work = Work.new(work_params) + + if work.save + json_response ok: true + else + error_response(:bad_request) + end + end + + private + + def work_params + params.require(:work).permit( + :description, + :amount, + :homie_id + ) + end +end \ No newline at end of file diff --git a/app/models/homie.rb b/app/models/homie.rb index 16aab9c..9d5d5f6 100644 --- a/app/models/homie.rb +++ b/app/models/homie.rb @@ -1,5 +1,6 @@ class Homie < ApplicationRecord has_many :money_moves + has_many :work belongs_to :chip def self.cash(importance) diff --git a/app/models/work.rb b/app/models/work.rb new file mode 100644 index 0000000..7f7337e --- /dev/null +++ b/app/models/work.rb @@ -0,0 +1,3 @@ +class Work < ApplicationRecord + belongs_to :homie +end \ No newline at end of file diff --git a/client/src/App.js b/client/src/App.js index ee9060c..e831832 100644 --- a/client/src/App.js +++ b/client/src/App.js @@ -13,8 +13,9 @@ import { CHIPS, MAKE_MONEY_MOVE, HOMIE_FLOW, - HOMIES + HOMIES, PUT_IN_WORK } from './RouteNames'; +import PutInWork from "./cash/PutInWork"; function App() { @@ -37,6 +38,10 @@ function App() { Make Money Move + + + Put in Work + @@ -46,6 +51,7 @@ function App() { + ); diff --git a/client/src/RouteNames.js b/client/src/RouteNames.js index d956223..ae706c3 100644 --- a/client/src/RouteNames.js +++ b/client/src/RouteNames.js @@ -2,4 +2,5 @@ export const CRIB = '/'; export const HOMIES = '/homies'; export const CHIPS = '/chips'; export const MAKE_MONEY_MOVE = '/make-money-move'; +export const PUT_IN_WORK = '/put-in-work'; export const HOMIE_FLOW = '/homie/:homie_id/flow'; diff --git a/client/src/cash/PutInWork.js b/client/src/cash/PutInWork.js new file mode 100644 index 0000000..62f9c1b --- /dev/null +++ b/client/src/cash/PutInWork.js @@ -0,0 +1,97 @@ +import React, {useState, useEffect} from 'react'; +import M from 'materialize-css'; +import {Icon, Select, Button, Textarea} from 'react-materialize'; +import './Cash.css'; +import axios from 'axios'; +import {errorToast} from "../common/errorHelpers"; + +const PutInWork = (props) => { + const [homies, setHomies] = useState([]); + const [selectedTo, setSelectedTo] = useState(''); + const [amountToAdd, setAmountToAdd] = useState(''); + const [workDescription, setWorkDescription] = useState(''); + const [submitInProgress, setSubmitInProgress] = useState(false); + + useEffect(() => { + (async () => { + try { + const response = await axios.get('/api/homies'); + if (response.status === 200 && response.data){ + setHomies(response.data); + } + } catch (e) { + errorToast(); + } + })(); + }, []); + + const homieOptions = homies.map(homie => ); + + const clearForm = () => { + setAmountToAdd(''); + setSelectedTo(''); + setWorkDescription(''); + } + + const handleSubmit = async (e) => { + e.preventDefault(); + setSubmitInProgress(true); + const workRequest = { + work: { + amount: parseInt(amountToAdd), + homie_id: selectedTo, + description: workDescription + } + } + + const submitResponse = await axios.post('/api/work', workRequest); + + if (submitResponse && submitResponse.status === 200 && submitResponse.data && submitResponse.data.ok === true) { + M.toast({html: "Alright"}); + clearForm(); + } else { + errorToast(); + } + setSubmitInProgress(false); + } + + const formComplete = () => selectedTo !== '' && parseInt(amountToAdd) !== 0 && !isNaN(parseInt(amountToAdd)); + + const disableSubmit = () => (!formComplete() || submitInProgress); + + return ( +
+
+

Put in Work

+ +
+ setAmountToAdd(parseInt(e.target.value))} /> + + +
+ + + + +
+ +