Skip to content

Commit

Permalink
Adds action to set homepage to show latest posts
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecoder committed Sep 18, 2024
1 parent bbf80b4 commit ab1c828
Showing 1 changed file with 67 additions and 17 deletions.
84 changes: 67 additions & 17 deletions packages/editor/src/dataviews/actions/set-as-homepage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -123,25 +123,75 @@ const renamePost: Action< PostWithPermissions > = {
}
}

const modalTranslatedString =
'publish' !== item.status
? // translators: %1$s: title of a unpublished page to be set as the home page. %2$s: title of the current home page.
__(
'The page "%1$s" is not published. Set it as the site homepage? This will publish the page and replace the current homepage: "%2$s"'
)
: // translators: %1$s: title of page to be set as the home page. %2$s: title of the current home page.
__(
'Set "%1$s" as the site homepage? This will replace the current homepage: "%2$s"'
);

const modalDescription = sprintf(
modalTranslatedString,
title,
getItemTitle( currentHomePage )
);
async function onSetLatestPostsHomepage( event: React.FormEvent ) {
event.preventDefault();

try {
// @ts-ignore
await editEntityRecord( 'root', 'site', undefined, {
show_on_front: 'posts',
} );

closeModal?.();

await saveEditedEntityRecord( 'root', 'site', undefined, {
show_on_front: 'posts',
} );

createSuccessNotice(
__( 'Homepage set to display latest posts' ),
{
type: 'snackbar',
}
);
onActionPerformed?.( items );
} catch ( error ) {
const typedError = error as CoreDataError;
const errorMessage =
typedError.message && typedError.code !== 'unknown_error'
? typedError.message
: __(
'An error occurred while setting the homepage to display latest posts'
);
createErrorNotice( errorMessage, { type: 'snackbar' } );
}
}
const currentHomePageTitle = getItemTitle( currentHomePage );

let modalDescription;
let submitAction;

if ( item.id === pageForPosts ) {
modalDescription = sprintf(
// translators: %s: title of the current home page.
__(
'Set the homepage to display the latest posts? This will replace the current home page: "%s"'
),
currentHomePageTitle
);
submitAction = onSetLatestPostsHomepage;
} else {
const modalTranslatedString =
'publish' !== item.status
? // translators: %1$s: title of a unpublished page to be set as the home page. %2$s: title of the current home page.
__(
'The page "%1$s" is not published. Set it as the site homepage? This will publish the page and replace the current homepage: "%2$s"'
)
: // translators: %1$s: title of page to be set as the home page. %2$s: title of the current home page.
__(
'Set "%1$s" as the site homepage? This will replace the current homepage: "%2$s"'
);

modalDescription = sprintf(
modalTranslatedString,
title,
currentHomePageTitle
);
submitAction = onSetAsHomepage;
}

return (
<form onSubmit={ onSetAsHomepage }>
<form onSubmit={ submitAction }>
<VStack spacing="5">
<Text>{ modalDescription }</Text>
<HStack justify="right">
Expand Down

0 comments on commit ab1c828

Please sign in to comment.