1+ import { Button , CircularLoader , Field , FieldLabel , Help , Icon , Info , MiniScrollableTable , Paragraph , Select , Tooltip } from "@contentstack/venus-components" ;
2+ import { useEffect , useState } from "react" ;
3+ import TableHeader from "./tableHeader" ;
4+ import SingleRowComp from "./singleRowComp" ;
5+ import { useSelector } from "react-redux" ;
6+ import { RootState } from "../../../store" ;
7+ import { getLocales } from "../../../services/api/upload.service" ;
8+ import { getStackLocales } from "../../../services/api/stacks.service" ;
9+ import { all } from "axios" ;
10+
11+ const Mapper = ( { cmsLocaleOptions, handleLangugeDelete, options, masterLocale } : { cmsLocaleOptions : Array < any > , handleLangugeDelete : any , options :any , masterLocale :string } ) => {
12+
13+
14+ const handleSelectedLocale = ( data : any , index : number ) => {
15+ return ;
16+
17+ }
18+
19+ const csValue = null ;
20+ return (
21+ < >
22+ { cmsLocaleOptions ?. length > 0 ?
23+ cmsLocaleOptions ?. map ( ( locale :any , index :any ) => (
24+ < div key = { index } className = "lang-container" >
25+ < Select
26+ value = { locale ?. value === 'master_locale' ? locale : '' }
27+ onChange = { ( key : any ) => handleSelectedLocale ( key , 1 ) }
28+ options = { options }
29+ placeholder = {
30+ 'select language'
31+ }
32+ isSearchable
33+ //menuShouldScrollIntoView
34+ maxMenuHeight = { 150 }
35+ multiDisplayLimit = { 5 }
36+ menuPortalTarget = { document . querySelector ( ".language-mapper" ) }
37+ width = "270px"
38+ version = "v2"
39+ hideSelectedOptions = { true }
40+ isDisabled = { locale ?. value === 'master_locale' ? true : false }
41+ className = "select-container"
42+ noOptionsMessage = { ( ) => '' }
43+ />
44+ < span className = "span" > -</ span >
45+ < Select
46+ value = { csValue }
47+ onChange = { ( data : any ) => handleSelectedLocale ( data , 1 ) }
48+ options = { [ ] }
49+ placeholder = {
50+ 'select language'
51+ }
52+ //isSearchable
53+ //menuShouldScrollIntoView
54+ multiDisplayLimit = { 5 }
55+ //menuPortalTarget={document.querySelector(".config-wrapper")}
56+ width = "270px"
57+ version = "v2"
58+ hideSelectedOptions = { true }
59+ noOptionsMessage = { ( ) => '' }
60+ />
61+ < div className = { '' } >
62+ { locale ?. value !== 'master_locale' &&
63+ < Tooltip
64+ content = {
65+ 'Delete'
66+ }
67+ position = "top"
68+ showArrow = { false }
69+ >
70+ < Icon
71+ icon = "Trash"
72+ size = "mini"
73+ className = "contentTypeRows__icon"
74+ onClick = { ( ) => {
75+ handleLangugeDelete ( index , locale ) }
76+ }
77+ hover
78+ hoverType = "secondary"
79+ shadow = "medium"
80+ />
81+ </ Tooltip > }
82+ </ div >
83+
84+ </ div >
85+
86+ ) ) :
87+ < Info
88+ className = "info-tag"
89+ icon = { < Icon icon = 'Information' version = 'v2' size = 'small' > </ Icon > }
90+ //version="v2"
91+ content = "No langauges configured"
92+ type = "light" />
93+ }
94+ </ >
95+
96+
97+ ) ;
98+
99+ }
100+
101+ const LanguageMapper = ( ) => {
102+ const newMigrationData = useSelector ( ( state :RootState ) => state ?. migration ?. newMigrationData ) ;
103+ const [ newEntry , setnewEntry ] = useState < boolean > ( false ) ;
104+ const [ options , setoptions ] = useState ( [ {
105+ label : '1' ,
106+ value : 'hello'
107+ } ] ) ;
108+ const [ cmsLocaleOptions , setcmsLocaleOptions ] = useState < { label : string ; value : string } [ ] > ( [ ] ) ;
109+
110+
111+ const selectedOrganisation = useSelector ( ( state :RootState ) => state ?. authentication ?. selectedOrganisation ) ;
112+ const [ isLoading , setisLoading ] = useState < boolean > ( false ) ;
113+
114+ useEffect ( ( ) => {
115+ const fetchData = async ( ) => {
116+ try {
117+ setisLoading ( true ) ;
118+ const res = await fetchLocales ( ) ;
119+ console . log ( "res ---> " , res ?. data ?. locales ) ;
120+ const allLocales :any = Object . keys ( res ?. data ?. locales || { } ) . map ( ( key ) => ( {
121+ label : key ,
122+ value : key
123+ } ) ) ;
124+
125+ setoptions ( allLocales ) ;
126+ setcmsLocaleOptions ( ( prevList : any ) => {
127+
128+ const newLabel = newMigrationData ?. destination_stack ?. selectedStack ?. master_locale ;
129+
130+ const isPresent = prevList . some ( ( item : any ) => item . value === 'master_locale' ) ;
131+
132+ if ( ! isPresent ) {
133+ return [
134+ ...prevList ,
135+ {
136+ label : newLabel ,
137+ value : 'master_locale' ,
138+ } ,
139+ ] ;
140+ }
141+
142+ return prevList ;
143+ } ) ;
144+
145+ setisLoading ( false ) ;
146+ } catch ( error ) {
147+ console . error ( "Error fetching locales:" , error ) ;
148+ }
149+ } ;
150+
151+ fetchData ( ) ;
152+ } , [ ] ) ;
153+
154+ const fetchLocales = async ( ) => {
155+ return await getStackLocales (
156+ newMigrationData ?. destination_stack ?. selectedOrg ?. value ,
157+ newMigrationData ?. destination_stack ?. selectedStack ?. value
158+ ) ;
159+ } ;
160+ const addRowComp = ( ) => {
161+ setnewEntry ( true ) ;
162+ setcmsLocaleOptions ( ( prevList : any ) => [
163+ ...prevList , // Keep existing elements
164+ {
165+ label : `${ prevList . length + 1 } ` , // Generate new label
166+ value : 'name'
167+ }
168+ ] ) ;
169+
170+
171+ }
172+
173+ const handleDeleteLocale = ( id :number , locale : any ) => {
174+ setcmsLocaleOptions ( ( prevList ) => {
175+ return prevList . filter ( ( item :any ) => item . label !== locale . label ) } ) ;
176+
177+ }
178+
179+ return (
180+ < div className = "mini-table" >
181+ { isLoading ?
182+ < CircularLoader > </ CircularLoader >
183+ :
184+ < >
185+ < MiniScrollableTable
186+ width = { "600px" }
187+ headerComponent = { < TableHeader
188+ cms = { newMigrationData ?. legacy_cms ?. selectedCms ?. parent } /> }
189+ rowComponent = {
190+ < Mapper
191+ masterLocale = { newMigrationData ?. destination_stack ?. selectedStack ?. master_locale }
192+ options = { options }
193+ cmsLocaleOptions = { cmsLocaleOptions }
194+ handleLangugeDelete = { handleDeleteLocale }
195+ /> }
196+ // footerComponent={
197+ // <Button className="ml-10 mt-10 mb-10"
198+ // buttonType="secondary"
199+ // version={'v2'}
200+ // icon="AddPlus"
201+ // onClick={addRowComp}
202+ // size='small'>
203+ // Add Language
204+ // </Button>
205+
206+ // }
207+ type = "Secondary"
208+ />
209+ < Button className = "ml-10 mt-10 mb-10"
210+ buttonType = "secondary"
211+ aria-label = "add language"
212+ version = { 'v2' }
213+ icon = "AddPlus"
214+ onClick = { addRowComp }
215+ size = 'small' >
216+ Add Language
217+ </ Button >
218+ </ >
219+
220+ }
221+
222+ </ div >
223+ )
224+
225+ }
226+
227+ export default LanguageMapper ;
0 commit comments