Skip to content

Commit

Permalink
Refactor withPageData HOC for clarity and type safety
Browse files Browse the repository at this point in the history
Enhanced the docstrings for `withPageData` to provide clearer explanations and consistent formatting. Updated type annotations and added a comprehensive example to illustrate usage.
  • Loading branch information
leolabdev committed Aug 31, 2024
1 parent 0edbc52 commit 2f9093c
Showing 1 changed file with 21 additions and 15 deletions.
36 changes: 21 additions & 15 deletions frontend-next-migration/src/shared/lib/hocs/withPageData/index.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
import {ComponentType} from "react";

/**
* Higher-order component to fetch page data based on language parameter and pass it to the wrapped component.
* Higher-Order Component (HOC) to fetch and provide page data based on language.
*
* @template PageProps - Props used by the wrapped component
* @param PageComponent - The React component to wrap and provide data to
* @param getPage - Function that fetches the page data given a language string
*
* @returns A component that fetches data and renders the wrapped component with both fetched data and original props
* @template PageProps - The type of props the page component expects.
* @param {ComponentType<PageProps>} PageComponent - The component to wrap.
* @param {(lng: string) => Promise<{ page: PageProps }>} getPage - The function to fetch page data based on language.
* @returns {ComponentType<DefaultAppRouterProps>} - The wrapped component with fetched data.
*
* @example
* // Define a page component
* const MyPage: React.FC<{ content: string }> = ({ content }) => (
* <div>{content}</div>
* );
*
* // Assume MyPageComponent is a React component and fetchPageData is a function that returns a promise
* // resolving to page data
*
* const MyPageWithData = withPageData(MyPageComponent, fetchPageData);
* // Define a function to fetch page data
* const fetchPage = async (lng: string) => {
* const response = await fetch(`/api/page?lang=${lng}`);
* const data = await response.json();
* return { page: data };
* };
*
* // Usage in a React application
* <MyPageWithData params={{ lng: 'en' }} someOtherProp="value" />
* // Wrap the page component with the HOC
* const MyPageWithData = withPageData(MyPage, fetchPage);
*
* // Now MyPageWithData can be used within a routing context
*/
export function withPageData<PageProps>(
PageComponent: ComponentType<PageProps>,
getPage: (lng: string) => Promise<{ page: PageProps }>
) {
return async function PageWithHOC(props: DefaultAppRouterProps & PageProps) {
): ComponentType<DefaultAppRouterProps> {
return async function PageWithHOC(props: DefaultAppRouterProps) {
const {params} = props;
const data = await getPage(params.lng);

return <PageComponent {...data.page} {...props} />;
};
}
}

0 comments on commit 2f9093c

Please sign in to comment.