This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
/
store-notices.tsx
170 lines (158 loc) · 4.45 KB
/
store-notices.tsx
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
159
160
161
162
163
164
165
166
167
168
169
170
/**
* External dependencies
*/
import { __ } from '@wordpress/i18n';
import classnames from 'classnames';
import { useRef, useEffect, RawHTML } from '@wordpress/element';
import { sanitizeHTML } from '@woocommerce/utils';
import { useDispatch } from '@wordpress/data';
import { usePrevious } from '@woocommerce/base-hooks';
import { decodeEntities } from '@wordpress/html-entities';
import type { NoticeType } from '@woocommerce/types';
import type { NoticeBannerProps } from '@woocommerce/base-components/notice-banner';
/**
* Internal dependencies
*/
import StoreNotice from '../store-notice';
const StoreNotices = ( {
className,
notices,
}: {
className: string;
notices: NoticeType[];
} ): JSX.Element => {
const ref = useRef< HTMLDivElement >( null );
const { removeNotice } = useDispatch( 'core/notices' );
const noticeIds = notices.map( ( notice ) => notice.id );
const previousNoticeIds = usePrevious( noticeIds );
useEffect( () => {
// Scroll to container when an error is added here.
const containerRef = ref.current;
if ( ! containerRef ) {
return;
}
// Do not scroll if input has focus.
const activeElement = containerRef.ownerDocument.activeElement;
const inputs = [ 'input', 'select', 'button', 'textarea' ];
if (
activeElement &&
inputs.indexOf( activeElement.tagName.toLowerCase() ) !== -1 &&
activeElement.getAttribute( 'type' ) !== 'radio'
) {
return;
}
const newNoticeIds = noticeIds.filter(
( value ) =>
! previousNoticeIds || ! previousNoticeIds.includes( value )
);
if ( newNoticeIds.length && containerRef?.scrollIntoView ) {
containerRef.scrollIntoView( {
behavior: 'smooth',
} );
}
}, [ noticeIds, previousNoticeIds, ref ] );
// Group notices by whether or not they are dismissible. Dismissible notices can be grouped.
const dismissibleNotices = notices.filter(
( { isDismissible } ) => !! isDismissible
);
const nonDismissibleNotices = notices.filter(
( { isDismissible } ) => ! isDismissible
);
// Group dismissibleNotices by status. They will be combined into a single notice.
const dismissibleNoticeGroups = {
error: dismissibleNotices.filter(
( { status } ) => status === 'error'
),
success: dismissibleNotices.filter(
( { status } ) => status === 'success'
),
warning: dismissibleNotices.filter(
( { status } ) => status === 'warning'
),
info: dismissibleNotices.filter( ( { status } ) => status === 'info' ),
default: dismissibleNotices.filter(
( { status } ) => status === 'default'
),
};
return (
<div
ref={ ref }
className={ classnames( className, 'wc-block-components-notices' ) }
>
{ nonDismissibleNotices.map( ( notice ) => (
<StoreNotice
key={ notice.id + '-' + notice.context }
{ ...notice }
>
<RawHTML>
{ sanitizeHTML( decodeEntities( notice.content ) ) }
</RawHTML>
</StoreNotice>
) ) }
{ Object.entries( dismissibleNoticeGroups ).map(
( [ status, noticeGroup ] ) => {
if ( ! noticeGroup.length ) {
return null;
}
const uniqueNotices = noticeGroup
.filter(
(
notice: NoticeType,
noticeIndex: number,
noticesArray: NoticeType[]
) =>
noticesArray.findIndex(
( _notice: NoticeType ) =>
_notice.content === notice.content
) === noticeIndex
)
.map( ( notice ) => ( {
...notice,
content: sanitizeHTML(
decodeEntities( notice.content )
),
} ) );
const noticeProps: Omit< NoticeBannerProps, 'children' > & {
key: string;
} = {
key: `store-notice-${ status }`,
status: 'error',
onRemove: () => {
noticeGroup.forEach( ( notice ) => {
removeNotice( notice.id, notice.context );
} );
},
};
return uniqueNotices.length === 1 ? (
<StoreNotice { ...noticeProps }>
<RawHTML>{ noticeGroup[ 0 ].content }</RawHTML>
</StoreNotice>
) : (
<StoreNotice
{ ...noticeProps }
summary={
status === 'error'
? __(
'Please fix the following errors before continuing',
'woo-gutenberg-products-block'
)
: ''
}
>
<ul>
{ uniqueNotices.map( ( notice ) => (
<li
key={ notice.id + '-' + notice.context }
>
<RawHTML>{ notice.content }</RawHTML>
</li>
) ) }
</ul>
</StoreNotice>
);
}
) }
</div>
);
};
export default StoreNotices;