This repository has been archived by the owner on May 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored a little using react-hooks
- Loading branch information
1 parent
e4f2eae
commit 0194b09
Showing
7 changed files
with
10,266 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,38 @@ | ||
import React, { Component } from "react"; | ||
import "./Card.css"; | ||
|
||
class Card extends Component { | ||
render() { | ||
const Card = ({onWasteTypeChange,onWasteWeightChange,handleClick,waste}) => { | ||
return ( | ||
<div className="tc pa2 ma1 bw2 shadow-5 trash-dump-card"> | ||
<input | ||
className="pa2 w-20 waste-name" | ||
value={this.props.waste} | ||
value={waste.type} | ||
type="text" | ||
placeholder="Name of the Waste" | ||
onChange={this.props.onWaste} | ||
onChange={onWasteTypeChange} | ||
/> | ||
<input | ||
className="pa2 w-20 weight-in-kg" | ||
value={this.props.weight} | ||
value={waste.weight} | ||
type="number" | ||
placeholder="Weight number in kg" | ||
onChange={this.props.onWeight} | ||
onChange={onWasteWeightChange} | ||
/> | ||
|
||
<button | ||
className={ | ||
this.props.waste && this.props.weight | ||
waste.type && waste.weight | ||
? "submit-button" | ||
: "submit-button-deactivated" | ||
} | ||
// Send onClick event to parent | ||
onClick={this.props.handleClick} | ||
onClick={handleClick} | ||
> | ||
Submit | ||
</button> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
|
||
export default Card; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,61 @@ | ||
import React, { Component } from "react"; | ||
import React, { Component, useState, useEffect } from "react"; | ||
import "./App.css"; | ||
import Card from "../components/Cards/Card"; | ||
import ResultBox from "../components/ResultBox/ResultBox"; | ||
import Trash from "../components/Trash/Trash"; | ||
|
||
class App extends Component { | ||
constructor(props) { | ||
super(props); | ||
const App = () => { | ||
const [waste,setWaste] = useState({ | ||
type:"", | ||
weight: 0, | ||
location: "" | ||
}); | ||
const [isRender,setRender] = useState(false); | ||
const recycling =['plastic', 'pet', 'hdpe', 'ldpe', 'pp', 'metal', 'tin', 'aluminium', 'steel', 'cardboard', 'paper', 'glass'] | ||
|
||
this.state = { | ||
waste: "", | ||
weight: "", | ||
location: "", | ||
renderResults: false, | ||
recycling: ['plastic', 'pet', 'hdpe', 'ldpe', 'pp', 'metal', 'tin', 'aluminium', 'steel', 'cardboard', 'paper', 'glass'], | ||
}; | ||
} | ||
|
||
onWasteChange = event => { | ||
let waste = event.target.value.toLowerCase(); | ||
this.setState({ waste: waste, renderResults: false }); | ||
const onWasteTypeChange = event => { | ||
let wasteType = event.target.value.toLowerCase(); | ||
setWaste({ | ||
...waste, | ||
type:wasteType | ||
}); | ||
|
||
}; | ||
|
||
onWeightChange = event => { | ||
this.setState({ weight: event.target.value, renderResults: false }); | ||
const onWasteWeightChange = event => { | ||
setWaste({ | ||
...waste, | ||
weight:event.target.value | ||
}); | ||
|
||
setRender(false); | ||
}; | ||
|
||
handleClick = () => { | ||
if (this.state.waste && this.state.weight) { | ||
this.setState({ renderResults: true }); | ||
const handleClick = () => { | ||
if (waste.type && waste.weight) { | ||
setRender(!isRender) | ||
} | ||
}; | ||
|
||
render() { | ||
|
||
return ( | ||
<div className="App"> | ||
<Trash /> | ||
<Card | ||
onWaste={this.onWasteChange} | ||
onWeight={this.onWeightChange} | ||
waste={this.state.waste} | ||
weight={this.state.weight} | ||
onWasteTypeChange={onWasteTypeChange} | ||
onWasteWeightChange={onWasteWeightChange} | ||
waste={waste} | ||
// Bring submit button onclick event to parent | ||
handleClick={this.handleClick.bind(this)} | ||
handleClick={handleClick} | ||
/> | ||
{/* CONDITIONALLY RENDER RESULTS */} | ||
{this.state.renderResults ? ( | ||
<ResultBox waste={this.state.waste} weight={this.state.weight} recycling={this.state.recycling} /> | ||
{isRender ? ( | ||
<ResultBox type={waste.type} weight={waste.weight} recycling={recycling} /> | ||
) : null} | ||
</div> | ||
); | ||
} | ||
|
||
} | ||
|
||
export default App; |
Oops, something went wrong.