Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Elise customers #1

Merged
merged 5 commits into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"private": true,
"dependencies": {
"axios": "^0.19.0",
"bootstrap": "^4.3.1",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"react-router-dom": "^4.4.0-beta.8",
Expand Down
35 changes: 22 additions & 13 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,29 @@ import './App.css';

import Search from './components/Search';
import MovieLibrary from './components/MovieLibrary';
import Customer from './components/Customer';
import Checkout from './components/Checkout';
import CustomerList from './components/CustomerList';
import Customer from './components/Customer';

class App extends Component {
constructor(props) {
super(props);
this.state = {
selectedCustomer: '',
}
}

selectCustomer = (customerName) => {
console.log(customerName);
this.setState({
selectedCustomer: customerName,
});
}

render() {
return (
<div className="App">
<header className="App-header">

<Router>
<nav>
<ul>
Expand All @@ -25,26 +40,20 @@ class App extends Component {
<Link to='/movielibrary'>Movie Library</Link>
</li>
<li>
<Link to='/customers'>Customer List</Link>
</li>
<li>
<Link to='/checkout'>Checkout</Link>
<Link to='/customerlist'>Customer List</Link>
</li>
</ul>
</nav>

<Route path="/search" component={Search} />
<Route path="/movielibrary" component={MovieLibrary} />
<Route path="/customers" component={Customer} />
<Route path="/checkout" component={Checkout} />
<Route path="/customerlist" render={(props) => <CustomerList {...props} selectedCustomer={this.selectCustomer} />} />

</Router>
</header>





<section>
<Checkout selectedCustomer={this.state.selectedCustomer}/>
</section>
</div>
);
}
Expand Down
16 changes: 16 additions & 0 deletions src/components/Checkout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React from 'react';

const Checkout = (props) => {
return(
<div>
<div>
Selected Customer: {props.selectedCustomer}
</div>
<div>
Movie
</div>
</div>
);
}

export default Checkout;
27 changes: 27 additions & 0 deletions src/components/Customer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React, {Component} from 'react';

class Customer extends Component {
constructor (props) {
super(props);
}

onChangeHandler = (event) => {
this.props.selectedCallback(event.target.value);
}

render () {
return(
<label>
<input
type="radio"
value={this.props.customerId}
onChange={this.onChangeHandler}
checked={this.props.isSelected === this.props.name}
/>
{this.props.name}
</label>
);
}
}

export default Customer;
72 changes: 68 additions & 4 deletions src/components/CustomerList.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,82 @@
import React, { Component } from 'react';
import axios from 'axios';
import Customer from './Customer';

const URL_CUSTOMERS = 'http://localhost:4000/customers';

class CustomerList extends Component {
constructor() {
super();
this.state = {
allCustomers: [],
errorMessage: [],
selectedCustomer: '',
}
}

componentDidMount = () => {
const allCustomers = [];
axios.get(URL_CUSTOMERS)
.then((response) => {
response.data.forEach((element) => {
allCustomers.push(element);
})
this.setState({allCustomers, });
})
.catch((error) => {
const errorMessage = this.state.errorMessage;
const newError = error.response.data.errors.text;
newError.forEach((text) => {
errorMessage.push(text);
})
this.setState({errorMessage, });
})
}

updateSelected = (customerId) => {
this.state.allCustomers.forEach((customer) => {
if(customer.id === parseInt(customerId, 10)) {
this.setState({
selectedCustomer: customer.name,
})
}
})
}

onSaveButtonClick = (event) => {
event.preventDefault();
this.props.selectedCustomer(this.state.selectedCustomer);
}

displayCustomers = () => {
const displayedCustomers = this.state.allCustomers.map((customer) => {
return(
<div>
<Customer
key={customer.id}
customerId={customer.id}
name={customer.name}
selectedCallback={this.updateSelected}
isSelected={this.state.selectedCustomer}
/>
</div>
)
})
return (
<form>
{ displayedCustomers }
<button onClick={this.onSaveButtonClick}>Save</button>
</form>
);
}

render() {
return(
<div>
Customer List
</div>
<section>
<h3>All Customers</h3>
{this.displayCustomers()}
</section>
)

}
}

Expand Down
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import 'bootstrap/dist/css/bootstrap.min.css';

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();