File size: 3,869 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { Context, type Callback } from '@automattic/calypso-router';
import PageViewTracker from 'calypso/a8c-for-agencies/components/a4a-page-view-tracker';
import { getActiveAgency } from 'calypso/state/a8c-for-agencies/agency/selectors';
import SitesSidebar from '../../components/sidebar-menu/sites';
import AddSitesFromWPCOM from './add-sites/add-sites-from-wpcom';
import {
	A4A_SITES_DASHBOARD_DEFAULT_CATEGORY,
	A4A_SITES_DASHBOARD_DEFAULT_FEATURE,
	DEFAULT_SORT_DIRECTION,
	DEFAULT_SORT_FIELD,
} from './constants';
import NeedSetup from './needs-setup-sites';
import SitesDashboard from './sites-dashboard';
import { SitesDashboardProvider } from './sites-dashboard-provider';
import type { DashboardSortInterface } from 'calypso/jetpack-cloud/sections/agency-dashboard/sites-overview/types';

function configureSitesContext( context: Context ) {
	const category = context.params.category || A4A_SITES_DASHBOARD_DEFAULT_CATEGORY;
	const siteUrl = context.params.siteUrl;
	const siteFeature = context.params.feature || A4A_SITES_DASHBOARD_DEFAULT_FEATURE;

	const {
		s: search,
		page: contextPage,
		issue_types,
		sort_field,
		sort_direction,
		is_favorite,
		is_development,
	} = context.query;

	const sort: DashboardSortInterface = {
		field: sort_field || DEFAULT_SORT_FIELD,
		direction: sort_direction || DEFAULT_SORT_DIRECTION,
	};
	const currentPage = parseInt( contextPage ) || 1;

	context.primary = (
		<SitesDashboardProvider
			categoryInitialState={ category }
			siteUrlInitialState={ siteUrl }
			siteFeatureInitialState={ siteFeature }
			showOnlyFavoritesInitialState={
				is_favorite === '' || is_favorite === '1' || is_favorite === 'true'
			}
			showOnlyDevelopmentInitialState={
				is_development === '' || is_development === '1' || is_development === 'true'
			}
			path={ context.path }
			searchQuery={ search }
			currentPage={ currentPage }
			issueTypes={ issue_types }
			sort={ sort }
			{ ...( context.featurePreview ? { featurePreview: context.featurePreview } : {} ) }
		>
			<PageViewTracker
				title="Sites"
				path={ context.path }
				properties={ {
					category: context.params.category,
					siteUrl: context.params.siteUrl,
					feature: context.params.feature,
				} }
			/>

			<SitesDashboard />
		</SitesDashboardProvider>
	);

	context.secondary = <SitesSidebar path={ context.path } />;
}

export const sitesContext: Callback = ( context: Context, next ) => {
	configureSitesContext( context );
	next();
};

export const dashboardSitesContext: Callback = ( context: Context, next ) => {
	const {
		s: search,
		page: contextPage,
		sort_field,
		sort_direction,
		issue_types,
		is_favorite,
		is_development,
	} = context.query;
	const state = context.store.getState();
	const agency = getActiveAgency( state );

	context.dashboardSitesQuery = {
		isPartnerOAuthTokenLoaded: false,
		searchQuery: search || '',
		currentPage: contextPage || 1,
		sort: {
			field: sort_field || '',
			direction: sort_direction || '',
		},
		perPage: 100,
		agencyId: agency?.id,
		filter: {
			issueTypes: [ issue_types ],
			showOnlyFavorites: !! is_favorite,
			showOnlyDevSites: !! is_development,
		},
	};

	next();
};

export const addSitesContext: Callback = ( context: Context, next ) => {
	context.secondary = <SitesSidebar path={ context.path } />;
	context.primary = (
		<>
			<PageViewTracker title="Add Sites > Add sites from WordPress.com" path={ context.path } />
			<AddSitesFromWPCOM dashboardSitesQuery={ context.dashboardSitesQuery } />
		</>
	);
	next();
};

export const needsSetupContext: Callback = ( context: Context, next ) => {
	context.secondary = <SitesSidebar path={ context.path } />;
	const { license_key } = context.query;
	context.primary = (
		<>
			<PageViewTracker title="Sites > Needs Setup" path={ context.path } />
			<NeedSetup licenseKey={ license_key } />
		</>
	);

	next();
};