PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.15
Yatra – Travel Booking & Tour Operator Software v3.0.15
3.0.15 3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 All 83 releases
yatra / resources / js / hooks / useNavigate.ts

useNavigate.ts in Yatra – Travel Booking & Tour Operator Software 3.0.15, at resources/js/hooks/useNavigate.ts

98 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Optional URL params accepted by navigateMenu. Most callers just pass
3 * a subpage string (and optionally a `tab` string for the 2nd arg);
4 * pages that need to deep-link into an action (`?action=view&id=42`)
5 * can pass an object here instead.
6 */
7 export type NavigateMenuParams = {
8 tab?: string;
9 action?: string;
10 id?: string | number;
11 } & Record<string, string | number | undefined>;
12
13 /**
14 * Primary sidebar navigation: replaces the query string with a clean Yatra URL (drops action, id, etc.)
15 * so it matches {@link getUrl} in Layout and avoids full page reloads.
16 *
17 * Accepts the second argument as either a plain tab string OR an
18 * options object. The object form lets pages deep-link into an action
19 * (`navigateMenu("bookings", { action: "view", id: bookingId })`)
20 * without dropping out to the lower-level `useNavigate()` hook.
21 */
22 export function navigateMenu(
23 subpage: string,
24 tabOrParams?: string | NavigateMenuParams,
25 ): void {
26 const pathname = window.location.pathname;
27 const page =
28 new URLSearchParams(window.location.search).get("page") || "yatra";
29 const sp = new URLSearchParams();
30 sp.set("page", page);
31
32 if (subpage === "dashboard") {
33 // Match Layout getUrl("dashboard"): only ?page=yatra
34 } else {
35 sp.set("subpage", subpage);
36 if (typeof tabOrParams === "string") {
37 if (tabOrParams !== "") {
38 sp.set("tab", tabOrParams);
39 }
40 } else if (tabOrParams && typeof tabOrParams === "object") {
41 Object.entries(tabOrParams).forEach(([key, value]) => {
42 if (value === undefined || value === null || value === "") return;
43 sp.set(key, String(value));
44 });
45 }
46 }
47
48 const newUrl = `${pathname}?${sp.toString()}`;
49 window.history.pushState({}, "", newUrl);
50 window.dispatchEvent(new PopStateEvent("popstate"));
51 }
52
53 /**
54 * Navigation hook for updating URL parameters
55 */
56 export const useNavigate = () => {
57 const navigate = (params: {
58 subpage?: string;
59 tab?: string;
60 action?: string;
61 tabMode?: "specific" | "recurring";
62 id?: number | string;
63 trip_id?: number | string;
64 [key: string]: string | number | undefined;
65 }) => {
66 const urlParams = new URLSearchParams(window.location.search);
67
68 const setParam = (key: string, value?: string | number) => {
69 if (value === undefined || value === null) {
70 urlParams.delete(key);
71 } else {
72 urlParams.set(key, value.toString());
73 }
74 };
75
76 const standardKeys = ["subpage", "tab", "action", "id"];
77
78 standardKeys.forEach((key) => {
79 if (Object.prototype.hasOwnProperty.call(params, key)) {
80 setParam(key, params[key]);
81 }
82 });
83
84 // Handle custom parameters (like trip_id, tab_mode)
85 Object.keys(params).forEach((key) => {
86 if (!standardKeys.includes(key)) {
87 setParam(key, params[key]);
88 }
89 });
90
91 const newUrl = `${window.location.pathname}?${urlParams.toString()}`;
92 window.history.pushState({}, "", newUrl);
93 window.dispatchEvent(new PopStateEvent("popstate"));
94 };
95
96 return { navigate, navigateMenu };
97 };
98