1- import React , { useState , useEffect } from "react" ;
2- import { Box , Grid } from "@mui/material" ;
1+ import React , { useState } from "react" ;
2+ import { Grid } from "@mui/material" ;
33import AddIcon from "@mui/icons-material/Add" ;
44import DeleteIcon from "@mui/icons-material/Delete" ;
55import { useTranslation } from "next-i18next" ;
66import AletheiaButton , { ButtonType } from "../../../Button" ;
77import AletheiaInput from "../../../AletheiaInput" ;
8+ import { IInputExtraSourcesList } from "../../../../types/VerificationRequest" ;
9+ import { SourceType } from "../../../../types/Source" ;
810
9- interface IInputExtraSourcesList {
10- value : any ;
11- onChange : ( value : any [ ] ) => void ;
12- disabled : boolean ;
13- placeholder : string ;
14- }
11+ const formatSources = ( sources : SourceType [ ] ) => {
12+ const sourceArray = Array . isArray ( sources ) ? sources : [ ] ;
13+ if ( sourceArray . length === 0 ) return [ createEmptySource ( ) ] ;
1514
16- const InputExtraSourcesList = ( { value, onChange, disabled, placeholder } : IInputExtraSourcesList ) => {
17- const { t } = useTranslation ( ) ;
15+ return sourceArray . map ( ( source ) => ( {
16+ id : source . _id ,
17+ href : typeof source === "object" ? source . href : source || "" ,
18+ isNewSource : ! ! ( typeof source === "object" ? source . href : source ) ,
19+ } ) ) ;
20+ } ;
1821
19- const [ items , setItems ] = useState ( ( ) => {
20- const initialArray = Array . isArray ( value ) ? value : [ ] ;
21- if ( initialArray . length === 0 ) return [ { id : Math . random ( ) , text : "" , isOriginal : false } ] ;
22+ const createEmptySource = ( ) => ( {
23+ id : Math . random ( ) . toString ( ) ,
24+ href : "" ,
25+ isNewSource : false
26+ } ) ;
2227
23- return initialArray . map ( ( item ) => {
24- const isObject = typeof item === "object" && item !== null ;
25- const textValue = isObject ? item . href : item ;
26- return {
27- id : item . _id || Math . random ( ) ,
28- text : textValue || "" ,
29- isOriginal : ! ! textValue ,
30- } ;
31- } ) ;
32- } ) ;
28+ const InputExtraSourcesList = ( { sources, onChange, disabled, placeholder } : IInputExtraSourcesList ) => {
29+ const { t } = useTranslation ( ) ;
30+ const [ sourcesList , setSourcesList ] = useState ( ( ) => formatSources ( sources as SourceType [ ] ) ) ;
3331
34- useEffect ( ( ) => {
35- handleNotifyChange ( items ) ;
36- } , [ ] ) ;
32+ const handleSubmit = ( hrefsList : typeof sourcesList ) => {
33+ const updatedHrefs = [ ...new Set ( hrefsList . map ( source => source . href . trim ( ) ) . filter ( Boolean ) ) ] ;
34+ onChange ( updatedHrefs ) ;
35+ } ;
3736
38- const handleNotifyChange = ( newItems : any [ ] ) => {
39- setItems ( newItems ) ;
40- const uniqueTexts = Array . from (
41- new Set (
42- newItems
43- . map ( i => i . text . trim ( ) )
44- . filter ( text => text !== "" )
45- )
46- ) ;
37+ const updateSources = ( id : string , newHref : string ) => {
38+ const newHrefList = sourcesList . map ( source => source . id === id ? { ...source , href : newHref } : source ) ;
39+ setSourcesList ( newHrefList ) ;
40+ handleSubmit ( newHrefList ) ;
41+ } ;
4742
48- onChange ( uniqueTexts ) ;
49- } ;
43+ const addField = ( ) => {
44+ if ( disabled ) return ;
45+ const newHrefList = [ ...sourcesList , createEmptySource ( ) ] ;
46+ setSourcesList ( newHrefList ) ;
47+ handleSubmit ( newHrefList ) ;
48+ } ;
5049
51- const addField = ( ) => {
52- if ( disabled ) return ;
53- handleNotifyChange ( [ ...items , { id : Math . random ( ) , text : "" , isOriginal : false } ] ) ;
54- } ;
50+ const removeField = ( id : string , index : number ) => {
51+ if ( disabled || index === 0 ) return ;
52+ const newHrefList = sourcesList . filter ( ( source ) => source . id !== id ) ;
5553
56- const removeField = ( id : any ) => {
57- const index = items . findIndex ( ( item ) => item . id === id ) ;
58- if ( disabled || index === 0 ) return ;
59- handleNotifyChange ( items . filter ( ( item ) => item . id !== id ) ) ;
60- } ;
54+ setSourcesList ( newHrefList ) ;
55+ handleSubmit ( newHrefList ) ;
56+ } ;
6157
62- const updateField = ( id : any , newText : string ) => {
63- const updatedItems = items . map ( ( item ) =>
64- item . id === id ? { ...item , text : newText } : item
65- ) ;
66- handleNotifyChange ( updatedItems ) ;
67- } ;
58+ return (
59+ < Grid container justifyContent = "center" >
60+ { sourcesList . map ( ( source , index ) => (
61+ < Grid item
62+ key = { source . id }
63+ style = { {
64+ display : "flex" ,
65+ width : "100%" ,
66+ gap : 12 ,
67+ marginTop : 12
68+ } }
69+ >
70+ < AletheiaInput
71+ value = { source . href }
72+ disabled = { disabled || ( index === 0 && source . isNewSource ) }
73+ onChange = { ( newHref ) => updateSources ( source . id , newHref . target . value ) }
74+ placeholder = { t ( placeholder ) }
75+ white = "true"
76+ />
6877
69- return (
70- < Grid container >
71- { items . map ( ( item , index ) => (
72- < Box key = { item . id } sx = { { display : "flex" , width : "100%" , gap : 1 , mt : 1 } } >
73- < AletheiaInput
74- value = { item . text }
75- disabled = { disabled || ( index === 0 && item . isOriginal ) }
76- onChange = { ( e ) => updateField ( item . id , e . target . value ) }
77- placeholder = { t ( placeholder ) }
78- white = "true"
79- />
78+ { ! disabled && index !== 0 && (
79+ < AletheiaButton
80+ onClick = { ( ) => removeField ( source . id , index ) }
81+ style = { { minWidth : "40px" } }
82+ >
83+ < DeleteIcon fontSize = "small" />
84+ </ AletheiaButton >
85+ ) }
86+ </ Grid >
87+ ) ) }
8088
81- { ! disabled && index !== 0 && (
82- < AletheiaButton
83- onClick = { ( ) => removeField ( item . id ) }
84- style = { { minWidth : "40px" } }
85- >
86- < DeleteIcon fontSize = "small" />
87- </ AletheiaButton >
88- ) }
89- </ Box >
90- ) ) }
91-
92- { ! disabled && (
93- < AletheiaButton
94- type = { ButtonType . blue }
95- onClick = { addField }
96- style = { { marginTop : "8px" } }
97- >
98- < AddIcon fontSize = "small" />
99- </ AletheiaButton >
100- ) }
101- </ Grid >
102- ) ;
89+ { ! disabled && (
90+ < Grid item >
91+ < AletheiaButton
92+ type = { ButtonType . blue }
93+ onClick = { addField }
94+ style = { { marginTop : 12 } }
95+ >
96+ < AddIcon fontSize = "small" />
97+ { t ( "sourceForm:addNewSourceButton" ) }
98+ </ AletheiaButton >
99+ </ Grid >
100+ ) }
101+ </ Grid >
102+ ) ;
103103} ;
104104
105- export default InputExtraSourcesList ;
105+ export default InputExtraSourcesList ;
0 commit comments