Giter Club home page Giter Club logo

react-3-mini's Introduction

Project Summary

In this project, we'll introduce axios and how to use it inside of a React project. We'll cover full CRUD ( GET, PUT, POST, DELETE ) and also cover how to use .then(). The majority of the React application will already be built for you. If you're finding it hard to dive into an existing code base and understand exactly what is going on, that's perfectly normal. It's most important to focus only on how we're interacting with the API using axios.

To help you understand wether or not you API requests are failed or successful, we've used a UI tool to build in the ability to visualize notifications in the app. The specific style we use is referred to as a "toast" notification. To take advantage of this capability to visualize successful and failed API requests, an additional line of code will need to be added to axios requests, examples of which can be found below.

  • Success: toast.success("Success!");
  • Failure: toast.error("Failed!");

Live Example

Click Me!

Setup

  1. Fork and clone this repository.
  2. cd into the project directory.
  3. Run npm install.
  4. Run npm start.
  5. In a seperate terminal, cd into the project directory.

API Documentation

https://app.swaggerhub.com/apis/DevMountain/Joes-Auto/1.0.0

Please reference this API documentation when completing the project steps.

Step 1

Summary

In this step, we'll make use of axios to get the Get All Vehicles button to work. When fetching data from a server you should always use a GET request.

Instructions

  • Open ./src/App.js.
  • Locate the pre-made getVehicles method.
  • Using axios and the API documentation make a GET request to receive all the vehicles.
    • If the request is successful, use this.setState() to update the value of vehiclesToDisplay and use toast.success.
      • Hint: Inspect the returned data object.
    • If the request is unsuccessful, use toast.error.

Solution

./src/App.js ( getVehicles method )
getVehicles() {
  axios.get('https://joes-autos.herokuapp.com/api/vehicles').then( results => {
    toast.success("Successfully got Vehicles.");
    this.setState({ 'vehiclesToDisplay': results.data });
  }).catch( () => toast.error("Failed at fetching Vehicles") );
}

Step 2

Summary

In this step, we'll make use of axios to get the Increase Price and Decrease Price buttons to work. When modifying/updating data on a server you always use a PUT request.

Instructions

  • Open ./src/App.js.
  • Locate the pre-made updatePrice method.
  • Using axios and the API documentation make a PUT request to either increase or decrease the price.
    • If the request is successful, use this.setState() to update the value of vehiclesToDisplay and use toast.success.
      • Hint: Inspect the returned data object.
    • If the request is unsuccessful, use toast.error.

Solution

./src/App.js ( updatePrice method )
updatePrice( priceChange, id ) {
  axios.put(`https://joes-autos.herokuapp.com/api/vehicles/${ id }/${ priceChange }`).then( results => {
    toast.success("Successfully updated price.");
    this.setState({ 'vehiclesToDisplay': results.data.vehicles });
  }).catch( () => toast.error("Failed at updating price") );
}

Step 3

Summary

In this step, we'll make use of axios to get the Add vehicle button to work. When creating new data on a server you should always use a POST request.

Instructions

  • Open ./src/App.js.
  • Locate the pre-made addCar method.
  • Using axios and the API documentation make a POST request to create a new vehicle.
    • If the request is successful, use this.setState() to update the value of vehiclesToDisplay and use toast.success.
      • Hint: Inspect the returned data object.
    • If the request is unsuccessful, use toast.error.

Solution

./src/App.js ( addCar method )
addCar() {
  let newCar = {
    make: this.make.value,
    model: this.model.value,
    color: this.color.value,
    year: this.year.value,
    price: this.price.value
  };

  axios.post('https://joes-autos.herokuapp.com/api/vehicles', newCar).then( results => {
    toast.success("Successfully added vehicle.");
    this.setState({ vehiclesToDisplay: results.data.vehicles });
  }).catch( () => toast.error('Failed at adding new vehicle.') );
}

Step 4

Summary

In this step, we'll make use of axios to get the SOLD! button to work. When deleting data on a server you should always use a DELETE request.

Instructions

  • Open ./src/App.js.
  • Locate the pre-made sellCar method.
  • Using axios and the API documentation make a DELETE request to delete ( "sell" ) a vehicle.
    • If the request is successful, use this.setState() to update the value of vehiclesToDisplay and use toast.success.
      • Hint: Inspect the returned data object.
    • If the request is unsuccessful, use toast.error.

Solution

./src/App.js ( sellCar method )
sellCar( id ) {
  axios.delete(`https://joes-autos.herokuapp.com/api/vehicles/${ id }`).then( results => {
    toast.success("Successfully sold car.");
    this.setState({ 'vehiclesToDisplay': results.data.vehicles });
  }).catch( () => toast.error("Failed at selling car.") );
}

Black Diamond

If there is extra time during the lecture, try to complete the remaining methods. The remaining methods can also be used as axios and CRUD practice on your own time.

Contributions

If you see a problem or a typo, please fork, make the necessary changes, and create a pull request so we can review your changes and merge them into the master repo and branch.

Copyright

© DevMountain LLC, 2017. Unauthorized use and/or duplication of this material without express and written permission from DevMountain, LLC is strictly prohibited. Excerpts and links may be used, provided that full and clear credit is given to DevMountain with appropriate and specific direction to the original content.

react-3-mini's People

Contributors

alan-miller avatar andrewwestenskow avatar brackcarmony avatar dependabot[bot] avatar devlemire avatar ischoen avatar jakeleonard24 avatar joeblank avatar josephdevmtn avatar mattcbodily avatar matthops avatar sheaclose avatar spcbrn avatar steven-isbell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-3-mini's Issues

Put lacks id

The onClick function on the Increase/Decrease buttons needs to pass v.id. Otherwise the put won't work.

Backend issue

I keep getting an issue from the backend. What is expected from the referer header?

get car error

The backend keeps stopping me. Is this possibly a /etc/hosts entry on my end?

App.js line 308

In the color drown down menu, the color purple is the only option which has a capitalized value. This was making it hard to filter vehicles by color.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.