File size: 2,460 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
import { isUserLoggedIn } from 'calypso/state/current-user/selectors';
import { setSection } from 'calypso/state/ui/actions';
import { setLocale } from 'calypso/state/ui/language/actions';

const noop = () => {};

export function makeLayoutMiddleware( LayoutComponent ) {
	return ( context, next ) => {
		const {
			i18n,
			store,
			queryClient,
			section,
			pathname,
			query,
			primary,
			secondary,
			renderHeaderSection,
			showGdprBanner,
			cachedMarkup,
		} = context;
		// Markup exists in the cache; no need to do extra work which won't be used.
		if ( cachedMarkup ) {
			return next();
		}

		// On server, only render LoggedOutLayout when logged-out.
		if ( ! ( context.isServerSide && isUserLoggedIn( context.store.getState() ) ) ) {
			context.layout = (
				<LayoutComponent
					i18n={ i18n }
					store={ store }
					queryClient={ queryClient }
					currentSection={ section }
					currentRoute={ pathname }
					currentQuery={ query }
					primary={ primary }
					secondary={ secondary }
					renderHeaderSection={ renderHeaderSection }
					redirectUri={ context.originalUrl }
					showGdprBanner={ showGdprBanner }
				/>
			);
		}
		next();
	};
}

export function setSectionMiddleware( section ) {
	return ( context, next = noop ) => {
		// save the section in context
		context.section = section;

		// save the section to Redux, too (poised to become legacy)
		context.store.dispatch( setSection( section ) );
		next();
	};
}

export function setLocaleMiddleware( param = 'lang' ) {
	return ( context, next ) => {
		const queryLocale = context.query[ param ];
		if ( queryLocale ) {
			context.lang = queryLocale;
			context.store.dispatch( setLocale( queryLocale ) );
		}

		const paramsLocale = context.params[ param ];
		if ( paramsLocale ) {
			context.lang = paramsLocale;
			context.store.dispatch( setLocale( paramsLocale ) );
		}
		next();
	};
}

/**
 * Composes multiple handlers into one.
 * @param {Object[]} handlers { ...( context, Function ) => void } - A list of route handlers to compose
 * @returns {Function} { ( context, Function ) => void } - A new route handler that executes the handlers in succession
 */
export function composeHandlers( ...handlers ) {
	return ( context, next ) => {
		const it = handlers.values();
		function handleNext() {
			const nextHandler = it.next().value;
			if ( ! nextHandler ) {
				next();
			} else {
				nextHandler( context, handleNext );
			}
		}
		handleNext();
	};
}