PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.13
Yatra – Travel Booking & Tour Operator Software v3.0.13
3.0.16 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 All 84 releases
yatra / resources / js / lib / countries.ts

countries.ts in Yatra – Travel Booking & Tour Operator Software 3.0.13, at resources/js/lib/countries.ts

106 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Country data — single source of truth on the client.
3 *
4 * The canonical list lives in PHP at `Yatra\Helpers\FormatHelper::getCountries()`
5 * (full ISO-3166-1 alpha-2 + commonly-accepted territories). The backend
6 * localizes it into `window.yatraAdmin.countries` on every admin page
7 * load, so this module's job is just to:
8 *
9 * 1. Read from that localized payload (zero network round-trip)
10 * 2. Offer the two shapes the React code happens to want — a
11 * `{code => name}` map (what the canonical PHP returns) and an
12 * `Array<{code, name}>` (what dropdown components prefer)
13 * 3. Provide a tiny code→name lookup helper for badge/label render
14 *
15 * If the localized payload is missing (very early bootstrap, tests,
16 * etc.) we fall back to a minimal hardcoded set so the UI doesn't
17 * crash. That fallback is intentionally tiny — production should
18 * always hit the real localized list.
19 *
20 * Operators that want a custom order / curated subset apply the
21 * `yatra_countries_list` PHP filter once; the change propagates here
22 * automatically on the next page load.
23 */
24
25 export type CountryMap = Record<string, string>;
26 export interface CountryOption {
27 code: string;
28 name: string;
29 }
30
31 const FALLBACK_COUNTRIES: CountryMap = {
32 US: "United States",
33 GB: "United Kingdom",
34 CA: "Canada",
35 AU: "Australia",
36 IN: "India",
37 NP: "Nepal",
38 };
39
40 /**
41 * Return the canonical `{CODE: 'Name'}` map. Reads from the localized
42 * payload first; falls back to the minimal set when missing.
43 */
44 export function getCountryMap(): CountryMap {
45 // Admin app localizes onto `yatraAdmin`; the front-end account page localizes
46 // onto `yatraAccountPage`. Read whichever is present so the same helper works
47 // in both contexts (otherwise the account page silently fell back to a tiny
48 // 6-country list and rendered ISO codes like "BH" instead of "Bahrain").
49 const w = window as any;
50 const sources = [w?.yatraAdmin?.countries, w?.yatraAccountPage?.countries];
51 for (const raw of sources) {
52 if (raw && typeof raw === "object" && !Array.isArray(raw)) {
53 const map = raw as CountryMap;
54 // Guard against an accidentally-empty object from the server.
55 if (Object.keys(map).length > 0) return map;
56 }
57 }
58 return FALLBACK_COUNTRIES;
59 }
60
61 /**
62 * Return countries as an array of `{code, name}` — the shape dropdown
63 * components and `<Select>` inputs typically want.
64 *
65 * The PHP source already returns alphabetically sorted by name, so we
66 * preserve insertion order. (Object.entries respects insertion order
67 * for string keys per ES2015.)
68 */
69 export function getCountryOptions(): CountryOption[] {
70 const map = getCountryMap();
71 return Object.entries(map).map(([code, name]) => ({ code, name }));
72 }
73
74 /**
75 * Country options shaped for SearchableSelect, each carrying its national flag.
76 *
77 * The flag base URL is localized by AdminAssetsProvider (`flagBase`), pointing at
78 * the SVG set the plugin already ships and the phone widget already uses — one
79 * source of flag assets for the whole product. Without that global the options
80 * are returned flagless rather than with broken images.
81 */
82 export function getCountrySelectOptions(): Array<{
83 value: string;
84 label: string;
85 icon?: string;
86 }> {
87 const base = (window as any).yatraAdmin?.flagBase || "";
88
89 return getCountryOptions().map((c) => ({
90 value: c.code,
91 label: c.name,
92 icon: base ? base + c.code.toLowerCase() + ".svg" : undefined,
93 }));
94 }
95
96 /**
97 * Look up a single country's display name by ISO-3166 code.
98 * Returns the code itself when unknown — same fallback the PHP
99 * helper uses, so the two never disagree on rendering.
100 */
101 export function getCountryName(code: string): string {
102 if (!code) return "";
103 const map = getCountryMap();
104 return map[code.toUpperCase()] ?? code;
105 }
106