Skip to content

Commit e7a3b6d

Browse files
committed
setup reducer and config
1 parent d682fbb commit e7a3b6d

17 files changed

+307
-29
lines changed

backend/Backend/settings.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@
6767
AUTH_PASSWORD_VALIDATORS = [
6868
{
6969
'NAME':
70-
'django.contrib.auth.password_validation.' +
71-
'UserAttributeSimilarityValidator',
70+
'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
7271
},
7372
{
7473
'NAME':

backend/Backend/urls.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
path('api/', include('core.urls'))
99
]
1010

11-
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT),
11+
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

backend/Pipfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ pillow = "*"
1313
django-linter = "*"
1414
pylint = "*"
1515
isort = "*"
16+
ipython = "*"
17+
ipdb = "*"
18+
autopep8 = "*"
1619

1720
[requires]
1821
python_version = "3.8"

backend/Pipfile.lock

Lines changed: 123 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/core/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ class OrderItem(models.Model):
8282
image = models.CharField(max_length=200, null=True, blank=True)
8383
_id = models.AutoField(primary_key=True, editable=False)
8484

85+
class Meta:
86+
verbose_name = "OrderItem"
87+
verbose_name_plural = "OrderItems"
88+
8589
def __str__(self):
8690
return str(self.name)
8791

@@ -98,5 +102,9 @@ class ShippingAddress(models.Model):
98102
max_digits=7, decimal_places=2, null=True, blank=True)
99103
_id = models.AutoField(primary_key=True, editable=False)
100104

105+
class Meta:
106+
verbose_name = "ShippingAddress"
107+
verbose_name_plural = "ShippingAddresses"
108+
101109
def __str__(self):
102110
return str(self.address)

backend/core/serializer.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from django.contrib.auth.models import User
2+
from rest_framework.serializers import ModelSerializer
3+
4+
from core.models import Order, OrderItem, Product, Review, ShippingAddress
5+
6+
7+
class ProductSerializer(ModelSerializer):
8+
class Meta:
9+
model = Product
10+
fields = '__all__'

backend/core/views.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
from rest_framework.decorators import api_view
22
from rest_framework.response import Response
33

4-
from .product import PRODUCT
4+
from core.models import Order, OrderItem, Product, Review, ShippingAddress
5+
from core.serializer import ProductSerializer
56

67

78
@api_view(['GET'])
89
def getProducts(request):
9-
products = PRODUCT
10-
return Response(products)
10+
products = Product.objects.all()
11+
serializer = ProductSerializer(products, many=True)
12+
return Response(serializer.data)
1113

1214

1315
@api_view(['GET'])
1416
def getProduct(request, id):
15-
product = PRODUCT[id-1]
16-
return Response(product)
17+
product = Product.objects.get(pk=id)
18+
serializer = ProductSerializer(product)
19+
return Response(serializer.data)

backend/static/images/airpods.jpg

20.4 KB
Loading

