-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changes to routes added addtocart functionality
- Loading branch information
Showing
10 changed files
with
164 additions
and
101 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
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,30 @@ | ||
import axios from 'axios'; | ||
|
||
/* ------------ ACTION TYPES ------------------ */ | ||
|
||
const ADD_TO_CART = 'ADD_TO_CART'; | ||
|
||
|
||
/* ------------ ACTION CREATORS ------------------ */ | ||
|
||
const add = orderItem => ({ type: ADD_TO_CART, orderItem }); | ||
|
||
/* ------------ REDUCERS ------------------ */ | ||
|
||
export default function reducer (orderItems = [], action) { | ||
switch (action.type) { | ||
case ADD_TO_CART: | ||
return [action.orderItem, ...orderItems]; | ||
|
||
default: | ||
return orderItems; | ||
} | ||
} | ||
|
||
/* ------------ THUNK CREATORS ------------------ */ | ||
|
||
export const addOrder = (order, userId) => dispatch => { | ||
axios.post(`/${userId}/orders/current`) | ||
.then(res => dispatch(add(res.data))) | ||
.catch(err => console.error(`Adding order to user: ${userId} unsuccessful`, err)); | ||
} |
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 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,32 @@ | ||
const router = require('express').Router(); | ||
const {Order, OrderItems} = require('../db/models'); | ||
|
||
/* | ||
when order is clicked: update cart table. | ||
change status: purchased | ||
put total | ||
*/ | ||
|
||
// EI: | ||
// a more RESTful URL would be: | ||
// PUT /users/:userId/orders/:orderId/purchase | ||
// or | ||
// PUT /orders/:orderId/purchase | ||
// | ||
// A good example of a RESTful API is Paypal's: | ||
// https://developer.paypal.com/docs/api/ | ||
|
||
router.put('/:orderId/purchase', (req, res, next) => { | ||
Order.update({ | ||
status: 'purchased', | ||
total: req.body.total | ||
}, { | ||
where: { | ||
userId: req.params.userId, | ||
returning: true | ||
} | ||
}) | ||
.then( result => res.send(result[1].dataValues)) | ||
}) | ||
|
||
module.exports = router; |
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
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