-
Notifications
You must be signed in to change notification settings - Fork 485
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
address saving functionality #100
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
addressModel: Built the address model to store the addresses for customers, sellers and admins together based on their user ID. | ||
server.js: Changed the server.js file to add the routes for Address creation and fetch | ||
addressRouter: Created this file to perform all the logic of address posting and fetching with the correct routes. (Post on Postman, Fetch on App) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import mongoose from 'mongoose'; | ||
|
||
const addressSchema = new mongoose.Schema( | ||
{ | ||
name: { type: String, required: true }, | ||
phone: { type: Number, required: true, unique: true }, | ||
pincode: { type: Number, required: true}, | ||
addressLine: { type: String, required: true}, | ||
landmark: { type: String, required: true}, | ||
city: { type: String, required: true }, | ||
state: { type: String, required: true }, | ||
addressType: { type: String}, | ||
user: {type : mongoose.Schema.Types.ObjectId, ref: 'User', required: true} | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
); | ||
const Address = mongoose.model('Address', addressSchema); | ||
export default Address; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import express from 'express'; | ||
import expressAsyncHandler from 'express-async-handler'; | ||
import bcrypt from 'bcryptjs'; | ||
import data from '../data.js'; | ||
import Address from '../models/addressModel.js'; | ||
import { generateToken, isAdmin, isAuth } from '../utils.js'; | ||
|
||
const addressRouter = express.Router(); | ||
|
||
addressRouter.get( | ||
'/', | ||
expressAsyncHandler(async (req, res) => { | ||
console.log("address") | ||
const address = await Address.find() | ||
res.send({address}); | ||
}) | ||
); | ||
|
||
addressRouter.post( | ||
'/', | ||
isAuth, | ||
expressAsyncHandler(async (req, res) => { | ||
console.log("Address req ", req) | ||
const address = new Address({ | ||
name: req.body.name, | ||
phone: req.body.phone, | ||
pincode: req.body.pincode, | ||
addressLine: req.body.addressLine, | ||
landmark: req.body.landmark, | ||
city: req.body.city, | ||
state: req.body.state, | ||
addressType: req.body.addressType, | ||
user: req.body.user | ||
}); | ||
const createAddress = await address.save(); | ||
res | ||
.status(201) | ||
.send({ message: 'New Address Created', address: createAddress }); | ||
}) | ||
); | ||
|
||
// userRouter.get( | ||
// '/seed', | ||
// expressAsyncHandler(async (req, res) => { | ||
// // await User.remove({}); | ||
// const createdUsers = await User.insertMany(data.users); | ||
// res.send({ createdUsers }); | ||
// }) | ||
// ); | ||
|
||
// userRouter.post( | ||
// '/signin', | ||
// expressAsyncHandler(async (req, res) => { | ||
// const user = await User.findOne({ email: req.body.email }); | ||
// if (user) { | ||
// if (bcrypt.compareSync(req.body.password, user.password)) { | ||
// res.send({ | ||
// _id: user._id, | ||
// name: user.name, | ||
// email: user.email, | ||
// isAdmin: user.isAdmin, | ||
// isSeller: user.isSeller, | ||
// token: generateToken(user), | ||
// }); | ||
// return; | ||
// } | ||
// } | ||
// res.status(401).send({ message: 'Invalid email or password' }); | ||
// }) | ||
// ); | ||
|
||
// userRouter.post( | ||
// '/register', | ||
// expressAsyncHandler(async (req, res) => { | ||
// const user = new User({ | ||
// name: req.body.name, | ||
// email: req.body.email, | ||
// password: bcrypt.hashSync(req.body.password, 8), | ||
// }); | ||
// const createdUser = await user.save(); | ||
// res.send({ | ||
// _id: createdUser._id, | ||
// name: createdUser.name, | ||
// email: createdUser.email, | ||
// isAdmin: createdUser.isAdmin, | ||
// isSeller: user.isSeller, | ||
// token: generateToken(createdUser), | ||
// }); | ||
// }) | ||
// ); | ||
|
||
// userRouter.get( | ||
// '/:id', | ||
// expressAsyncHandler(async (req, res) => { | ||
// const user = await User.findById(req.params.id); | ||
// if (user) { | ||
// res.send(user); | ||
// } else { | ||
// res.status(404).send({ message: 'User Not Found' }); | ||
// } | ||
// }) | ||
// ); | ||
// userRouter.put( | ||
// '/profile', | ||
// isAuth, | ||
// expressAsyncHandler(async (req, res) => { | ||
// const user = await User.findById(req.user._id); | ||
// if (user) { | ||
// user.name = req.body.name || user.name; | ||
// user.email = req.body.email || user.email; | ||
// if (user.isSeller) { | ||
// user.seller.name = req.body.sellerName || user.seller.name; | ||
// user.seller.logo = req.body.sellerLogo || user.seller.logo; | ||
// user.seller.description = | ||
// req.body.sellerDescription || user.seller.description; | ||
// } | ||
// if (req.body.password) { | ||
// user.password = bcrypt.hashSync(req.body.password, 8); | ||
// } | ||
// const updatedUser = await user.save(); | ||
// res.send({ | ||
// _id: updatedUser._id, | ||
// name: updatedUser.name, | ||
// email: updatedUser.email, | ||
// isAdmin: updatedUser.isAdmin, | ||
// isSeller: user.isSeller, | ||
// token: generateToken(updatedUser), | ||
// }); | ||
// } | ||
// }) | ||
// ); | ||
|
||
// userRouter.get( | ||
// '/', | ||
// isAuth, | ||
// isAdmin, | ||
// expressAsyncHandler(async (req, res) => { | ||
// const users = await User.find({}); | ||
// res.send(users); | ||
// }) | ||
// ); | ||
|
||
// userRouter.delete( | ||
// '/:id', | ||
// isAuth, | ||
// isAdmin, | ||
// expressAsyncHandler(async (req, res) => { | ||
// const user = await User.findById(req.params.id); | ||
// if (user) { | ||
// if (user.email === '[email protected]') { | ||
// res.status(400).send({ message: 'Can Not Delete Admin User' }); | ||
// return; | ||
// } | ||
// const deleteUser = await user.remove(); | ||
// res.send({ message: 'User Deleted', user: deleteUser }); | ||
// } else { | ||
// res.status(404).send({ message: 'User Not Found' }); | ||
// } | ||
// }) | ||
// ); | ||
|
||
// userRouter.put( | ||
// '/:id', | ||
// isAuth, | ||
// isAdmin, | ||
// expressAsyncHandler(async (req, res) => { | ||
// const user = await User.findById(req.params.id); | ||
// if (user) { | ||
// user.name = req.body.name || user.name; | ||
// user.email = req.body.email || user.email; | ||
// user.isSeller = Boolean(req.body.isSeller); | ||
// user.isAdmin = Boolean(req.body.isAdmin); | ||
// // user.isAdmin = req.body.isAdmin || user.isAdmin; | ||
// const updatedUser = await user.save(); | ||
// res.send({ message: 'User Updated', user: updatedUser }); | ||
// } else { | ||
// res.status(404).send({ message: 'User Not Found' }); | ||
// } | ||
// }) | ||
// ); | ||
|
||
export default addressRouter; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -85,6 +85,7 @@ orderRouter.post( | |
'/', | ||
isAuth, | ||
expressAsyncHandler(async (req, res) => { | ||
console.log("Order req ", req) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove logs. |
||
if (req.body.orderItems.length === 0) { | ||
res.status(400).send({ message: 'Cart is empty' }); | ||
} else { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
addressReducers: Created addressReducers file (support file) | ||
addressConstants: Created addressConstants file (support file) | ||
AddressActions: Created AddressActions file (support file) | ||
AddressScreen: Created AddressScreen file to let the users add and see their saved addresses. | ||
App.js: Redesigned the navbar to match that of Amazon. Introduced the address feature. | ||
store.js: (support file) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import Axios from 'axios'; | ||
import { | ||
ADDRESS_CREATE_REQUEST, | ||
ADDRESS_CREATE_SUCCESS, | ||
ADDRESS_CREATE_FAIL, | ||
ADDRESS_LIST_REQUEST, | ||
ADDRESS_LIST_SUCCESS, | ||
ADDRESS_LIST_FAIL | ||
} from '../constants/addressConstants'; | ||
|
||
export const listAddresses = () => async (dispatch) => { | ||
dispatch({ type: ADDRESS_LIST_REQUEST }); | ||
try { | ||
const { data } = await Axios.get('/api/address', { | ||
}); | ||
console.log(data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove logs. |
||
dispatch({ type: ADDRESS_LIST_SUCCESS, payload: data }); | ||
} catch (error) { | ||
dispatch({ type: ADDRESS_LIST_FAIL, payload: error.message }); | ||
} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,7 @@ export const signin = (email, password) => async (dispatch) => { | |
dispatch({ type: USER_SIGNIN_REQUEST, payload: { email, password } }); | ||
try { | ||
const { data } = await Axios.post('/api/users/signin', { email, password }); | ||
console.log("TOKEN ", data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can remove logs. |
||
dispatch({ type: USER_SIGNIN_SUCCESS, payload: data }); | ||
localStorage.setItem('userInfo', JSON.stringify(data)); | ||
} catch (error) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const ADDRESS_CREATE_REQUEST = 'ADDRESS_CREATE_REQUEST'; | ||
export const ADDRESS_CREATE_SUCCESS = 'ADDRESS_CREATE_SUCCESS'; | ||
export const ADDRESS_CREATE_FAIL = 'ADDRESS_CREATE_FAIL'; | ||
|
||
export const ADDRESS_LIST_REQUEST = 'ADDRESS_LIST_REQUEST'; | ||
export const ADDRESS_LIST_SUCCESS = 'ADDRESS_LIST_SUCCESS'; | ||
export const ADDRESS_LIST_FAIL = 'ADDRESS_LIST_FAIL'; | ||
export const ADDRESS_DETAILS_RESET = 'ADDRESS_DETAILS_RESET'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better to remove commented code (dead code).