Skip to content

Commit 8060627

Browse files
Matthew FreireMatthew Freire
authored andcommitted
Payment history view
1 parent 0980a43 commit 8060627

File tree

6 files changed

+164
-64
lines changed

6 files changed

+164
-64
lines changed

core/api/serializers.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from django_countries.serializer_fields import CountryField
22
from rest_framework import serializers
3-
from core.models import Address, Item, Order, OrderItem, Coupon, Variation, ItemVariation
3+
from core.models import (
4+
Address, Item, Order, OrderItem, Coupon, Variation, ItemVariation,
5+
Payment
6+
)
47

58

69
class StringSerializer(serializers.StringRelatedField):
@@ -195,3 +198,13 @@ class Meta:
195198
'address_type',
196199
'default'
197200
)
201+
202+
203+
class PaymentSerializer(serializers.ModelSerializer):
204+
class Meta:
205+
model = Payment
206+
fields = (
207+
'id',
208+
'amount',
209+
'timestamp'
210+
)

core/api/urls.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
AddressCreateView,
1414
AddressUpdateView,
1515
AddressDeleteView,
16-
OrderItemDeleteView
16+
OrderItemDeleteView,
17+
PaymentListView
1718
)
1819

1920
urlpatterns = [
@@ -35,4 +36,6 @@
3536
OrderItemDeleteView.as_view(), name='order-item-delete'),
3637
path('order-item/update-quantity/',
3738
OrderQuantityUpdateView.as_view(), name='order-item-update-quantity'),
39+
path('payments/', PaymentListView.as_view(), name='payment-list'),
40+
3841
]

core/api/views.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@
1414
from rest_framework.response import Response
1515
from rest_framework.status import HTTP_200_OK, HTTP_400_BAD_REQUEST
1616
from core.models import Item, OrderItem, Order
17-
from .serializers import ItemSerializer, OrderSerializer, ItemDetailSerializer, AddressSerializer
17+
from .serializers import (
18+
ItemSerializer, OrderSerializer, ItemDetailSerializer, AddressSerializer,
19+
PaymentSerializer
20+
)
1821
from core.models import Item, OrderItem, Order, Address, Payment, Coupon, Refund, UserProfile, Variation, ItemVariation
1922

2023

@@ -287,3 +290,11 @@ class AddressUpdateView(UpdateAPIView):
287290
class AddressDeleteView(DestroyAPIView):
288291
permission_classes = (IsAuthenticated, )
289292
queryset = Address.objects.all()
293+
294+
295+
class PaymentListView(ListAPIView):
296+
permission_classes = (IsAuthenticated, )
297+
serializer_class = PaymentSerializer
298+
299+
def get_queryset(self):
300+
return Payment.objects.filter(user=self.request.user)

db.sqlite3

0 Bytes
Binary file not shown.

src/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ export const addressUpdateURL = id => `${endpoint}/addresses/${id}/update/`;
1919
export const addressDeleteURL = id => `${endpoint}/addresses/${id}/delete/`;
2020
export const orderItemDeleteURL = id => `${endpoint}/order-items/${id}/delete/`;
2121
export const orderItemUpdateQuantityURL = `${endpoint}/order-item/update-quantity/`;
22+
export const paymentListURL = `${endpoint}/payments/`;

src/containers/Profile.js

Lines changed: 133 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,74 @@ import {
1515
Menu,
1616
Message,
1717
Segment,
18-
Select
18+
Select,
19+
Table
1920
} from "semantic-ui-react";
2021
import {
2122
countryListURL,
2223
addressListURL,
2324
addressCreateURL,
2425
addressUpdateURL,
2526
addressDeleteURL,
26-
userIDURL
27+
userIDURL,
28+
paymentListURL
2729
} from "../constants";
2830
import { authAxios } from "../utils";
2931

3032
const UPDATE_FORM = "UPDATE_FORM";
3133
const CREATE_FORM = "CREATE_FORM";
3234

35+
class PaymentHistory extends React.Component {
36+
state = {
37+
payments: []
38+
};
39+
40+
componentDidMount() {
41+
this.handleFetchPayments();
42+
}
43+
44+
handleFetchPayments = () => {
45+
this.setState({ loading: true });
46+
authAxios
47+
.get(paymentListURL)
48+
.then(res => {
49+
this.setState({
50+
loading: false,
51+
payments: res.data
52+
});
53+
})
54+
.catch(err => {
55+
this.setState({ error: err, loading: false });
56+
});
57+
};
58+
59+
render() {
60+
const { payments } = this.state;
61+
return (
62+
<Table celled>
63+
<Table.Header>
64+
<Table.Row>
65+
<Table.HeaderCell>ID</Table.HeaderCell>
66+
<Table.HeaderCell>Amount</Table.HeaderCell>
67+
<Table.HeaderCell>Date</Table.HeaderCell>
68+
</Table.Row>
69+
</Table.Header>
70+
<Table.Body>
71+
{payments.map(p => {
72+
return (
73+
<Table.Row key={p.id}>
74+
<Table.Cell>{p.id}</Table.Cell>
75+
<Table.Cell>${p.amount}</Table.Cell>
76+
<Table.Cell>{new Date(p.timestamp).toUTCString()}</Table.Cell>
77+
</Table.Row>
78+
);
79+
})}
80+
</Table.Body>
81+
</Table>
82+
);
83+
}
84+
}
85+
3386
class AddressForm extends React.Component {
3487
state = {
3588
error: null,
@@ -227,6 +280,16 @@ class Profile extends React.Component {
227280
});
228281
};
229282

