@@ -15,21 +15,74 @@ import {
15
15
Menu ,
16
16
Message ,
17
17
Segment ,
18
- Select
18
+ Select ,
19
+ Table
19
20
} from "semantic-ui-react" ;
20
21
import {
21
22
countryListURL ,
22
23
addressListURL ,
23
24
addressCreateURL ,
24
25
addressUpdateURL ,
25
26
addressDeleteURL ,
26
- userIDURL
27
+ userIDURL ,
28
+ paymentListURL
27
29
} from "../constants" ;
28
30
import { authAxios } from "../utils" ;
29
31
30
32
const UPDATE_FORM = "UPDATE_FORM" ;
31
33
const CREATE_FORM = "CREATE_FORM" ;
32
34
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
+
33
86
class AddressForm extends React . Component {
34
87
state = {
35
88
error : null ,
@@ -227,6 +280,16 @@ class Profile extends React.Component {
227
280
} ) ;
228
281
} ;
229
282
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
+
230
293
handleFormatCountries = countries => {
231
294
const keys = Object . keys ( countries ) ;
232
295
return keys . map ( k => {
@@ -293,16 +356,76 @@ class Profile extends React.Component {
293
356
this . setState ( { selectedAddress : null } ) ;
294
357
} ;
295
358
296
- render ( ) {
359
+ renderAddresses = ( ) => {
297
360
const {
298
361
activeItem,
299
- error,
300
- loading,
301
362
addresses,
302
363
countries,
303
364
selectedAddress,
304
365
userID
305
366
} = 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 ;
306
429
const { isAuthenticated } = this . props ;
307
430
if ( ! isAuthenticated ) {
308
431
return < Redirect to = "/login" /> ;
@@ -349,63 +472,12 @@ class Profile extends React.Component {
349
472
</ Menu >
350
473
</ Grid . Column >
351
474
< Grid . Column width = { 10 } >
352
- < Header > { `Update your ${
353
- activeItem === "billingAddress" ? "billing" : "shipping"
354
- } address`} </ Header >
475
+ < Header > { this . handleGetActiveItem ( ) } </ Header >
355
476
< 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 ( )
409
481
) }
410
482
</ Grid . Column >
411
483
</ Grid . Row >
0 commit comments