frontend/package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"name": "frontend",
3+
"proxy": "http://127.0.0.1:8000",
34
"version": "0.1.0",
45
"private": true,
56
"dependencies": {
@@ -15,10 +16,14 @@
1516
"react": "^17.0.2",
1617
"react-bootstrap": "^1.5.2",
1718
"react-dom": "^17.0.2",
19+
"react-redux": "^7.2.3",
1820
"react-router": "^5.2.0",
1921
"react-router-bootstrap": "^0.25.0",
2022
"react-router-dom": "^5.2.0",
2123
"react-scripts": "4.0.3",
24+
"redux": "^4.0.5",
25+
"redux-devtools-extension": "^2.13.9",
26+
"redux-thunk": "^2.3.0",
2227
"web-vitals": "^1.0.1"
2328
},
2429
"scripts": {

frontend/src/Pages/HomePage.jsx

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,31 @@
1-
import React, { useState, useEffect } from "react";
1+
import React, { useEffect } from "react";
2+
import { useDispatch, useSelector } from "react-redux";
23
import { Row, Col } from "react-bootstrap";
34
import Product from "../components/Product";
4-
import axios from "axios";
5+
import { listProducts } from "../actions/productActions";
56

67
function HomePage() {
7-
const [products, setProducts] = useState([]);
8+
const dispatch = useDispatch();
9+
const { loading, error, products } = useSelector(
10+
(state) => state.productList
11+
);
812
useEffect(() => {
9-
async function fetchProducts() {
10-
const { data } = await axios.get("http://localhost:8000/api/products/");
11-
setProducts(data);
12-
}
13-
fetchProducts();
14-
}, []);
13+
dispatch(listProducts());
14+
}, [dispatch]);
1515
return (
1616
<div>
1717
<h1>Latest Product</h1>
18-
<Row>
19-
{products.map((product) => (
20-
<Col sm={12} md={6} lg={4} xl={3} key={product._id}>
21-
<Product product={product} />
22-
</Col>
23-
))}
24-
</Row>
18+
{loading ? (
19+
<h1>Loading...</h1>
20+
) : (
21+
<Row>
22+
{products.map((product) => (
23+
<Col sm={12} md={6} lg={4} xl={3} key={product._id}>
24+
<Product product={product} />
25+
</Col>
26+
))}
27+
</Row>
28+
)}
2529
</div>
2630
);
2731
}

frontend/src/Pages/ProductPage.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function ProductPage(props) {
1818
useEffect(() => {
1919
async function fetchProduct() {
2020
const { data } = await axios.get(
21-
`http://localhost:8000/api/product/${props.match.params.id}`
21+
`/api/product/${props.match.params.id}`
2222
);
2323
setProduct(data);
2424
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import {
2+
PRODUCT_LIST_FAIL,
3+
PRODUCT_LIST_REQUEST,
4+
PRODUCT_LIST_SUCCESS,
5+
} from "../constants/productConstants";
6+
7+
import axios from "axios";
8+
9+
export const listProducts = () => async (dispatch) => {
10+
try {
11+
dispatch({ type: PRODUCT_LIST_REQUEST });
12+
const { data } = await axios.get("/api/products/");
13+
dispatch({ type: PRODUCT_LIST_SUCCESS, payload: data });
14+
} catch (err) {
15+
dispatch({
16+
type: PRODUCT_LIST_FAIL,
17+
payload:
18+
err.response && err.response.data.message
19+
? err.response.data.message
20+
: err.message,
21+
});
22+
}
23+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const PRODUCT_LIST_REQUEST = "PRODUCT_LIST_REQUEST";
2+
export const PRODUCT_LIST_SUCCESS = "PRODUCT_LIST_SUCCESS";
3+
export const PRODUCT_LIST_FAIL = "PRODUCT_LIST_FAIL";

frontend/src/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import React from "react";
22
import ReactDOM from "react-dom";
3+
import { Provider } from "react-redux";
4+
import store from "./store.jsx";
35
import "./index.css";
46
import App from "./App.jsx";
57

6-
ReactDOM.render(<App />, document.getElementById("root"));
8+
ReactDOM.render(
9+
<Provider store={store}>
10+
<App />
11+
</Provider>,
12+
document.getElementById("root")
13+
);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import {
2+
PRODUCT_LIST_FAIL,
3+
PRODUCT_LIST_REQUEST,
4+
PRODUCT_LIST_SUCCESS,
5+
} from "../constants/productConstants";
6+
7+
export const productListReducer = (state = { products: [] }, action) => {
8+
switch (action.type) {
9+
case PRODUCT_LIST_REQUEST:
10+
return { loading: true, products: [] };
11+
case PRODUCT_LIST_SUCCESS:
12+
return { loading: false, products: action.payload };
13+
case PRODUCT_LIST_FAIL:
14+
return { loading: false, error: action.payload };
15+
default:
16+
return state;
17+
}
18+
};

0 commit comments

Comments
 (0)