File size: 2,019 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
import { createHigherOrderComponent } from '@wordpress/compose';
import { useEffect, useState } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { retrieveGooglePhotosPickerSessionCookie } from 'calypso/jetpack-connect/persistence-utils';
import { setPhotoPickerSession } from 'calypso/state/media/actions';
import getGooglePhotosPickerFeatureStatus from 'calypso/state/selectors/get-google-photos-picker-feature-status';
import getGooglePhotosPickerSession from 'calypso/state/selectors/get-google-photos-picker-session';
import {
	useCreateGooglePhotosPickerSessionMutation,
	useDeleteGooglePhotosPickerSessionMutation,
} from './use-google-photos-picker-session-mutation';
import useGooglePhotosPickerSessionQuery from './use-google-photos-picker-session-query';

export const withGooglePhotosPickerSession = createHigherOrderComponent( ( Wrapped ) => {
	return ( props ) => {
		const dispatch = useDispatch();
		const [ cachedSessionId ] = useState( retrieveGooglePhotosPickerSessionCookie() );
		const photosPickerApiEnabled = useSelector( ( state ) =>
			getGooglePhotosPickerFeatureStatus( state )
		);
		const photosPickerSession = useSelector( ( state ) => getGooglePhotosPickerSession( state ) );

		const { data: cachedSession } = useGooglePhotosPickerSessionQuery( cachedSessionId, {
			enabled: !! photosPickerApiEnabled,
		} );
		const { createSession, isPending } = useCreateGooglePhotosPickerSessionMutation();
		const { mutate: deletePickerSession } = useDeleteGooglePhotosPickerSessionMutation();

		useEffect( () => {
			cachedSession && dispatch( setPhotoPickerSession( cachedSession ) );
		}, [ cachedSession, dispatch ] );

		return (
			<Wrapped
				{ ...props }
				photosPickerApiEnabled={ photosPickerApiEnabled }
				photosPickerSession={ photosPickerSession }
				deletePhotosPickerSession={ deletePickerSession }
				createPhotosPickerSession={ createSession }
				isCreatingPhotosPickerSession={ isPending }
			/>
		);
	};
}, 'WithGooglePhotosPickerSession' );