@@ -284,6 +284,7 @@ const ContentMapper = forwardRef(({projectData}: ContentMapperComponentProps, re
284284 const deletedExstingField : ExistingFieldType = existingField ;
285285 const isNewStack = newMigrationData ?. stackDetails ?. isNewStack ;
286286 const [ isFieldDeleted , setIsFieldDeleted ] = useState < boolean > ( false ) ;
287+ const [ isContentDeleted , setIsContentDeleted ] = useState < boolean > ( false ) ;
287288
288289 /** ALL HOOKS Here */
289290 const { projectId = '' } = useParams ( ) ;
@@ -323,13 +324,46 @@ const ContentMapper = forwardRef(({projectData}: ContentMapperComponentProps, re
323324 } , [ tableData ] ) ;
324325
325326 useEffect ( ( ) => {
326- if ( contentTypeMapped && otherCmsTitle ) {
327- setOtherContentType ( {
328- label : contentTypeMapped ?. [ otherCmsTitle ] ?? 'Select content type from existing stack' ,
329- value : contentTypeMapped ?. [ otherCmsTitle ] ?? 'Select content type from existing stack'
330- } ) ;
327+ const mappedContentType = contentTypesList && contentTypesList ?. find ( ( item ) => item ?. title === contentTypeMapped ?. [ otherCmsTitle ] ) ;
328+
329+ if ( contentTypeMapped && otherCmsTitle ) {
330+
331+ if ( mappedContentType ?. uid ) {
332+ setOtherContentType ( {
333+ id : mappedContentType ?. uid ,
334+ label : contentTypeMapped ?. [ otherCmsTitle ] ,
335+ value : contentTypeMapped ?. [ otherCmsTitle ] ,
336+ } ) ;
337+ setIsContentDeleted ( false ) ;
338+ } else {
339+
340+ setOtherContentType ( {
341+ label : 'Select content type from existing stack' ,
342+ value : 'Select content type from existing stack' ,
343+ } ) ;
344+
345+ }
346+
347+ }
348+
349+ } , [ contentTypeMapped , otherCmsTitle , contentTypesList ] ) ;
350+
351+
352+
353+ useEffect ( ( ) => {
354+ if ( isContentDeleted ) {
355+ setContentTypeMapped ( ( prevState : ContentTypeMap ) => {
356+ const { [ otherCmsTitle ] : removed , ...newState } = prevState ;
357+
358+ return newState ;
359+ } ) ;
360+
361+ setIsFieldDeleted ( false ) ;
331362 }
332- } , [ contentTypeMapped , otherCmsTitle ] ) ;
363+
364+
365+ } , [ isContentDeleted , contentTypesList , otherCmsTitle ] )
366+
333367
334368 useEffect ( ( ) => {
335369 if ( contentTypeMapped [ otherCmsTitle ] === otherContentType ?. label ) {
@@ -567,10 +601,10 @@ const ContentMapper = forwardRef(({projectData}: ContentMapperComponentProps, re
567601 const otherTitle = contentTypes ?. [ i ] ?. otherCmsTitle ;
568602 setOtherCmsTitle ( otherTitle ) ;
569603
570- setOtherContentType ( {
571- label : contentTypeMapped ?. [ otherTitle ] || 'Select content type from existing stack' ,
572- value : contentTypeMapped ?. [ otherTitle ] || 'Select content type from existing stack'
573- } ) ;
604+ // setOtherContentType({
605+ // label: contentTypeMapped?.[otherTitle] || 'Select content type from existing stack',
606+ // value: contentTypeMapped?.[otherTitle] || 'Select content type from existing stack'
607+ // });
574608
575609 setContentTypeUid ( contentTypes ?. [ i ] ?. id ?? '' ) ;
576610 fetchFields ( contentTypes ?. [ i ] ?. id ?? '' , searchText || '' ) ;
@@ -583,6 +617,16 @@ const ContentMapper = forwardRef(({projectData}: ContentMapperComponentProps, re
583617 const { data, status } = await getExistingContentTypes ( projectId ) ;
584618 if ( status === 201 ) {
585619 setContentTypesList ( data ?. contentTypes ) ;
620+ const mappedContentType = data ?. contentTypes && data ?. contentTypes ?. find ( ( item :ContentTypeList ) => item ?. title === contentTypeMapped ?. [ otherCmsTitle ] ) ;
621+
622+ if ( mappedContentType ?. uid ) {
623+ setOtherContentType ( {
624+ id : mappedContentType ?. uid ,
625+ label : contentTypeMapped ?. [ otherCmsTitle ] ,
626+ value : contentTypeMapped ?. [ otherCmsTitle ] ,
627+ } ) ;
628+ setIsContentDeleted ( false ) ;
629+ }
586630 }
587631 } ;
588632
@@ -1487,17 +1531,29 @@ const ContentMapper = forwardRef(({projectData}: ContentMapperComponentProps, re
14871531 fieldMapping : updatedRows
14881532 }
14891533 } ;
1534+ let newstate = { } ;
1535+ setContentTypeMapped ( ( prevState : ContentTypeMap ) => {
1536+ const newState = { ...prevState } ;
1537+
1538+ delete newState [ otherCmsTitle ] ;
1539+ newstate = newState ;
1540+
1541+ return newState ;
1542+ } ) ;
14901543 if ( orgId && selectedContentType ) {
14911544 const { status } = await resetToInitialMapping (
14921545 orgId ,
14931546 projectID ,
14941547 selectedContentType ?. id ?? '' ,
14951548 dataCs
14961549 ) ;
1550+
14971551 setExistingField ( { } ) ;
14981552 setContentTypeSchema ( [ ] ) ;
1499- setContentTypeMapped ( { } ) ;
1553+
15001554 if ( status == 200 ) {
1555+ await updateContentMapper ( orgId , projectID , { ...newstate } ) ;
1556+
15011557 Notification ( {
15021558 notificationContent : { text : 'Content type reset successfully' } ,
15031559 notificationProps : {
@@ -1512,6 +1568,51 @@ const ContentMapper = forwardRef(({projectData}: ContentMapperComponentProps, re
15121568
15131569 // Function to fetch single content type
15141570 const handleFetchContentType = async ( ) => {
1571+ const { data , status} = await fetchExistingContentType ( projectId , '' ) ;
1572+ // if(status === 201){
1573+ // Notification({
1574+ // notificationContent: { text: "All Content Types fetched successfully" },
1575+ // notificationProps: {
1576+ // position: 'bottom-center',
1577+ // hideProgressBar: false
1578+ // },
1579+ // type: 'success'
1580+ // });
1581+
1582+ // }
1583+ const contentTypesArr : ContentTypeList [ ] = contentTypesList ;
1584+ const index = contentTypesList . findIndex ( ct => ct ?. uid === data ?. uid ) ;
1585+
1586+ if ( index != - 1 ) {
1587+ contentTypesArr [ index ] = data ;
1588+ }
1589+
1590+ setContentTypesList ( data ?. contentTypes ) ;
1591+
1592+
1593+ const content_type = data ?. contentTypes ?. find ( ( item :any ) => item ?. title === otherContentType ?. label ) ;
1594+ const contentTypeKey = Object . keys ( contentTypeMapped ) . find ( key => contentTypeMapped [ key ] === otherContentType ?. label ) ;
1595+
1596+
1597+ if ( ! content_type && contentTypeKey ) {
1598+ const updatedState = { ...contentTypeMapped } ;
1599+ delete updatedState [ contentTypeKey ] ;
1600+
1601+ setContentTypeMapped ( ( prevState : ContentTypeMap ) => {
1602+ const newState = { ...prevState } ;
1603+
1604+ delete newState [ contentTypeKey ]
1605+
1606+ return newState ;
1607+ } ) ;
1608+ await updateContentMapper ( selectedOrganisation ?. value , projectId , { ... updatedState } ) ;
1609+ setOtherContentType ( {
1610+ label : 'Select content type from existing stack' ,
1611+ value : 'Select content type from existing stack'
1612+
1613+ } ) ;
1614+ }
1615+
15151616 if ( otherContentType ?. label === "Select Content Type" ) {
15161617 Notification ( {
15171618 notificationContent : { text : "Please Select a Content Type to fetch." } ,
@@ -1521,8 +1622,9 @@ const ContentMapper = forwardRef(({projectData}: ContentMapperComponentProps, re
15211622 } ,
15221623 type : 'error'
15231624 } ) ;
1524- } else {
1525- const { data , status} = await fetchExistingContentType ( projectId , otherContentType ?. id ?? '' ) ;
1625+ } else if ( otherContentType ?. id ) {
1626+
1627+ const { data , status} = await fetchExistingContentType ( projectId , otherContentType ?. id ?? '' ) ;
15261628
15271629 const index = contentTypesList . findIndex ( ct => ct ?. uid === data ?. uid ) ;
15281630
@@ -1532,7 +1634,7 @@ const ContentMapper = forwardRef(({projectData}: ContentMapperComponentProps, re
15321634 contentTypesArr [ index ] = data ;
15331635 }
15341636
1535- setContentTypesList ( contentTypesArr ) ;
1637+ // setContentTypesList(contentTypesArr);
15361638 setContentTypeSchema ( data ?. schema ) ;
15371639 if ( status == 201 ) {
15381640 Notification ( {
@@ -1560,7 +1662,7 @@ const ContentMapper = forwardRef(({projectData}: ContentMapperComponentProps, re
15601662 }
15611663 ] ;
15621664
1563- const isOtherContentType = contentTypesList ?. some ( ( ct ) => ct ?. title === otherContentType ?. label ) ;
1665+ const isOtherContentType = contentTypesList && contentTypesList ?. some ( ( ct ) => ct ?. title === otherContentType ?. label ) ;
15641666
15651667 if ( ! isNewStack ) {
15661668 columns ?. push ( {
0 commit comments