Skip to content

Commit

Permalink
Merge pull request #31 from jineshmodi26/main
Browse files Browse the repository at this point in the history
Create Add Store Page
  • Loading branch information
jineshmodi26 authored Nov 9, 2021
2 parents cd6cf66 + 2a6e36b commit 3795f17
Show file tree
Hide file tree
Showing 6 changed files with 544 additions and 146 deletions.
5 changes: 4 additions & 1 deletion client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import ProductList from "./components/Dashboard/ProductList";
import AddProduct from "./components/Dashboard/AddProduct";
import StoreDetails from "./components/Dashboard/StoreDetails";
import Orders from "./components/Orders/OrderPage"
import AddStore from "./components/Dashboard/AddStore";
import ShopList from "./components/Dashboard/ShopList";

function App() {
console.log("store : ", store);
Expand Down Expand Up @@ -53,7 +55,8 @@ function App() {
<Route exact path="/dashboard" component={DashboardHome} />
<Route path="/dashboard/product/view" component={ProductList} />
<Route path="/dashboard/product/add" component={AddProduct} />
<Route path="/dashboard/store/details" component={StoreDetails} />
<Route path="/dashboard/store/view" component={ShopList} />
<Route path="/dashboard/store/add" component={AddStore}/>
</Switch>
</Layout>
</Switch>
Expand Down
308 changes: 308 additions & 0 deletions client/src/components/Dashboard/AddStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
import React, { useState } from "react";
import {
Container,
makeStyles,
Avatar,
TextField,
Button,
Grid,
Divider,
} from "@material-ui/core";
import PhotoCamera from "@material-ui/icons/PhotoCamera";
import IconButton from "@material-ui/core/IconButton";
import logo from "../../assets/shopping.gif";
import ReactStars from "react-rating-stars-component";
import axios from "axios";
import { useSelector } from "react-redux";
import { ToastContainer, toast } from "react-toastify";
import unAuth from "../../assets/401.png";

const useStyles = makeStyles((theme) => ({
container: {
paddingTop: theme.spacing(10),
padding: theme.spacing(5),
},
ratingDiv: {
display: "flex",
justifyContent: "center",
},
large: {
width: theme.spacing(35),
height: theme.spacing(35),
margin: "auto",
},
imgUpload: {
display: "flex",
alignItems: "center",
// margin: "10px",
justifyContent: "center",
},
uploadButton: {
height: "40px",
width: "40px",
},
loadingDiv: {
width: "100%",
display: "flex",
},
loadingImage: {
margin: "auto",
},
submitButton: {
display: "flex",
justifyContent: "center",
},
emailField: {
display: "flex",
justifyContent: "flex-end",
padding: "20px",
},
nameField: {
display: "flex",
justifyContent: "flex-start",
padding: "20px",
},
field: {
display: "flex",
justifyContent: "center",
padding: "20px",
},
}));

const AddStore = () => {
const classes = useStyles();
const [fileInput, setFileInput] = useState("");
const [previewSource, setPreviewSource] = useState("");
const [loading, setLoading] = useState(true);

const [name, setName] = useState("");
const [description, setDescription] = useState("");
const [rating, setRating] = useState(0);
const [location, setLocation] = useState("");
const [landmark, setLandmark] = useState("");
const [city, setCity] = useState("");
const [state, setState] = useState("");
const [pincode, setPincode] = useState();
const user = useSelector((state) => state.authReducer);
const imageFile = (e) => {
const file = e.target.files[0];
previewFile(file);
};

const previewFile = (file) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = () => {
setPreviewSource(reader.result);
};
};

const createShop = (e) => {
e.preventDefault();
axios
.post(
"http://localhost:4000/api/v1/stores",
{
user: user.user,
name: name,
description: description,
addresses: [
{
location: location,
landmark: landmark,
city: city,
state: state,
pincode: pincode,
},
],
rating: rating,
images: [previewSource],
},
{
headers: {
Authorization: `Bearer ${user.jwtToken}`,
},
}
)
.then((res) => {
if (res.data.error === true) {
toast.error("Store Not Created.");
} else {
toast.success("Store Created Successfully");
}
})
.catch((err) => {
console.log(err);
});
};

return (
<>
<Container className={classes.container}>
{user.user && user.user.admin === true ? (
<>
<ToastContainer />
<form onSubmit={createShop}>
<div>
{loading ? (
<Avatar
alt="Remy Sharp"
src={previewSource} //add || with exixsting store image like profile edit
alt="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png"
variant="rounded"
className={classes.large}
/>
) : (
<Avatar
alt="Remy Sharp"
src={logo}
alt="https://cdn.pixabay.com/photo/2015/10/05/22/37/blank-profile-picture-973460_1280.png"
variant="rounded"
className={classes.large}
/>
)}
<div className={classes.imgUpload}>
<input
accept="image/*"
id="icon-button-file"
type="file"
style={{ display: "none" }}
value={fileInput}
onChange={imageFile}
/>
<label htmlFor="icon-button-file">
<IconButton
color="primary"
variant="contained"
aria-label="upload picture"
component="span"
>
<PhotoCamera className={classes.uploadButton} />
</IconButton>
</label>
</div>
<div className={classes.ratingDiv}>
<ReactStars
size="40"
count={5}
edit={true}
onChange={(value) => {
setRating(value);
}}
/>
</div>
</div>
<Grid container className={classes.form}>
<Grid item lg={6} className={classes.emailField}>
<TextField
label="Store Name"
autoFocus
required
value={name}
onChange={(e) => {
setName(e.target.value);
}}
InputLabelProps={{
shrink: true,
}}
variant="outlined"
/>
</Grid>
<Grid item lg={6} className={classes.nameField}>
<TextField
label="Description"
required
value={description}
onChange={(e) => {
setDescription(e.target.value);
}}
InputLabelProps={{
shrink: true,
}}
multiline
maxRows={4}
variant="outlined"
/>
</Grid>
<Grid lg={12}>
<Divider />
</Grid>
<Grid item lg={6} className={classes.emailField}>
<TextField
value={location}
onChange={(e) => {
setLocation(e.target.value);
}}
required
variant="outlined"
label="location"
/>
</Grid>
<Grid item lg={6} className={classes.nameField}>
<TextField
value={landmark}
onChange={(e) => {
setLandmark(e.target.value);
}}
required
variant="outlined"
label="landmark"
/>
</Grid>
<Grid item lg={6} className={classes.emailField}>
<TextField
value={city}
onChange={(e) => {
setCity(e.target.value);
}}
required
variant="outlined"
label="city"
/>
</Grid>
<Grid item lg={6} className={classes.nameField}>
<TextField
value={state}
variant="outlined"
onChange={(e) => {
setState(e.target.value);
}}
required
label="state"
/>
</Grid>
<Grid item lg={12} className={classes.field}>
<TextField
value={pincode}
onChange={(e) => {
setPincode(parseInt(e.target.value));
}}
required
variant="outlined"
label="pincode"
/>
</Grid>
</Grid>
<div className={classes.submitButton}>
<Button type="submit" variant="contained" color="primary">
Submit
</Button>
</div>
</form>
</>
) : (
<div className={classes.loadingDiv}>
<img
src={unAuth}
alt="loading..."
width="300px"
className={classes.loadingImage}
/>
</div>
)}
</Container>
</>
);
};

export default AddStore;
2 changes: 1 addition & 1 deletion client/src/components/Dashboard/DashboardLeftbar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function DashBoardLeftbar() {
</div>
</Link>

<Link to="/dashboard/store/details" className={classes.a}>
<Link to="/dashboard/store/view" className={classes.a}>
<div className={classes.item}>
<Storefront className={classes.icon}/>
<Typography className={classes.text}>
Expand Down
Loading

0 comments on commit 3795f17

Please sign in to comment.