@@ -3,7 +3,7 @@ import useDebounce from "@dashboard/hooks/useDebounce";
33import { getFullName } from "@dashboard/misc" ;
44import useCustomerSearch from "@dashboard/searches/useCustomerSearch" ;
55import { mapEdgesToItems } from "@dashboard/utils/maps" ;
6- import { DynamicCombobox , type Option } from "@saleor/macaw-ui-next" ;
6+ import { Box , Button , DynamicCombobox , type Option } from "@saleor/macaw-ui-next" ;
77import { useMemo , useRef , useState } from "react" ;
88import { useIntl } from "react-intl" ;
99
@@ -44,21 +44,34 @@ export const GiftCardCustomerSelectField = ({
4444 value : email ,
4545 label : getFullName ( { firstName, lastName } ) || email ,
4646 } ) ) ;
47- const trimmed = inputValue . trim ( ) ;
4847
49- if ( isValidEmailPattern ( trimmed ) ) {
50- const hasExactMatch = opts . some ( opt => opt . value . toLowerCase ( ) === trimmed . toLowerCase ( ) ) ;
48+ return opts ;
49+ } , [ customers ] ) ;
50+
51+ const customEmail = useMemo ( ( ) : string | null => {
52+ const trimmedCustomValue = inputValue . trim ( ) ;
5153
52- if ( ! hasExactMatch ) {
53- opts . unshift ( {
54- label : `${ intl . formatMessage ( messages . useEmail ) } ${ trimmed } ` ,
55- value : trimmed ,
56- } ) ;
57- }
54+ if (
55+ selectedCustomer . email &&
56+ trimmedCustomValue . toLowerCase ( ) === selectedCustomer . email . toLowerCase ( )
57+ ) {
58+ return null ;
5859 }
5960
60- return opts ;
61- } , [ customers , inputValue , intl ] ) ;
61+ const hasExactMatch = options . some (
62+ opt => opt . value . toLowerCase ( ) === trimmedCustomValue . toLowerCase ( ) ,
63+ ) ;
64+
65+ if ( hasExactMatch ) {
66+ return null ;
67+ }
68+
69+ if ( isValidEmailPattern ( trimmedCustomValue ) ) {
70+ return trimmedCustomValue ;
71+ }
72+
73+ return null ;
74+ } , [ options , inputValue , selectedCustomer . email ] ) ;
6275
6376 const value = useMemo < Option | null > (
6477 ( ) =>
@@ -94,30 +107,49 @@ export const GiftCardCustomerSelectField = ({
94107 const label = intl . formatMessage ( messages . customerLabel ) ;
95108
96109 return (
97- < DynamicCombobox
98- data-test-id = "customer-field"
99- disabled = { disabled }
100- label = { label }
101- options = { options }
102- value = { value }
103- onChange = { handleSelect }
104- onInputValueChange = { val => {
105- setInputValue ( val ) ;
106- debouncedSearch ( val ) ;
107- } }
108- onFocus = { ( ) => {
109- if ( ! hasFetchedRef . current ) {
110- search ( "" ) ;
111- hasFetchedRef . current = true ;
112- }
113- } }
114- onScrollEnd = { ( ) => {
115- if ( ! result ?. loading && result ?. data ?. search ?. pageInfo ?. hasNextPage ) {
116- loadMore ( ) ;
117- }
118- } }
119- loading = { result ?. loading }
120- name = "customer"
121- />
110+ < >
111+ < DynamicCombobox
112+ data-test-id = "customer-field"
113+ disabled = { disabled }
114+ label = { label }
115+ options = { options }
116+ value = { value }
117+ onChange = { handleSelect }
118+ onInputValueChange = { val => {
119+ setInputValue ( val ) ;
120+ debouncedSearch ( val ) ;
121+ } }
122+ onFocus = { ( ) => {
123+ if ( ! hasFetchedRef . current ) {
124+ search ( "" ) ;
125+ hasFetchedRef . current = true ;
126+ }
127+ } }
128+ onScrollEnd = { ( ) => {
129+ if ( ! result ?. loading && result ?. data ?. search ?. pageInfo ?. hasNextPage ) {
130+ loadMore ( ) ;
131+ }
132+ } }
133+ loading = { result ?. loading }
134+ name = "customer"
135+ />
136+ { customEmail && (
137+ < Box >
138+ < Button
139+ display = "inline-block"
140+ variant = "tertiary"
141+ onClick = { ( ) => {
142+ setInputValue ( customEmail ) ;
143+ setSelectedCustomer ( {
144+ email : customEmail ,
145+ name : customEmail ,
146+ } ) ;
147+ } }
148+ >
149+ { intl . formatMessage ( messages . useEmail ) } { customEmail }
150+ </ Button >
151+ </ Box >
152+ ) }
153+ </ >
122154 ) ;
123155} ;
0 commit comments