11import { useEffect , useMemo , useState } from 'react' ;
22import { cx } from '@emotion/css' ;
33import { Box , Button , Flex , Heading , Note , Paragraph } from '@contentful/f36-components' ;
4- import type { PreviewPayload } from '@types' ;
4+ import type { MappingReviewSuspendPayload , PreviewPayload } from '@types' ;
55import {
66 buildCheckboxEntryList ,
77 collectCheckboxEntryListRowIds ,
@@ -10,23 +10,25 @@ import {
1010import { fetchContentTypesInfoByIds } from '../../../../services/contentTypeService' ;
1111import { CheckboxEntryList } from './CheckboxEntryList' ;
1212import { overviewSectionBox , overviewSectionBoxScrollable } from './OverviewSection.styles' ;
13- import { createEntriesFromPreviewPayload } from '../../../../services/entryService' ;
1413import { PageAppSDK } from '@contentful/app-sdk' ;
1514import type { EntryProps } from 'contentful-management' ;
1615import { SummaryModal } from '../modals/SummaryModal' ;
16+ import { isPreviewPayload } from '../../../../utils/utils' ;
1717
1818interface OverviewSectionProps {
1919 sdk : PageAppSDK ;
20- payload : PreviewPayload ;
20+ payload : PreviewPayload | MappingReviewSuspendPayload ;
2121 oauthToken : string ;
2222 onReturnToMainPage : ( ) => void ;
23+ onCreateSelected ?: ( ) => Promise < void > ;
2324}
2425
2526const OverviewSection = ( {
2627 sdk,
2728 payload,
2829 oauthToken,
2930 onReturnToMainPage,
31+ onCreateSelected,
3032} : OverviewSectionProps ) => {
3133 const [ contentTypeDisplayInfoMap , setContentTypeDisplayInfoMap ] = useState <
3234 ContentTypeDisplayInfoMap | undefined
@@ -35,9 +37,38 @@ const OverviewSection = ({
3537 const [ isCreating , setIsCreating ] = useState ( false ) ;
3638 const [ summaryEntries , setSummaryEntries ] = useState < EntryProps [ ] | null > ( null ) ;
3739
40+ const overviewPayload = useMemo < PreviewPayload > ( ( ) => {
41+ if ( isPreviewPayload ( payload ) ) {
42+ return payload ;
43+ }
44+
45+ return {
46+ // TODO: remove this temporary mock once the backend provides preview entries
47+ entries : [
48+ {
49+ fields : {
50+ url : {
51+ 'en-US' : '/blog/an-url' ,
52+ } ,
53+ internalLabel : {
54+ 'en-US' : '/blog/another-url' ,
55+ } ,
56+ } ,
57+ tempId : 'url_1' ,
58+ contentTypeId : 'url' ,
59+ } ,
60+ ] ,
61+ assets : [ ] ,
62+ referenceGraph : payload . referenceGraph ,
63+ normalizedDocument : payload . normalizedDocument ,
64+ } ;
65+ } , [ payload ] ) ;
66+
3867 useEffect ( ( ) => {
3968 const fetchContentTypesInfo = async ( ) => {
40- const contentTypeIds = [ ...new Set ( payload . entries . map ( ( entry ) => entry . contentTypeId ) ) ]
69+ const contentTypeIds = [
70+ ...new Set ( overviewPayload . entries . map ( ( entry ) => entry . contentTypeId ) ) ,
71+ ]
4172 . filter ( ( id ) : id is string => Boolean ( id ) )
4273 . sort ( ) ;
4374 if ( contentTypeIds . length === 0 ) {
@@ -54,11 +85,11 @@ const OverviewSection = ({
5485 } ;
5586
5687 fetchContentTypesInfo ( ) ;
57- } , [ sdk , payload . entries ] ) ;
88+ } , [ sdk , overviewPayload . entries ] ) ;
5889
5990 const checkboxEntryRows = useMemo (
60- ( ) => buildCheckboxEntryList ( payload , contentTypeDisplayInfoMap , sdk . locales . default ) ,
61- [ payload , contentTypeDisplayInfoMap , sdk . locales . default ]
91+ ( ) => buildCheckboxEntryList ( overviewPayload , contentTypeDisplayInfoMap , sdk . locales . default ) ,
92+ [ overviewPayload , contentTypeDisplayInfoMap , sdk . locales . default ]
6293 ) ;
6394
6495 useEffect ( ( ) => {
@@ -78,24 +109,14 @@ const OverviewSection = ({
78109 } ;
79110
80111 const handleCreateSelected = async ( ) => {
81- if ( selectedEntryTempIds . size === 0 ) {
112+ if ( selectedEntryTempIds . size === 0 || ! onCreateSelected ) {
82113 return ;
83114 }
115+
84116 setIsCreating ( true ) ;
117+
85118 try {
86- const result = await createEntriesFromPreviewPayload (
87- sdk ,
88- payload ,
89- selectedEntryTempIds ,
90- oauthToken
91- ) ;
92- if ( result . errors . length > 0 ) {
93- sdk . notifier . error ( 'Failed to create entries' ) ;
94- } else {
95- setSummaryEntries ( result . createdEntries ) ;
96- }
97- } catch {
98- sdk . notifier . error ( 'Failed to create entries' ) ;
119+ await onCreateSelected ( ) ;
99120 } finally {
100121 setIsCreating ( false ) ;
101122 }
@@ -128,7 +149,7 @@ const OverviewSection = ({
128149 variant = "primary"
129150 onClick = { handleCreateSelected }
130151 isLoading = { isCreating }
131- isDisabled = { selectedEntryTempIds . size === 0 } >
152+ isDisabled = { selectedEntryTempIds . size === 0 || ! onCreateSelected } >
132153 Create selected entries
133154 </ Button >
134155 </ Flex >
0 commit comments