283+
handleGetActiveItem = () => {
284+
const { activeItem } = this.state;
285+
if (activeItem === "billingAddress") {
286+
return "Billing Address";
287+
} else if (activeItem === "shippingAddress") {
288+
return "Shipping Address";
289+
}
290+
return "Payment History";
291+
};
292+
230293
handleFormatCountries = countries => {
231294
const keys = Object.keys(countries);
232295
return keys.map(k => {
@@ -293,16 +356,76 @@ class Profile extends React.Component {
293356
this.setState({ selectedAddress: null });
294357
};
295358

296-
render() {
359+
renderAddresses = () => {
297360
const {
298361
activeItem,
299-
error,
300-
loading,
301362
addresses,
302363
countries,
303364
selectedAddress,
304365
userID
305366
} = this.state;
367+
return (
368+
<React.Fragment>
369+
<Card.Group>
370+
{addresses.map(a => {
371+
return (
372+
<Card key={a.id}>
373+
<Card.Content>
374+
{a.default && (
375+
<Label as="a" color="blue" ribbon="right">
376+
Default
377+
</Label>
378+
)}
379+
<Card.Header>
380+
{a.street_address}, {a.apartment_address}
381+
</Card.Header>
382+
<Card.Meta>{a.country}</Card.Meta>
383+
<Card.Description>{a.zip}</Card.Description>
384+
</Card.Content>
385+
<Card.Content extra>
386+
<Button
387+
color="yellow"
388+
onClick={() => this.handleSelectAddress(a)}
389+
>
390+
Update
391+
</Button>
392+
<Button
393+
color="red"
394+
onClick={() => this.handleDeleteAddress(a.id)}
395+
>
396+
Delete
397+
</Button>
398+
</Card.Content>
399+
</Card>
400+
);
401+
})}
402+
</Card.Group>
403+
{addresses.length > 0 ? <Divider /> : null}
404+
{selectedAddress === null ? (
405+
<AddressForm
406+
activeItem={activeItem}
407+
countries={countries}
408+
formType={CREATE_FORM}
409+
userID={userID}
410+
callback={this.handleCallback}
411+
/>
412+
) : null}
413+
{selectedAddress && (
414+
<AddressForm
415+
activeItem={activeItem}
416+
userID={userID}
417+
countries={countries}
418+
address={selectedAddress}
419+
formType={UPDATE_FORM}
420+
callback={this.handleCallback}
421+
/>
422+
)}
423+
</React.Fragment>
424+
);
425+
};
426+
427+
render() {
428+
const { activeItem, error, loading } = this.state;
306429
const { isAuthenticated } = this.props;
307430
if (!isAuthenticated) {
308431
return <Redirect to="/login" />;
@@ -349,63 +472,12 @@ class Profile extends React.Component {
349472
</Menu>
350473
</Grid.Column>
351474
<Grid.Column width={10}>
352-
<Header>{`Update your ${
353-
activeItem === "billingAddress" ? "billing" : "shipping"
354-
} address`}</Header>
475+
<Header>{this.handleGetActiveItem()}</Header>
355476
<Divider />
356-
<Card.Group>
357-
{addresses.map(a => {
358-
return (
359-
<Card key={a.id}>
360-
<Card.Content>
361-
{a.default && (
362-
<Label as="a" color="blue" ribbon="right">
363-
Default
364-
</Label>
365-
)}
366-
<Card.Header>
367-
{a.street_address}, {a.apartment_address}
368-
</Card.Header>
369-
<Card.Meta>{a.country}</Card.Meta>
370-
<Card.Description>{a.zip}</Card.Description>
371-
</Card.Content>
372-
<Card.Content extra>
373-
<Button
374-
color="yellow"
375-
onClick={() => this.handleSelectAddress(a)}
376-
>
377-
Update
378-
</Button>
379-
<Button
380-
color="red"
381-
onClick={() => this.handleDeleteAddress(a.id)}
382-
>
383-
Delete
384-
</Button>
385-
</Card.Content>
386-
</Card>
387-
);
388-
})}
389-
</Card.Group>
390-
{addresses.length > 0 ? <Divider /> : null}
391-
{selectedAddress === null ? (
392-
<AddressForm
393-
activeItem={activeItem}
394-
countries={countries}
395-
formType={CREATE_FORM}
396-
userID={userID}
397-
callback={this.handleCallback}
398-
/>
399-
) : null}
400-
{selectedAddress && (
401-
<AddressForm
402-
activeItem={activeItem}
403-
userID={userID}
404-
countries={countries}
405-
address={selectedAddress}
406-
formType={UPDATE_FORM}
407-
callback={this.handleCallback}
408-
/>
477+
{activeItem === "paymentHistory" ? (
478+
<PaymentHistory />
479+
) : (
480+
this.renderAddresses()
409481
)}
410482
</Grid.Column>
411483
</Grid.Row>

0 commit comments

Comments
 (0)