-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve backlink in order details and draft order details (#5193)
* improve backlink in order details * fix tests * pass navigator opts to datagrid * make hook generic * fix ts * fix test --------- Co-authored-by: Paweł Chyła <chyla1988@gmail.com>
- Loading branch information
Showing
9 changed files
with
165 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"saleor-dashboard": patch | ||
--- | ||
|
||
When navigating to order details from the order list, the back button will now return you to the previous page with the same filters and pagination applied. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { renderHook } from "@testing-library/react-hooks"; | ||
import { useLocation } from "react-router"; | ||
|
||
import { useBackLinkWithState } from "./useBackLinkWithState"; | ||
|
||
jest.mock("react-router", () => ({ | ||
useLocation: jest.fn(), | ||
})); | ||
|
||
describe("useBackLinkWithState", () => { | ||
// Arrange | ||
it("should return path if there is no previous location in state", () => { | ||
(useLocation as jest.Mock).mockReturnValue({ | ||
state: {}, | ||
}); | ||
|
||
// Act | ||
const { result } = renderHook(() => | ||
useBackLinkWithState({ | ||
path: "/orders", | ||
}), | ||
); | ||
|
||
// Assert | ||
expect(result.current).toBe("/orders"); | ||
}); | ||
|
||
it("should return the previous URL if it is an order list path", () => { | ||
// Arrange | ||
|
||
(useLocation as jest.Mock).mockReturnValue({ | ||
state: { | ||
prevLocation: { | ||
pathname: "/orders", | ||
search: "?asc=false&after=cursor", | ||
}, | ||
}, | ||
}); | ||
|
||
// Act | ||
const { result } = renderHook(() => | ||
useBackLinkWithState({ | ||
path: "/orders", | ||
}), | ||
); | ||
|
||
// Assert | ||
expect(result.current).toBe("/orders?asc=false&after=cursor"); | ||
}); | ||
|
||
it("should return the previous URL if it is a draft order list path", () => { | ||
// Arrange | ||
(useLocation as jest.Mock).mockReturnValue({ | ||
state: { | ||
prevLocation: { | ||
pathname: "/orders/drafts", | ||
search: "?asc=false&after=cursor", | ||
}, | ||
}, | ||
}); | ||
|
||
// Act | ||
const { result } = renderHook(() => | ||
useBackLinkWithState({ | ||
path: "/orders/drafts", | ||
}), | ||
); | ||
|
||
// Assert | ||
expect(result.current).toBe("/orders/drafts?asc=false&after=cursor"); | ||
}); | ||
}); |
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,43 @@ | ||
import { useEffect, useState } from "react"; | ||
import { useLocation } from "react-router"; | ||
import urljoin from "url-join"; | ||
|
||
type LocationWithState = Location & { | ||
state?: { | ||
prevLocation?: Location; | ||
}; | ||
}; | ||
|
||
const getPreviousUrl = (location: LocationWithState) => { | ||
if (!location.state?.prevLocation) { | ||
return null; | ||
} | ||
|
||
const { pathname, search } = location.state.prevLocation; | ||
|
||
return urljoin(pathname, search); | ||
}; | ||
|
||
interface UseBackLinkWithState { | ||
path: string; | ||
} | ||
|
||
export const useBackLinkWithState = ({ path }: UseBackLinkWithState) => { | ||
const location = useLocation(); | ||
const [backLink, setBackLink] = useState<string>(path); | ||
|
||
useEffect(() => { | ||
if (location.state) { | ||
const previousUrl = getPreviousUrl(location as LocationWithState); | ||
|
||
// Prevent other links from being set as back link | ||
const isCorrectPath = previousUrl?.includes(path); | ||
|
||
if (isCorrectPath && previousUrl) { | ||
setBackLink(previousUrl); | ||
} | ||
} | ||
}, [location, path]); | ||
|
||
return backLink; | ||
}; |
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
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