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!");
Fork
andclone
this repository.cd
into the project directory.- Run
npm install
. - Run
npm start
. - In a seperate terminal,
cd
into the project directory.
https://app.swaggerhub.com/apis/DevMountain/Joes-Auto/1.0.0
Please reference this API documentation when completing the project steps.
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.
- 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 ofvehiclesToDisplay
and usetoast.success
.- Hint: Inspect the returned data object.
- If the request is unsuccessful, use
toast.error
.
- If the request is successful, use
./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") );
}
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.
- 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 ofvehiclesToDisplay
and usetoast.success
.- Hint: Inspect the returned data object.
- If the request is unsuccessful, use
toast.error
.
- If the request is successful, use
./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") );
}
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.
- 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 ofvehiclesToDisplay
and usetoast.success
.- Hint: Inspect the returned data object.
- If the request is unsuccessful, use
toast.error
.
- If the request is successful, use
./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.') );
}
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.
- 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 ofvehiclesToDisplay
and usetoast.success
.- Hint: Inspect the returned data object.
- If the request is unsuccessful, use
toast.error
.
- If the request is successful, use
./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.") );
}
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.
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.
© 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.