-
Notifications
You must be signed in to change notification settings - Fork 293
/
Footer.js
158 lines (141 loc) · 4.49 KB
/
Footer.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* Audience Selection Panel Footer
*
* Site Kit by Google, Copyright 2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* External dependencies
*/
import PropTypes from 'prop-types';
/**
* WordPress dependencies
*/
import { useCallback, useState } from '@wordpress/element';
import { __, _n, sprintf } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { useSelect, useDispatch } from 'googlesitekit-data';
import {
AUDIENCE_SELECTED,
AUDIENCE_SELECTION_FORM,
MAX_SELECTED_AUDIENCES_COUNT,
MIN_SELECTED_AUDIENCES_COUNT,
} from './constants';
import { CORE_FORMS } from '../../../../../../googlesitekit/datastore/forms/constants';
import { CORE_USER } from '../../../../../../googlesitekit/datastore/user/constants';
import { SelectionPanelFooter } from '../../../../../../components/SelectionPanel';
export default function Footer( { isOpen, closePanel, savedItemSlugs } ) {
const selectedItems = useSelect( ( select ) =>
select( CORE_FORMS ).getValue(
AUDIENCE_SELECTION_FORM,
AUDIENCE_SELECTED
)
);
const audienceSettings = useSelect( ( select ) =>
select( CORE_USER ).getAudienceSettings()
);
const saveError = useSelect( ( select ) =>
select( CORE_USER ).getErrorForAction( 'saveAudienceSettings', [
{
...audienceSettings,
configuredAudiences: selectedItems,
},
] )
);
const isSavingSettings = useSelect( ( select ) =>
select( CORE_USER ).isSavingAudienceSettings()
);
const hiddenTileDismissedItems = useSelect( ( select ) => {
const dismissedItems = select( CORE_USER ).getDismissedItems();
return dismissedItems?.filter( ( item ) =>
item.startsWith( 'audience-tile-' )
);
} );
const { saveAudienceSettings, removeDismissedItems } =
useDispatch( CORE_USER );
const selectedItemsCount = selectedItems?.length || 0;
let itemLimitError;
if ( selectedItemsCount < MIN_SELECTED_AUDIENCES_COUNT ) {
itemLimitError = sprintf(
/* translators: 1: Minimum number of groups that can be selected. 2: Number of selected groups. */
_n(
'Select at least %1$d group (%2$d selected)',
'Select at least %1$d groups (%2$d selected)',
MIN_SELECTED_AUDIENCES_COUNT,
'google-site-kit'
),
MIN_SELECTED_AUDIENCES_COUNT,
selectedItemsCount
);
} else if ( selectedItemsCount > MAX_SELECTED_AUDIENCES_COUNT ) {
itemLimitError = sprintf(
/* translators: 1: Maximum number of groups that can be selected. 2: Number of selected groups. */
__( 'Select up to %1$d groups (%2$d selected)', 'google-site-kit' ),
MAX_SELECTED_AUDIENCES_COUNT,
selectedItemsCount
);
}
const [ dismissedItemsError, setDismissedItemsError ] = useState( null );
const saveSettings = useCallback(
async ( configuredAudiences ) => {
setDismissedItemsError( null );
let { error } = await saveAudienceSettings( {
configuredAudiences,
} );
if ( ! error ) {
const unselectedHiddenTileDismissedItems =
hiddenTileDismissedItems?.filter( ( item ) => {
const audienceResourceName = item.replace(
'audience-tile-',
''
);
return ! configuredAudiences.includes(
audienceResourceName
);
} );
if ( unselectedHiddenTileDismissedItems?.length > 0 ) {
( { error } = await removeDismissedItems(
...unselectedHiddenTileDismissedItems
) );
if ( error ) {
setDismissedItemsError( error );
}
}
}
return { error };
},
[ hiddenTileDismissedItems, removeDismissedItems, saveAudienceSettings ]
);
return (
<SelectionPanelFooter
savedItemSlugs={ savedItemSlugs }
selectedItemSlugs={ selectedItems }
saveSettings={ saveSettings }
saveError={ saveError || dismissedItemsError }
itemLimitError={ itemLimitError }
minSelectedItemCount={ MIN_SELECTED_AUDIENCES_COUNT }
maxSelectedItemCount={ MAX_SELECTED_AUDIENCES_COUNT }
isBusy={ isSavingSettings }
isOpen={ isOpen }
closePanel={ closePanel }
/>
);
}
Footer.propTypes = {
isOpen: PropTypes.bool,
closePanel: PropTypes.func.isRequired,
savedItemSlugs: PropTypes.array,
};