-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat!: Adds EmptyState content-type and enable
EmptyState
section o…
…verride (#2184) # This approach introduces Breaking Changes ## What's the purpose of this pull request? `EmptyState` is a complex section that is being used directly (without CMS) on the error 500, error 404, and login pages. While we've been doing section override 2.0, we've seen that it wouldn't be possible without bringing this section to the CMS and making it more generic (without direct use on the pages). So, the idea we discussed is to create three new content-types (login, 500, and 404) and use the EmptyState section on these pages. - [x] Adapts login page to use CMS with `EmptyState` - [x] Adapts error 500 page to use CMS with `EmptyState` - [x] Adapts error 404 page to use CMS with `EmptyState` - [x] This PR also adapts `EmptyState` to use Section Override API v2. ## How to test it? 1. Use the workspace https://formspacefs1239--storeframework.myvtex.com/admin/headless-cms/faststore 2. Change the API workspace to `formspacefs1239` in the `faststore.config.default.js` file. 3. run this project locally with `yarn dev`. 4. confirm if the new content types are in the cms like in the image below <img width="1217" alt="Screenshot 2023-12-28 at 19 25 23" src="https://github.com/vtex/faststore/assets/11325562/d7de163e-09c6-4845-a60f-b7f99da5bcfa"> If they are not created, you should run in the workspace formspacefs1239 the commands: ```sh vtex switch storeframework vtex use formspacefs1239 vtex cms sync faststore ``` Also, add the `EmptyState` section to every page. 5. You can test each page: - login (before redirect): http://localhost:3000/login - error 500: http://localhost:3000/500?errorId=123456&fromUrl=/office - error 404: http://localhost:3000/non-existing-page |Page|| |-|-| |login|<img width="400" alt="Screenshot 2023-12-28 at 19 16 33" src="https://github.com/vtex/faststore/assets/11325562/10672370-c128-49f1-8b8d-c1c2372c0bd0">| |error 500|<img width="400" alt="Screenshot 2023-12-28 at 19 19 26" src="https://github.com/vtex/faststore/assets/11325562/0b60a25b-2f08-4afc-ad04-757cd2c9433b">| |error 404|<img width="400" alt="Screenshot 2023-12-28 at 19 14 06" src="https://github.com/vtex/faststore/assets/11325562/05b639c4-f0c6-4a4e-96e9-44ea5ccdb170">| - Starter PR should be opened after emerging this PR and publishing the content types. ## References - #2091 - vtex-sites/starter.store#246 --------- Co-authored-by: Fanny <fanny.chien@vtex.com.br>
- Loading branch information
1 parent
1883bd4
commit 7aa6d91
Showing
11 changed files
with
301 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/core/src/components/sections/EmptyState/DefaultComponents.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { EmptyState as UIEmptyState } from '@faststore/ui' | ||
|
||
export const EmptyStateDefaultComponents = { | ||
EmptyState: UIEmptyState, | ||
} as const |
99 changes: 91 additions & 8 deletions
99
packages/core/src/components/sections/EmptyState/EmptyState.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,116 @@ | ||
import { ReactNode } from 'react' | ||
import type { PropsWithChildren } from 'react' | ||
import { useRouter } from 'next/router' | ||
|
||
import { Icon as UIIcon, Loader as UILoader } from '@faststore/ui' | ||
|
||
import { useOverrideComponents } from '../../../sdk/overrides/OverrideContext' | ||
|
||
import Section from '../Section' | ||
|
||
import styles from './section.module.scss' | ||
|
||
import { EmptyState as EmptyStateWrapper } from 'src/components/sections/EmptyState/Overrides' | ||
import { EmptyStateDefaultComponents } from './DefaultComponents' | ||
import { getOverridableSection } from '../../../sdk/overrides/getOverriddenSection' | ||
|
||
export interface EmptyStateProps { | ||
/** | ||
* Title for the `EmptyState` component. | ||
*/ | ||
title: string | ||
titleIcon?: ReactNode | ||
/** | ||
* A React component that will be rendered as an icon. | ||
*/ | ||
titleIcon?: { | ||
icon: string | ||
alt: string | ||
} | ||
/** | ||
* Subtitle for the `EmptyState` component. | ||
*/ | ||
subtitle?: string | ||
/** | ||
* Boolean that makes the loader be shown. | ||
*/ | ||
showLoader?: boolean | ||
/** | ||
* Object that manages the error state descriptions. | ||
*/ | ||
errorState?: { | ||
errorId?: { | ||
show?: boolean | ||
description?: string | ||
} | ||
fromUrl?: { | ||
show?: boolean | ||
description?: string | ||
} | ||
} | ||
} | ||
|
||
const useErrorState = () => { | ||
const router = useRouter() | ||
const { | ||
query: { errorId, fromUrl }, | ||
pathname, | ||
asPath, | ||
} = router | ||
|
||
return { | ||
errorId, | ||
fromUrl: fromUrl ?? asPath ?? pathname, | ||
} | ||
} | ||
|
||
function EmptyState({ | ||
title = EmptyStateWrapper.props.title, | ||
titleIcon = EmptyStateWrapper.props.titleIcon, | ||
title, | ||
titleIcon, | ||
children, | ||
subtitle, | ||
errorState, | ||
showLoader = false, | ||
}: PropsWithChildren<EmptyStateProps>) { | ||
const { EmptyState: EmptyStateWrapper } = | ||
useOverrideComponents<'EmptyState'>() | ||
const { errorId, fromUrl } = useErrorState() | ||
|
||
const icon = !!titleIcon?.icon ? ( | ||
<UIIcon | ||
name={titleIcon?.icon} | ||
aria-label={titleIcon?.alt} | ||
width={56} | ||
height={56} | ||
weight="thin" | ||
/> | ||
) : ( | ||
EmptyStateWrapper.props.titleIcon | ||
) | ||
|
||
return ( | ||
<Section className={`${styles.section} section-empty-state`}> | ||
<EmptyStateWrapper.Component | ||
bkgColor="light" | ||
{...EmptyStateWrapper.props} | ||
title={title} | ||
titleIcon={titleIcon} | ||
title={title ?? EmptyStateWrapper.props.title} | ||
titleIcon={icon} | ||
> | ||
{!!subtitle && <h2>{subtitle}</h2>} | ||
{!!errorState?.errorId?.show && ( | ||
<p>{`${errorState?.errorId?.description} ${errorId}`}</p> | ||
)} | ||
{!!errorState?.fromUrl?.show && ( | ||
<p>{`${errorState?.fromUrl?.description} ${fromUrl}`}</p> | ||
)} | ||
{showLoader && <UILoader />} | ||
{children} | ||
</EmptyStateWrapper.Component> | ||
</Section> | ||
) | ||
} | ||
|
||
export default EmptyState | ||
const OverridableEmptyState = getOverridableSection<typeof EmptyState>( | ||
'EmptyState', | ||
EmptyState, | ||
EmptyStateDefaultComponents | ||
) | ||
|
||
export default OverridableEmptyState |
15 changes: 15 additions & 0 deletions
15
packages/core/src/components/sections/EmptyState/OverriddenDefaultEmptyState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { override } from 'src/customizations/src/components/overrides/EmptyState' | ||
import { getOverriddenSection } from 'src/sdk/overrides/getOverriddenSection' | ||
|
||
import type { SectionOverrideDefinitionV1 } from 'src/typings/overridesDefinition' | ||
import EmptyState from './EmptyState' | ||
|
||
/** | ||
* This component exists to support overrides 1.0 | ||
* | ||
* This allows users to override the default EmptyState section present in the Headless CMS | ||
*/ | ||
export const OverriddenDefaultEmptyState = getOverriddenSection({ | ||
...(override as SectionOverrideDefinitionV1<'EmptyState'>), | ||
Section: EmptyState, | ||
}) |
14 changes: 0 additions & 14 deletions
14
packages/core/src/components/sections/EmptyState/Overrides.tsx
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.