1
- import { Button , FormTokenField , Notice } from '@wordpress/components' ;
1
+ import { Button , FormTokenField } from '@wordpress/components' ;
2
2
import { useState } from '@wordpress/element' ;
3
3
import { __ , _n } from '@wordpress/i18n' ;
4
4
import { isEmpty } from 'lodash' ;
5
- import useRestaurantSearch , { possibleEmbed } from './use-restaurant-search' ;
6
5
7
- const MAX_SUGGESTIONS = 20 ;
6
+ export const possibleEmbed = / ^ \s * ( h t t p [ s ] ? : \/ \/ | < s c r i p t ) / ;
8
7
9
8
export default function RestaurantPicker ( props ) {
10
9
const [ input , setInput ] = useState ( '' ) ;
11
- const { restaurants, hasRequestFailed } = useRestaurantSearch ( input , MAX_SUGGESTIONS ) ;
12
10
const [ selectedRestaurants , setSelectedRestaurants ] = useState ( props . rids || [ ] ) ;
13
11
14
12
const idRegex = / ^ ( \d + ) $ | \( # ( \d + ) \) $ / ;
15
13
16
14
const onChange = selected => {
15
+ // we try to parse the restaurant id
17
16
const selectedIds = selected . map ( restaurant => {
18
17
const parsed = idRegex . exec ( restaurant ) ;
19
- const selectedId = parsed [ 1 ] || parsed [ 2 ] ;
20
-
21
- return selectedId ;
18
+ if ( parsed ) {
19
+ const selectedId = parsed [ 1 ] || parsed [ 2 ] ;
20
+ return selectedId ;
21
+ }
22
+ // and default we to user entry
23
+ return restaurant ;
22
24
} ) ;
23
25
setSelectedRestaurants ( selectedIds ) ;
24
26
props . onChange && props . onChange ( selectedIds ) ;
25
27
} ;
26
28
27
- const restaurantNames = restaurants
28
- . filter ( restaurant => selectedRestaurants . indexOf ( restaurant . rid . toString ( ) ) < 0 )
29
- . map ( restaurant => restaurant . name + ` (#${ restaurant . rid } )` ) ;
30
-
31
29
const onSubmit = event => {
32
30
event . preventDefault ( ) ;
33
31
props . onSubmit ( isEmpty ( selectedRestaurants ) ? input : selectedRestaurants ) ;
34
32
} ;
35
33
34
+ // Even though we don't search the OpenTable API
35
+ // we still allow for multiple restaurants, hence
36
+ // still use the token field
36
37
const formInput = (
37
38
< FormTokenField
38
39
value = { selectedRestaurants }
39
- suggestions = { restaurantNames }
40
40
saveTransform = { token => ( possibleEmbed . test ( token ) ? '' : token . trim ( ) ) }
41
41
onInputChange = { setInput }
42
- maxSuggestions = { MAX_SUGGESTIONS }
43
- label = { _n ( 'Restaurant' , 'Restaurants' , selectedRestaurants . length , 'jetpack' ) }
42
+ label = { _n ( 'Restaurant ID' , 'Restaurant IDs' , selectedRestaurants . length , 'jetpack' ) }
44
43
{ ...props }
45
44
onChange = { onChange }
46
45
__nextHasNoMarginBottom = { true }
@@ -59,14 +58,6 @@ export default function RestaurantPicker( props ) {
59
58
) : (
60
59
formInput
61
60
) }
62
- { hasRequestFailed && (
63
- < Notice status = "error" isDismissible = { false } >
64
- { __ (
65
- "OpenTable can't find this restaurant right now. Please try again later." ,
66
- 'jetpack'
67
- ) }
68
- </ Notice >
69
- ) }
70
61
</ div >
71
62
) ;
72
63
}
0 commit comments