@@ -50,6 +50,8 @@ import { useUser } from '@gitroom/frontend/components/layout/user.context';
50
50
import { makeId } from '@gitroom/nestjs-libraries/services/make.is' ;
51
51
import Image from 'next/image' ;
52
52
import { weightedLength } from '@gitroom/helpers/utils/count.length' ;
53
+ import { uniqBy } from 'lodash' ;
54
+ import { Select } from '@gitroom/react/form/select' ;
53
55
54
56
function countCharacters ( text : string , type : string ) : number {
55
57
if ( type !== 'x' ) {
@@ -65,17 +67,36 @@ export const AddEditModal: FC<{
65
67
reopenModal : ( ) => void ;
66
68
mutate : ( ) => void ;
67
69
} > = ( props ) => {
68
- const { date, integrations, reopenModal, mutate } = props ;
69
- const [ dateState , setDateState ] = useState ( date ) ;
70
-
71
- // hook to open a new modal
72
- const modal = useModals ( ) ;
70
+ const { date, integrations : ints , reopenModal, mutate } = props ;
71
+ const [ customer , setCustomer ] = useState ( '' ) ;
73
72
74
73
// selected integrations to allow edit
75
74
const [ selectedIntegrations , setSelectedIntegrations ] = useStateCallback <
76
75
Integrations [ ]
77
76
> ( [ ] ) ;
78
77
78
+ const integrations = useMemo ( ( ) => {
79
+ if ( ! customer ) {
80
+ return ints ;
81
+ }
82
+
83
+ const list = ints . filter ( ( f ) => f ?. customer ?. id === customer ) ;
84
+ if ( list . length === 1 ) {
85
+ setSelectedIntegrations ( [ list [ 0 ] ] ) ;
86
+ }
87
+
88
+ return list ;
89
+ } , [ customer , ints ] ) ;
90
+
91
+ const totalCustomers = useMemo ( ( ) => {
92
+ return uniqBy ( ints , ( i ) => i ?. customer ?. id ) . length ;
93
+ } , [ ints ] ) ;
94
+
95
+ const [ dateState , setDateState ] = useState ( date ) ;
96
+
97
+ // hook to open a new modal
98
+ const modal = useModals ( ) ;
99
+
79
100
// value of each editor
80
101
const [ value , setValue ] = useState <
81
102
Array < {
@@ -286,11 +307,12 @@ export const AddEditModal: FC<{
286
307
}
287
308
288
309
if (
289
- key . value . some (
290
- ( p ) => {
291
- return countCharacters ( p . content , key ?. integration ?. identifier || '' ) > ( key . maximumCharacters || 1000000 ) ;
292
- }
293
- )
310
+ key . value . some ( ( p ) => {
311
+ return (
312
+ countCharacters ( p . content , key ?. integration ?. identifier || '' ) >
313
+ ( key . maximumCharacters || 1000000 )
314
+ ) ;
315
+ } )
294
316
) {
295
317
if (
296
318
! ( await deleteDialog (
@@ -417,6 +439,26 @@ export const AddEditModal: FC<{
417
439
information = { data }
418
440
onChange = { setPostFor }
419
441
/>
442
+ { totalCustomers > 1 && (
443
+ < Select
444
+ hideErrors = { true }
445
+ label = ""
446
+ name = "customer"
447
+ value = { customer }
448
+ onChange = { ( e ) => {
449
+ setCustomer ( e . target . value ) ;
450
+ setSelectedIntegrations ( [ ] ) ;
451
+ } }
452
+ disableForm = { true }
453
+ >
454
+ < option value = "" > Selected Customer</ option >
455
+ { uniqBy ( ints , ( u ) => u ?. customer ?. name ) . map ( ( p ) => (
456
+ < option key = { p . customer ?. id } value = { p . customer ?. id } >
457
+ Customer: { p . customer ?. name }
458
+ </ option >
459
+ ) ) }
460
+ </ Select >
461
+ ) }
420
462
< DatePicker onChange = { setDateState } date = { dateState } />
421
463
</ div >
422
464
</ TopTitle >
0 commit comments