From 32ae56f0fd38ec9151776826aa9f231e29655054 Mon Sep 17 00:00:00 2001 From: K <774162+kaseea@users.noreply.github.com> Date: Tue, 25 Jun 2019 17:51:24 -0700 Subject: [PATCH] restructure axios get higher up to access state, and brings selected customer and movie to the top level, sort of --- src/App.js | 35 ------- src/components/Customer.js | 10 +- src/components/CustomerCollection.js | 39 +------- src/components/Library.js | 51 ++-------- src/components/NavBar.js | 134 ++++++++++++++++----------- 5 files changed, 102 insertions(+), 167 deletions(-) diff --git a/src/App.js b/src/App.js index 91a6b8913..05b72a30d 100644 --- a/src/App.js +++ b/src/App.js @@ -9,16 +9,7 @@ import './App.css'; class App extends Component { - constructor() { - super(); - this.state = { - movies: [], - errorMessage: null, - customers: [], - - }; - } // addCardCallback = (card) => { // console.log(card.text); @@ -51,25 +42,6 @@ class App extends Component { // }); // } - // componentDidMount() { - // // const localUrl = this.props.url + this.props.boardName + "/cards" - // const localUrl = 'http://localhost:3007/customers' - // console.log(localUrl); - // // is this needed and why? - // // const cards = this.state.cards - // axios.get(localUrl) - // .then((response) => { - // // console.log("in axios!"); - // // console.log(response.data) - // this.setState({ - // cards: response.data, - // }) - // }) - // .catch((error) => { - // this.setState({ errorMessage: error.message }); - // }); - // } - render() { @@ -90,13 +62,6 @@ class App extends Component { - {/* - It's possible to use regular expressions to control what param values should be matched. - * "/order/asc" - matched - * "/order/desc" - matched - * "/order/foo" - not matched - */} - ); diff --git a/src/components/Customer.js b/src/components/Customer.js index 0b6d1bbd8..07a47cbaa 100644 --- a/src/components/Customer.js +++ b/src/components/Customer.js @@ -21,8 +21,16 @@ class Customer extends Component { render() { return (
- { console.log(this.props.customer) } + +

{this.props.customer.name}

+

{this.props.customer.id}

+
) } diff --git a/src/components/CustomerCollection.js b/src/components/CustomerCollection.js index 7703d4adb..4657c7933 100644 --- a/src/components/CustomerCollection.js +++ b/src/components/CustomerCollection.js @@ -10,52 +10,23 @@ class CustomerCollection extends Component { constructor(props) { super(props); - this.state = { - customers: [], - }; } -componentDidMount() { - // const localUrl = this.props.url + this.props.boardName + "/cards" - const localUrl = 'http://localhost:3007/customers' - console.log(localUrl); - // is this needed and why? - // const cards = this.state.cards - axios.get(localUrl) - .then((response) => { - console.log("in axios!"); - console.log(response.data) - this.setState({ - - customers: response.data, - }) - }) - .catch((error) => { - this.setState({ errorMessage: error.message }); - }); - } - - - - render() { - const customerComponents = this.state.customers.map((customer, i) => { - console.log(customer) + const customerComponents = this.props.customers.map((customer, i) => { return (
- {/* { console.log(customer)} */} - - +
) }); return (
-

{this.state.message}

- - diff --git a/src/components/Library.js b/src/components/Library.js index b9f8603a7..07360b761 100644 --- a/src/components/Library.js +++ b/src/components/Library.js @@ -4,54 +4,25 @@ import axios from 'axios'; class Library extends Component { - constructor(props) { - super(props); - - this.state = { - movies: [], - }; - } - - -componentDidMount() { - // const localUrl = this.props.url + this.props.boardName + "/cards" - const localUrl = 'http://localhost:3007/movies' - console.log(localUrl); - // is this needed and why? - // const cards = this.state.cards - axios.get(localUrl) - .then((response) => { - console.log("in axios!"); - console.log(response.data) - this.setState({ - - movies: response.data, - }) - }) - .catch((error) => { - this.setState({ errorMessage: error.message }); - }); + super(props); } - - - render() { - const movieComponents = this.state.movies.map((movie, i) => { - console.log(movie) + const movieComponents = this.props.movies.map((movie, i) => { return (

{movie.title}

+
) }); return (
-

{this.state.message}

- - @@ -60,16 +31,6 @@ componentDidMount() { } } -// render() { -// return ( -//
-// { console.log(this.props.customer) } -//

THIS IS A MOVIE

-//
-// ) -// } -// } - Library.propTypes = { }; diff --git a/src/components/NavBar.js b/src/components/NavBar.js index eee06e295..640c1d332 100644 --- a/src/components/NavBar.js +++ b/src/components/NavBar.js @@ -9,70 +9,100 @@ import Search from './Search'; import './NavBar.css'; class NavBar extends Component { + constructor() { + super(); + + this.state = { + movies: [], + errorMessage: null, + customers: [], + selectedCustomer: null, + selectedMovie: null, + }; + } - // componentDidMount() { - // // const localUrl = this.props.url + this.props.boardName + "/cards" - // const localUrl = 'http://localhost:3007/customers' - // console.log(localUrl); - // // is this needed and why? - // // const cards = this.state.cards - // axios.get(localUrl) - // .then((response) => { - // console.log("in axios!"); - // console.log(response.data) - // // this.setState({ - // // cards: response.data, - // // }) - // }) - // .catch((error) => { - // this.setState({ errorMessage: error.message }); - // }); - // } + onSelectCustomer = (customerId) => { + // console.log(this.state.customers) + // console.log(customerId); + const customer = this.state.customers.find(customer => customer.id === customerId) + console.log(customer); + this.setState({ + selectedCustomer: customer + }); + } - + onSelectMovie = (movieId) => { + // console.log(this.state.movies) + // console.log(movieId); + const movie = this.state.movies.find(movie => movie.id === movieId) + console.log(movie); + this.setState({ + selectedMovie: movie + }); + } + + componentDidMount() { + // const localUrl = this.props.url + this.props.boardName + "/cards" + const localUrl = 'http://localhost:3007/movies' + console.log(localUrl); + // is this needed and why? + // const cards = this.state.cards + axios.get(localUrl) + .then((response) => { + console.log("in axios!"); + console.log(response.data) + this.setState({ + + movies: response.data, + }) + }) + .catch((error) => { + this.setState({ errorMessage: error.message }); + }); + const localUrl2 = 'http://localhost:3007/customers' + // is this needed and why? + // const cards = this.state.cards + axios.get(localUrl2) + .then((response) => { + console.log("in axios!"); + console.log(response.data) + let updatedCustomers = response.data; + this.setState({ + customers: updatedCustomers, + }) + }) + .catch((error) => { + this.setState({ errorMessage: error.message }); + }); + + } - render() { - function Child({ match }) { - return ( -
-

ID: {match.params.id}

-

awesome, we're getting { match.params.id } by typing in match.params.id

-
- ); - } - function ComponentWithRegex({ match }) { - return ( -
-

Only asc/desc are allowed: {match.params.direction}

-
- ); - } - function onShowCustomers({ match }) { - return ( -
- {console.log({ match })} - {console.log("POOOP")} -

please pleeeeease

-
- ); - } + render() { return (
-
  • {this.props.allMovies}
  • +
  • {this.props.allMovies}
  • {this.props.allCustomers}
  • {this.props.search}
  • - - - + ( + + )}/> + + ( + + )}/> - + +
    )