Skip to content

Commit

Permalink
chore: added properties from react-router link component
Browse files Browse the repository at this point in the history
  • Loading branch information
mimitorres committed Jul 22, 2024
1 parent 7c944f2 commit 088be2c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
40 changes: 27 additions & 13 deletions src/common/app-link/app-link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,29 +13,35 @@ import type { Params, RouteName } from "../../routes/routes";
defined in this project. To link outside, use <a></a> tags as usual.
*/

type Route = {
pathParams?: Params;
queryParams?: Params;
routeName: RouteName;
};

// Extract pathParams from the routeName
type AppLinkProps<R extends RouteName> = {
type AppLinkProps = {
children: React.ReactNode;
className?: string;
pathParams?: Params;
queryParams?: Params;
routeName: R;
route: Route;
targetBlank?: boolean;
replace?: boolean;
preventScrollReset?: boolean;
state?: any;
reloadDocument?: boolean;
};

const defaultProps = {
className: "",
pathParams: {},
queryParams: {},
targetBlank: false,
replace: false,
preventScrollReset: false,
reloadDocument: false,
};

const AppLink = <R extends RouteName>(props: AppLinkProps<R>) => {
const routePath = getRouteFor(
props.routeName,
props.pathParams,
props.queryParams,
);
const AppLink = (props: AppLinkProps) => {
const { routeName, pathParams, queryParams } = props.route;
const routePath = getRouteFor(routeName, pathParams, queryParams);
let targetBlankProps = {};
if (props.targetBlank) {
targetBlankProps = {
Expand All @@ -44,7 +50,15 @@ const AppLink = <R extends RouteName>(props: AppLinkProps<R>) => {
};
}
return (
<Link className={props.className} to={routePath} {...targetBlankProps}>
<Link
className={props.className}
to={routePath}
replace={props.replace}
preventScrollReset={props.preventScrollReset}
state={props.state}
reloadDocument={props.reloadDocument}
{...targetBlankProps}
>
{props.children}
</Link>
);
Expand Down
16 changes: 7 additions & 9 deletions src/common/app-redirect/app-redirect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,24 @@ import type { Params, RouteName } from "../../routes/routes";
defined in this project. To link outside, use <a></a> tags as usual.
*/

type AppRedirectProps<R extends RouteName> = {
type Route = {
pathParams?: Params;
queryParams?: Params;
routeName: R;
routeName: RouteName;
};

const defaultProps = {
pathParams: {},
queryParams: {},
type AppRedirectProps = {
route: Route;
};

const AppRedirect = <R extends RouteName>(props: AppRedirectProps<R>) => {
const AppRedirect = (props: AppRedirectProps) => {
const { routeName, pathParams, queryParams } = props.route;
const goToPage = useGoToPage();
useEffect(() => {
goToPage(props.routeName, props.pathParams, props.queryParams);
goToPage(routeName, pathParams, queryParams);
}, []);

return null;
};

AppRedirect.defaultProps = defaultProps;

export { AppRedirect };
9 changes: 7 additions & 2 deletions src/common/navbar/navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ export const Navbar = () => (
<Container>
<div className={styles.internalContainer}>
<div>
<AppLink routeName={RouteName.Home}>Logo goes here</AppLink>
<AppLink route={{ routeName: RouteName.Home }}>
Logo goes here
</AppLink>
</div>
<div className={styles.rightContainer}>
<nav>
<AppLink className={globalStyles.link} routeName={RouteName.About}>
<AppLink
className={globalStyles.link}
route={{ routeName: RouteName.About }}
>
About
</AppLink>
</nav>
Expand Down

0 comments on commit 088be2c

Please sign in to comment.