-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
148 additions
and
31 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
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import React, { Component } from 'react'; | ||
import logo from './logo.svg'; | ||
import './App.css'; | ||
import Product from '../product/product'; | ||
|
||
import HttpService from '../services/http-service'; | ||
|
||
const http = new HttpService(); | ||
class App extends Component { | ||
|
||
constructor(props){ | ||
super(props); | ||
|
||
this.state = {products:[]}; | ||
//Bind funictions | ||
this.loadData = this.loadData.bind(this);//the loaddata is bound to here thus referring to this scope rather than refering it to global scope | ||
this.productList = this.productList.bind(this); | ||
|
||
this.loadData(); //this is where the function is passed which is inside the loaddata variable. | ||
|
||
} | ||
//loadData=loaddata.bind(loaddata); bind can also be defined this way but if defined this way, loaddata is not reusable for another new object if we create one | ||
|
||
loadData = () => { | ||
var self = this; | ||
http.getProducts().then(data =>{ | ||
self.setState({products:data}) | ||
// we are inside of a promise here and | ||
// "then" here is screwing up our "this" hence a reference is created | ||
// before the promise is loaded. self is referring to the component | ||
// here. But if self is not used and this is placed along with the | ||
// setState then this referes to the promise and no longer to the | ||
// component because it is asynchronous. | ||
// everytime setState is called, it will render that component again and | ||
// all the components inside of it but not outside of it | ||
},err => { | ||
|
||
}); | ||
} | ||
|
||
productList = () => { | ||
const list = this.state.products.map((product) => | ||
<div className = "col-sm-4" key={product._id}> | ||
<Product title={product.title} price ={product.price} imgURL={product.imgURL}/> | ||
</div> | ||
); | ||
return (list); | ||
} | ||
|
||
render() { | ||
return ( | ||
<div className="App"> | ||
<div className="App-header"> | ||
<h2>welcome suckers, find your best deals</h2> | ||
</div> | ||
<div className="container App-main"> | ||
<div className="row"> | ||
{this.productList()} | ||
</div> | ||
|
||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default App; | ||
|
File renamed without changes.
File renamed without changes
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.Product { | ||
width:20rem; | ||
} | ||
|
||
.Product img { | ||
max-height:15rem; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import React, { Component } from 'react'//we are grabbing one specific thing 'component' from react,hence curly braces | ||
import './product.css'; | ||
|
||
class Product extends Component{ | ||
render(){ | ||
return ( | ||
<div className = "card"> | ||
<img className = "card-img-top" src = {this.props.imgURL}alt="Product"></img> | ||
{/* curly braces is a special syntax to insert javascript */} | ||
<div className="card-block"> | ||
<h4 className="card-title">{this.props.title}</h4> | ||
<p className="card-text">Price: ${this.props.price}</p> | ||
<a href="#" className="btn btn-primary">Add to wishlist</a> | ||
|
||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default Product; |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import 'whatwg-fetch'; | ||
|
||
class HttpService { | ||
getProducts = () =>{ | ||
//1 | ||
var promise = new Promise((resolve,reject) => { | ||
// 2 | ||
fetch('http://localhost:3002/product') | ||
.then(res => { | ||
// 4 | ||
resolve(res.json()); | ||
//josn takes the response and converts to json | ||
}) | ||
}); | ||
// 3 | ||
return promise; | ||
//then is chained to whatever fetch is returning | ||
} | ||
} | ||
|
||
export default HttpService;//ES6 format of module.export |