13 changed files with 1900 additions and 74 deletions
-
2src/app/router.tsx
-
6src/config/navigation.ts
-
39src/features/custom-trip/CustomTripDestinationDetailPage.tsx
-
29src/features/custom-trip/CustomTripPage.tsx
-
21src/features/custom-trip/components/CustomTripDestinationsTab.tsx
-
115src/features/custom-trip/services/custom-trip-api.ts
-
1441src/features/tours/TourHotelsPage.tsx
-
1src/features/tours/services/tour-api.ts
-
17src/features/tours/types.ts
-
32src/locales/ar.json
-
32src/locales/en.json
-
32src/locales/fa.json
-
207src/stores/hotels-catalog.ts
1441
src/features/tours/TourHotelsPage.tsx
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
@ -0,0 +1,207 @@ |
|||
import { create } from 'zustand' |
|||
import type { CustomTripHotelItem, CustomTripDestinationItem } from '@/features/custom-trip/types' |
|||
import { OFFICIAL_PRESET_CITIES } from '@/features/custom-trip/components/CustomTripDestinationsTab' |
|||
|
|||
/** |
|||
* بررسی تطابق هتل با شهر مقصد (پشتیبانی از نام فارسی، عربی، انگلیسی و کلید استاندارد مقصد) |
|||
*/ |
|||
export function doesHotelMatchDestination( |
|||
hotel: CustomTripHotelItem, |
|||
dest: CustomTripDestinationItem | string | null | undefined |
|||
): boolean { |
|||
if (!dest) return false |
|||
const hCity = (hotel.cityName || '').trim().toLowerCase() |
|||
if (!hCity) return false |
|||
|
|||
if (typeof dest === 'string') { |
|||
const target = dest.trim().toLowerCase() |
|||
if (!target || target === 'all') return true |
|||
return hCity === target || hCity.includes(target) || target.includes(hCity) |
|||
} |
|||
|
|||
const candidates = new Set<string>() |
|||
if (dest.name) candidates.add(dest.name.trim().toLowerCase()) |
|||
if (dest.nameEn) candidates.add(dest.nameEn.trim().toLowerCase()) |
|||
if (dest.id) candidates.add(String(dest.id).trim().toLowerCase()) |
|||
|
|||
if (dest.translations) { |
|||
Object.values(dest.translations).forEach((t) => { |
|||
if (t?.name) candidates.add(t.name.trim().toLowerCase()) |
|||
}) |
|||
} |
|||
|
|||
const preset = OFFICIAL_PRESET_CITIES.find( |
|||
(c) => |
|||
c.id.toLowerCase() === String(dest.id).toLowerCase() || |
|||
c.names.fa === dest.name || |
|||
c.names.ar === dest.name || |
|||
c.names.en.toLowerCase() === (dest.nameEn || '').toLowerCase() |
|||
) |
|||
if (preset) { |
|||
candidates.add(preset.id.toLowerCase()) |
|||
Object.values(preset.names).forEach((n) => { |
|||
if (n) candidates.add(n.trim().toLowerCase()) |
|||
}) |
|||
} |
|||
|
|||
for (const cand of candidates) { |
|||
if (!cand) continue |
|||
if (hCity === cand || hCity.includes(cand) || cand.includes(hCity)) { |
|||
return true |
|||
} |
|||
} |
|||
|
|||
return false |
|||
} |
|||
|
|||
function recalculateDestinationHotelCounts( |
|||
destinations: CustomTripDestinationItem[], |
|||
hotels: CustomTripHotelItem[] |
|||
): CustomTripDestinationItem[] { |
|||
if (!destinations.length) return destinations |
|||
return destinations.map((dest) => { |
|||
const count = hotels.filter((h) => doesHotelMatchDestination(h, dest)).length |
|||
return { |
|||
...dest, |
|||
hotelsCount: count, |
|||
} |
|||
}) |
|||
} |
|||
|
|||
const SYNC_CHANNEL_NAME = 'aqila_hotels_catalog_sync' |
|||
let broadcastChannel: BroadcastChannel | null = null |
|||
|
|||
if (typeof window !== 'undefined' && 'BroadcastChannel' in window) { |
|||
try { |
|||
broadcastChannel = new BroadcastChannel(SYNC_CHANNEL_NAME) |
|||
} catch { |
|||
broadcastChannel = null |
|||
} |
|||
} |
|||
|
|||
interface HotelsCatalogState { |
|||
hotels: CustomTripHotelItem[] |
|||
destinations: CustomTripDestinationItem[] |
|||
isLoaded: boolean |
|||
lastUpdatedAt: number |
|||
setHotels: ( |
|||
updater: CustomTripHotelItem[] | ((prev: CustomTripHotelItem[]) => CustomTripHotelItem[]), |
|||
broadcast?: boolean |
|||
) => void |
|||
setDestinations: ( |
|||
updater: |
|||
| CustomTripDestinationItem[] |
|||
| ((prev: CustomTripDestinationItem[]) => CustomTripDestinationItem[]), |
|||
broadcast?: boolean |
|||
) => void |
|||
upsertHotel: (hotel: CustomTripHotelItem, previousId?: string | number, broadcast?: boolean) => void |
|||
removeHotel: (id: string | number, broadcast?: boolean) => void |
|||
} |
|||
|
|||
export const useHotelsCatalogStore = create<HotelsCatalogState>((set, get) => ({ |
|||
hotels: [], |
|||
destinations: [], |
|||
isLoaded: false, |
|||
lastUpdatedAt: 0, |
|||
|
|||
setHotels: (updater, broadcast = false) => { |
|||
const nextHotels = typeof updater === 'function' ? updater(get().hotels) : updater |
|||
const nextDestinations = recalculateDestinationHotelCounts(get().destinations, nextHotels) |
|||
set({ |
|||
hotels: nextHotels, |
|||
destinations: nextDestinations, |
|||
isLoaded: true, |
|||
lastUpdatedAt: Date.now(), |
|||
}) |
|||
if (broadcast && broadcastChannel) { |
|||
try { |
|||
broadcastChannel.postMessage({ type: 'SET_HOTELS', hotels: nextHotels }) |
|||
} catch {} |
|||
} |
|||
}, |
|||
|
|||
setDestinations: (updater, broadcast = false) => { |
|||
const rawDestinations = |
|||
typeof updater === 'function' ? updater(get().destinations) : updater |
|||
const currentHotels = get().hotels |
|||
const nextDestinations = |
|||
currentHotels.length > 0 |
|||
? recalculateDestinationHotelCounts(rawDestinations, currentHotels) |
|||
: rawDestinations |
|||
set({ |
|||
destinations: nextDestinations, |
|||
lastUpdatedAt: Date.now(), |
|||
}) |
|||
if (broadcast && broadcastChannel) { |
|||
try { |
|||
broadcastChannel.postMessage({ type: 'SET_DESTINATIONS', destinations: nextDestinations }) |
|||
} catch {} |
|||
} |
|||
}, |
|||
|
|||
upsertHotel: (hotel, previousId, broadcast = true) => { |
|||
const current = get().hotels |
|||
const matchId = (h: CustomTripHotelItem) => |
|||
String(h.id) === String(hotel.id) || |
|||
(previousId !== undefined && String(h.id) === String(previousId)) |
|||
|
|||
const exists = current.some(matchId) |
|||
const nextHotels = exists |
|||
? current.map((h) => (matchId(h) ? hotel : h)) |
|||
: [hotel, ...current] |
|||
|
|||
const nextDestinations = recalculateDestinationHotelCounts(get().destinations, nextHotels) |
|||
set({ |
|||
hotels: nextHotels, |
|||
destinations: nextDestinations, |
|||
isLoaded: true, |
|||
lastUpdatedAt: Date.now(), |
|||
}) |
|||
|
|||
if (broadcast && broadcastChannel) { |
|||
try { |
|||
broadcastChannel.postMessage({ |
|||
type: 'UPSERT_HOTEL', |
|||
hotel, |
|||
previousId, |
|||
}) |
|||
} catch {} |
|||
} |
|||
}, |
|||
|
|||
removeHotel: (id, broadcast = true) => { |
|||
const nextHotels = get().hotels.filter((h) => String(h.id) !== String(id)) |
|||
const nextDestinations = recalculateDestinationHotelCounts(get().destinations, nextHotels) |
|||
set({ |
|||
hotels: nextHotels, |
|||
destinations: nextDestinations, |
|||
lastUpdatedAt: Date.now(), |
|||
}) |
|||
|
|||
if (broadcast && broadcastChannel) { |
|||
try { |
|||
broadcastChannel.postMessage({ |
|||
type: 'REMOVE_HOTEL', |
|||
id, |
|||
}) |
|||
} catch {} |
|||
} |
|||
}, |
|||
})) |
|||
|
|||
if (broadcastChannel) { |
|||
broadcastChannel.onmessage = (event) => { |
|||
const data = event.data |
|||
if (!data || typeof data !== 'object') return |
|||
if (data.type === 'SET_HOTELS' && Array.isArray(data.hotels)) { |
|||
useHotelsCatalogStore.getState().setHotels(data.hotels, false) |
|||
} else if (data.type === 'SET_DESTINATIONS' && Array.isArray(data.destinations)) { |
|||
useHotelsCatalogStore.getState().setDestinations(data.destinations, false) |
|||
} else if (data.type === 'UPSERT_HOTEL' && data.hotel) { |
|||
useHotelsCatalogStore.getState().upsertHotel(data.hotel, data.previousId, false) |
|||
} else if (data.type === 'REMOVE_HOTEL' && data.id !== undefined) { |
|||
useHotelsCatalogStore.getState().removeHotel(data.id, false) |
|||
} |
|||
} |
|||
} |
|||
|
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue