1
- import { Card } from '@automattic/components' ;
1
+ import page from '@automattic/calypso-router' ;
2
+ import { Gridicon , Card } from '@automattic/components' ;
2
3
import { Purchases } from '@automattic/data-stores' ;
4
+ import { DESKTOP_BREAKPOINT } from '@automattic/viewport' ;
5
+ import { useBreakpoint } from '@automattic/viewport-react' ;
3
6
import { DataViews , View , filterSortAndPaginate } from '@wordpress/dataviews' ;
4
- import { LocalizeProps } from 'i18n-calypso' ;
5
- import { useMemo , useState } from 'react' ;
7
+ import { useTranslate } from 'i18n-calypso' ;
8
+ import { useEffect , useMemo , useState } from 'react' ;
6
9
import { MembershipSubscription } from 'calypso/lib/purchases/types' ;
7
10
import {
8
11
usePurchasesFieldDefinitions ,
9
12
useMembershipsFieldDefinitions ,
10
13
} from './hooks/use-field-definitions' ;
11
14
15
+ const purchasesDesktopFields = [ 'site' , 'product' , 'status' , 'payment-method' ] ;
16
+ const purchasesMobileFields = [ 'product' ] ;
12
17
export const purchasesDataView : View = {
13
18
type : 'table' ,
14
19
page : 1 ,
15
20
perPage : 5 ,
16
21
titleField : 'purchase-id' ,
17
22
showTitle : false ,
18
- fields : [ 'site' , 'product' , 'status' , 'payment-method' ] ,
23
+ fields : purchasesDesktopFields ,
19
24
sort : {
20
- field : 'site ' ,
25
+ field : 'product ' ,
21
26
direction : 'desc' ,
22
27
} ,
23
28
layout : { } ,
24
29
} ;
25
30
26
- export function PurchasesDataViews ( props : {
27
- purchases : Purchases . Purchase [ ] ;
28
- translate : LocalizeProps [ 'translate' ] ;
29
- } ) {
30
- const { purchases } = props ;
31
+ export function PurchasesDataViews ( { purchases } : { purchases : Purchases . Purchase [ ] } ) {
32
+ const isDesktop = useBreakpoint ( DESKTOP_BREAKPOINT ) ;
33
+ const translate = useTranslate ( ) ;
31
34
const [ currentView , setView ] = useState ( purchasesDataView ) ;
35
+ // Hide fields at mobile width
36
+ useEffect ( ( ) => {
37
+ if ( isDesktop && currentView . fields === purchasesMobileFields ) {
38
+ setView ( { ...currentView , fields : purchasesDesktopFields } ) ;
39
+ return ;
40
+ }
41
+ if ( ! isDesktop && currentView . fields === purchasesDesktopFields ) {
42
+ setView ( { ...currentView , fields : purchasesMobileFields } ) ;
43
+ return ;
44
+ }
45
+ } , [ isDesktop , currentView , setView ] ) ;
32
46
const purchasesDataFields = usePurchasesFieldDefinitions ( purchasesDataView . fields ) ;
33
47
34
48
const { data : adjustedPurchases , paginationInfo } = useMemo ( ( ) => {
35
49
return filterSortAndPaginate ( purchases , currentView , purchasesDataFields ) ;
36
50
} , [ purchases , currentView , purchasesDataFields ] ) ;
37
51
52
+ const actions = useMemo (
53
+ ( ) => [
54
+ {
55
+ id : 'manage-purchase' ,
56
+ label : translate ( 'Manage this purchase' , { textOnly : true } ) ,
57
+ isPrimary : true ,
58
+ icon : < Gridicon icon = "chevron-right" /> ,
59
+ isEligible : ( item : Purchases . Purchase ) => Boolean ( item . domain && item . id ) ,
60
+ callback : ( items : Purchases . Purchase [ ] ) => {
61
+ const siteUrl = items [ 0 ] . domain ;
62
+ const subscriptionId = items [ 0 ] . id ;
63
+ if ( ! siteUrl ) {
64
+ // eslint-disable-next-line no-console
65
+ console . error ( 'Cannot display manage purchase page for subscription without site' ) ;
66
+ return ;
67
+ }
68
+ if ( ! subscriptionId ) {
69
+ // eslint-disable-next-line no-console
70
+ console . error ( 'Cannot display manage purchase page for subscription without ID' ) ;
71
+ return ;
72
+ }
73
+ page ( `/me/purchases/${ siteUrl } /${ subscriptionId } ` ) ;
74
+ } ,
75
+ } ,
76
+ ] ,
77
+ [ translate ]
78
+ ) ;
79
+
38
80
const getItemId = ( item : Purchases . Purchase ) => {
39
81
return item . id . toString ( ) ;
40
82
} ;
@@ -46,34 +88,69 @@ export function PurchasesDataViews( props: {
46
88
view = { currentView }
47
89
onChangeView = { setView }
48
90
defaultLayouts = { { table : { } } }
49
- actions = { undefined }
91
+ actions = { actions }
50
92
getItemId = { getItemId }
51
93
paginationInfo = { paginationInfo }
52
94
/>
53
95
</ Card >
54
96
) ;
55
97
}
56
98
99
+ const membershipsDesktopFields = [ 'site' , 'product' , 'status' ] ;
100
+ const membershipsMobileFields = [ 'product' ] ;
57
101
export const membershipDataView : View = {
58
102
type : 'table' ,
59
103
page : 1 ,
60
104
perPage : 5 ,
61
- titleField : 'site' ,
62
- fields : [ 'product' , 'status' ] ,
105
+ titleField : 'purchase-id' ,
106
+ showTitle : false ,
107
+ fields : membershipsDesktopFields ,
63
108
sort : {
64
- field : 'site ' ,
109
+ field : 'product ' ,
65
110
direction : 'desc' ,
66
111
} ,
67
112
layout : { } ,
68
113
} ;
69
114
70
- export function MembershipsDataViews ( props : {
71
- memberships : MembershipSubscription [ ] ;
72
- translate : LocalizeProps [ 'translate' ] ;
73
- } ) {
74
- const { memberships } = props ;
115
+ export function MembershipsDataViews ( { memberships } : { memberships : MembershipSubscription [ ] } ) {
75
116
const membershipsDataFields = useMembershipsFieldDefinitions ( ) ;
76
- const [ currentView , setView ] = useState ( purchasesDataView ) ;
117
+ const [ currentView , setView ] = useState ( membershipDataView ) ;
118
+ const isDesktop = useBreakpoint ( DESKTOP_BREAKPOINT ) ;
119
+ const translate = useTranslate ( ) ;
120
+
121
+ // Hide fields at mobile width
122
+ useEffect ( ( ) => {
123
+ if ( isDesktop && currentView . fields === membershipsMobileFields ) {
124
+ setView ( { ...currentView , fields : membershipsDesktopFields } ) ;
125
+ return ;
126
+ }
127
+ if ( ! isDesktop && currentView . fields === membershipsDesktopFields ) {
128
+ setView ( { ...currentView , fields : membershipsMobileFields } ) ;
129
+ return ;
130
+ }
131
+ } , [ isDesktop , currentView , setView ] ) ;
132
+
133
+ const actions = useMemo (
134
+ ( ) => [
135
+ {
136
+ id : 'manage-purchase' ,
137
+ label : translate ( 'Manage this purchase' , { textOnly : true } ) ,
138
+ isPrimary : true ,
139
+ icon : < Gridicon icon = "chevron-right" /> ,
140
+ isEligible : ( item : MembershipSubscription ) => Boolean ( item . ID ) ,
141
+ callback : ( items : MembershipSubscription [ ] ) => {
142
+ const subscriptionId = items [ 0 ] . ID ;
143
+ if ( ! subscriptionId ) {
144
+ // eslint-disable-next-line no-console
145
+ console . error ( 'Cannot display manage purchase page for subscription without ID' ) ;
146
+ return ;
147
+ }
148
+ page ( `/me/purchases/other/${ subscriptionId } ` ) ;
149
+ } ,
150
+ } ,
151
+ ] ,
152
+ [ translate ]
153
+ ) ;
77
154
78
155
const { data : adjustedMemberships , paginationInfo } = useMemo ( ( ) => {
79
156
return filterSortAndPaginate ( memberships , currentView , membershipsDataFields ) ;
@@ -90,7 +167,7 @@ export function MembershipsDataViews( props: {
90
167
view = { currentView }
91
168
onChangeView = { setView }
92
169
defaultLayouts = { { table : { } } }
93
- actions = { undefined }
170
+ actions = { actions }
94
171
getItemId = { getItemId }
95
172
paginationInfo = { paginationInfo }
96
173
/>
0 commit comments