| 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 |
const raw = (window as any)?.yatraAdmin?.countries; |
| 46 |
if (raw && typeof raw === "object" && !Array.isArray(raw)) { |
| 47 |
const map = raw as CountryMap; |
| 48 |
// Guard against an accidentally-empty object from the server. |
| 49 |
if (Object.keys(map).length > 0) return map; |
| 50 |
} |
| 51 |
return FALLBACK_COUNTRIES; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Return countries as an array of `{code, name}` — the shape dropdown |
| 56 |
* components and `<Select>` inputs typically want. |
| 57 |
* |
| 58 |
* The PHP source already returns alphabetically sorted by name, so we |
| 59 |
* preserve insertion order. (Object.entries respects insertion order |
| 60 |
* for string keys per ES2015.) |
| 61 |
*/ |
| 62 |
export function getCountryOptions(): CountryOption[] { |
| 63 |
const map = getCountryMap(); |
| 64 |
return Object.entries(map).map(([code, name]) => ({ code, name })); |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Look up a single country's display name by ISO-3166 code. |
| 69 |
* Returns the code itself when unknown — same fallback the PHP |
| 70 |
* helper uses, so the two never disagree on rendering. |
| 71 |
*/ |
| 72 |
export function getCountryName(code: string): string { |
| 73 |
if (!code) return ""; |
| 74 |
const map = getCountryMap(); |
| 75 |
return map[code.toUpperCase()] ?? code; |
| 76 |
} |
| 77 |
|