Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 13 additions & 17 deletions src/common/app-link/app-link.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,37 +25,33 @@ export default {
} as ComponentMeta<typeof AppLink>;

export const Default: ComponentStory<typeof AppLink> = ({
routeName,
className,
targetBlank,
pathParams,
queryParams,
children,
route,
}) => (
<AppLink
routeName={routeName}
className={className}
pathParams={pathParams}
queryParams={queryParams}
targetBlank={targetBlank}
>
<AppLink route={route} className={className} targetBlank={targetBlank}>
{children}
</AppLink>
);

Default.argTypes = {
routeName: {
control: {
type: "select",
options: RouteName,
route: {
routeName: {
control: {
type: "select",
options: RouteName,
},
},
},
};
Default.args = {
children: "My Link To Home",
routeName: RouteName.Home,
route: {
routeName: RouteName.Home,
pathParams: {},
queryParams: {},
},
className: "",
targetBlank: false,
pathParams: {},
queryParams: {},
};
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 = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if it makes sense to do something like interface AppLinkProps extends <whatever the Link props type is>

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 = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This type is duplicated, I'd prefer to have it centralized if possible

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
2 changes: 1 addition & 1 deletion src/pages/not-found/not-found.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { RouteName } from "routes/routes";
const NotFound = () => (
<div>
This page does not exist!
<AppLink routeName={RouteName.Home}>Go Home</AppLink>
<AppLink route={{ routeName: RouteName.Home }}>Go Home</AppLink>
</div>
);

Expand Down