@devtravelcode/widget-native 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. package/LICENSE +18 -0
  2. package/dist/App.d.ts +10 -0
  3. package/dist/App.js +192 -0
  4. package/dist/TravelHubWidget.d.ts +12 -0
  5. package/dist/TravelHubWidget.js +58 -0
  6. package/dist/components/ErrorBoundary.d.ts +18 -0
  7. package/dist/components/ErrorBoundary.js +63 -0
  8. package/dist/components/ui/Modal.d.ts +11 -0
  9. package/dist/components/ui/Modal.js +66 -0
  10. package/dist/components/ui/Skeleton.d.ts +8 -0
  11. package/dist/components/ui/Skeleton.js +33 -0
  12. package/dist/components/ui/badge.d.ts +11 -0
  13. package/dist/components/ui/badge.js +54 -0
  14. package/dist/components/ui/button.d.ts +14 -0
  15. package/dist/components/ui/button.js +101 -0
  16. package/dist/components/ui/card.d.ts +16 -0
  17. package/dist/components/ui/card.js +51 -0
  18. package/dist/components/ui/input.d.ts +8 -0
  19. package/dist/components/ui/input.js +61 -0
  20. package/dist/components/ui/label.d.ts +8 -0
  21. package/dist/components/ui/label.js +17 -0
  22. package/dist/context/WidgetContext.d.ts +29 -0
  23. package/dist/context/WidgetContext.js +64 -0
  24. package/dist/icons/CarIcon.d.ts +4 -0
  25. package/dist/icons/CarIcon.js +3 -0
  26. package/dist/icons/CloseIcon.d.ts +7 -0
  27. package/dist/icons/CloseIcon.js +3 -0
  28. package/dist/icons/FlightIcon.d.ts +4 -0
  29. package/dist/icons/FlightIcon.js +3 -0
  30. package/dist/icons/HotelIcon.d.ts +4 -0
  31. package/dist/icons/HotelIcon.js +3 -0
  32. package/dist/index.d.ts +2 -0
  33. package/dist/index.js +1 -0
  34. package/dist/lib/utils.d.ts +2 -0
  35. package/dist/lib/utils.js +3 -0
  36. package/dist/locales/widgetTranslations.d.ts +1 -0
  37. package/dist/locales/widgetTranslations.js +732 -0
  38. package/dist/modules/hooks/useAirportSearch.d.ts +21 -0
  39. package/dist/modules/hooks/useAirportSearch.js +48 -0
  40. package/dist/modules/hooks/useCarOffers.d.ts +7 -0
  41. package/dist/modules/hooks/useCarOffers.js +47 -0
  42. package/dist/modules/hooks/useCountriesByCodes.d.ts +4 -0
  43. package/dist/modules/hooks/useCountriesByCodes.js +60 -0
  44. package/dist/modules/hooks/useCountriesSearch.d.ts +11 -0
  45. package/dist/modules/hooks/useCountriesSearch.js +55 -0
  46. package/dist/modules/hooks/useDefaultHotelLocation.d.ts +7 -0
  47. package/dist/modules/hooks/useDefaultHotelLocation.js +44 -0
  48. package/dist/modules/hooks/useDetectNationality.d.ts +1 -0
  49. package/dist/modules/hooks/useDetectNationality.js +20 -0
  50. package/dist/modules/hooks/useFlightSearch.d.ts +20 -0
  51. package/dist/modules/hooks/useFlightSearch.js +243 -0
  52. package/dist/modules/hooks/useHotelDetails.d.ts +7 -0
  53. package/dist/modules/hooks/useHotelDetails.js +80 -0
  54. package/dist/modules/hooks/useHotelLocations.d.ts +5 -0
  55. package/dist/modules/hooks/useHotelLocations.js +42 -0
  56. package/dist/modules/hooks/useHotelMapData.d.ts +12 -0
  57. package/dist/modules/hooks/useHotelMapData.js +78 -0
  58. package/dist/modules/hooks/useHotelOffers.d.ts +11 -0
  59. package/dist/modules/hooks/useHotelOffers.js +122 -0
  60. package/dist/modules/hooks/useHotelSearchSSE.d.ts +18 -0
  61. package/dist/modules/hooks/useHotelSearchSSE.js +411 -0
  62. package/dist/modules/hooks/useSearchExpiration.d.ts +6 -0
  63. package/dist/modules/hooks/useSearchExpiration.js +35 -0
  64. package/dist/modules/hooks/useTranslationsHub.d.ts +5 -0
  65. package/dist/modules/hooks/useTranslationsHub.js +48 -0
  66. package/dist/modules/navbar/Navbar.d.ts +8 -0
  67. package/dist/modules/navbar/Navbar.js +69 -0
  68. package/dist/modules/search-form/AgeSelector.d.ts +9 -0
  69. package/dist/modules/search-form/AgeSelector.js +109 -0
  70. package/dist/modules/search-form/CustomSelect.d.ts +14 -0
  71. package/dist/modules/search-form/CustomSelect.js +94 -0
  72. package/dist/modules/search-form/FiltersSkeleton.d.ts +6 -0
  73. package/dist/modules/search-form/FiltersSkeleton.js +35 -0
  74. package/dist/modules/search-form/NationalityInput.d.ts +18 -0
  75. package/dist/modules/search-form/NationalityInput.js +172 -0
  76. package/dist/modules/search-form/Pagination.d.ts +8 -0
  77. package/dist/modules/search-form/Pagination.js +84 -0
  78. package/dist/modules/search-form/SearchExpiredPopup.d.ts +10 -0
  79. package/dist/modules/search-form/SearchExpiredPopup.js +91 -0
  80. package/dist/modules/search-form/SkeletonWidget.d.ts +2 -0
  81. package/dist/modules/search-form/SkeletonWidget.js +33 -0
  82. package/dist/modules/search-form/TimePicker.d.ts +9 -0
  83. package/dist/modules/search-form/TimePicker.js +182 -0
  84. package/dist/modules/search-form/cars/CarEmptyState.d.ts +6 -0
  85. package/dist/modules/search-form/cars/CarEmptyState.js +42 -0
  86. package/dist/modules/search-form/cars/CarFiltersSkeleton.d.ts +6 -0
  87. package/dist/modules/search-form/cars/CarFiltersSkeleton.js +35 -0
  88. package/dist/modules/search-form/cars/CarOffersList.d.ts +43 -0
  89. package/dist/modules/search-form/cars/CarOffersList.js +214 -0
  90. package/dist/modules/search-form/cars/CarResults.d.ts +25 -0
  91. package/dist/modules/search-form/cars/CarResults.js +116 -0
  92. package/dist/modules/search-form/cars/CarSearchForm.d.ts +19 -0
  93. package/dist/modules/search-form/cars/CarSearchForm.js +173 -0
  94. package/dist/modules/search-form/cars/CarSkeleton.d.ts +6 -0
  95. package/dist/modules/search-form/cars/CarSkeleton.js +46 -0
  96. package/dist/modules/search-form/cars/CarTopOffersSlider.d.ts +20 -0
  97. package/dist/modules/search-form/cars/CarTopOffersSlider.js +100 -0
  98. package/dist/modules/search-form/cars/car-filters/CarFilters.d.ts +8 -0
  99. package/dist/modules/search-form/cars/car-filters/CarFilters.js +318 -0
  100. package/dist/modules/search-form/cars/car-filters/FilterSection.d.ts +9 -0
  101. package/dist/modules/search-form/cars/car-filters/FilterSection.js +30 -0
  102. package/dist/modules/search-form/cars/car-filters/index.d.ts +6 -0
  103. package/dist/modules/search-form/cars/car-filters/index.js +3 -0
  104. package/dist/modules/search-form/cars/car-filters/useCarFilters.d.ts +24 -0
  105. package/dist/modules/search-form/cars/car-filters/useCarFilters.js +121 -0
  106. package/dist/modules/search-form/flight/AirlinesSelector.d.ts +8 -0
  107. package/dist/modules/search-form/flight/AirlinesSelector.js +235 -0
  108. package/dist/modules/search-form/flight/CabinClassSelector.d.ts +9 -0
  109. package/dist/modules/search-form/flight/CabinClassSelector.js +63 -0
  110. package/dist/modules/search-form/flight/DatePicker.d.ts +12 -0
  111. package/dist/modules/search-form/flight/DatePicker.js +536 -0
  112. package/dist/modules/search-form/flight/FlightSearchForm.d.ts +18 -0
  113. package/dist/modules/search-form/flight/FlightSearchForm.js +147 -0
  114. package/dist/modules/search-form/flight/LocationInput.d.ts +11 -0
  115. package/dist/modules/search-form/flight/LocationInput.js +294 -0
  116. package/dist/modules/search-form/flight/PassengersPicker.d.ts +16 -0
  117. package/dist/modules/search-form/flight/PassengersPicker.js +203 -0
  118. package/dist/modules/search-form/flight/SwitchLocationBtn.d.ts +9 -0
  119. package/dist/modules/search-form/flight/SwitchLocationBtn.js +48 -0
  120. package/dist/modules/search-form/flight/flight-filters/FiltersContent.d.ts +21 -0
  121. package/dist/modules/search-form/flight/flight-filters/FiltersContent.js +145 -0
  122. package/dist/modules/search-form/flight/flight-filters/FlightFiltersDynamic.d.ts +11 -0
  123. package/dist/modules/search-form/flight/flight-filters/FlightFiltersDynamic.js +213 -0
  124. package/dist/modules/search-form/flight/flight-filters/RangeBlock.d.ts +22 -0
  125. package/dist/modules/search-form/flight/flight-filters/RangeBlock.js +164 -0
  126. package/dist/modules/search-form/flight/flight-filters/index.d.ts +1 -0
  127. package/dist/modules/search-form/flight/flight-filters/index.js +1 -0
  128. package/dist/modules/search-form/flight/flight-results/FlightAdditionalInfo.d.ts +16 -0
  129. package/dist/modules/search-form/flight/flight-results/FlightAdditionalInfo.js +229 -0
  130. package/dist/modules/search-form/flight/flight-results/FlightBaggageToggle.d.ts +15 -0
  131. package/dist/modules/search-form/flight/flight-results/FlightBaggageToggle.js +110 -0
  132. package/dist/modules/search-form/flight/flight-results/FlightCard.d.ts +14 -0
  133. package/dist/modules/search-form/flight/flight-results/FlightCard.js +436 -0
  134. package/dist/modules/search-form/flight/flight-results/FlightInfo.d.ts +12 -0
  135. package/dist/modules/search-form/flight/flight-results/FlightInfo.js +48 -0
  136. package/dist/modules/search-form/flight/flight-results/FlightPriceBlock.d.ts +11 -0
  137. package/dist/modules/search-form/flight/flight-results/FlightPriceBlock.js +36 -0
  138. package/dist/modules/search-form/flight/flight-results/FlightResults.d.ts +22 -0
  139. package/dist/modules/search-form/flight/flight-results/FlightResults.js +109 -0
  140. package/dist/modules/search-form/flight/flight-results/FlightSegments.d.ts +40 -0
  141. package/dist/modules/search-form/flight/flight-results/FlightSegments.js +162 -0
  142. package/dist/modules/search-form/flight/flight-results/FlightsSkeleton.d.ts +6 -0
  143. package/dist/modules/search-form/flight/flight-results/FlightsSkeleton.js +52 -0
  144. package/dist/modules/search-form/flight/flight-results/ProvidersLoader.d.ts +9 -0
  145. package/dist/modules/search-form/flight/flight-results/ProvidersLoader.js +242 -0
  146. package/dist/modules/search-form/hotel/DatePicker.d.ts +1 -0
  147. package/dist/modules/search-form/hotel/DatePicker.js +1 -0
  148. package/dist/modules/search-form/hotel/GuestSelector.d.ts +13 -0
  149. package/dist/modules/search-form/hotel/GuestSelector.js +272 -0
  150. package/dist/modules/search-form/hotel/GuestSelectorMobilePopup.d.ts +2 -0
  151. package/dist/modules/search-form/hotel/GuestSelectorMobilePopup.js +1 -0
  152. package/dist/modules/search-form/hotel/HotelLocation.d.ts +17 -0
  153. package/dist/modules/search-form/hotel/HotelLocation.js +178 -0
  154. package/dist/modules/search-form/hotel/HotelLocationMobilePopup.d.ts +2 -0
  155. package/dist/modules/search-form/hotel/HotelLocationMobilePopup.js +1 -0
  156. package/dist/modules/search-form/hotel/HotelResults.d.ts +21 -0
  157. package/dist/modules/search-form/hotel/HotelResults.js +333 -0
  158. package/dist/modules/search-form/hotel/HotelSearchForm.d.ts +13 -0
  159. package/dist/modules/search-form/hotel/HotelSearchForm.js +130 -0
  160. package/dist/modules/search-form/hotel/HotelsSkeleton.d.ts +6 -0
  161. package/dist/modules/search-form/hotel/HotelsSkeleton.js +40 -0
  162. package/dist/modules/search-form/hotel/PassengersPicker.d.ts +2 -0
  163. package/dist/modules/search-form/hotel/PassengersPicker.js +1 -0
  164. package/dist/modules/search-form/hotel/components/HotelCard.d.ts +11 -0
  165. package/dist/modules/search-form/hotel/components/HotelCard.js +180 -0
  166. package/dist/modules/search-form/hotel/components/HotelEmptyState.d.ts +6 -0
  167. package/dist/modules/search-form/hotel/components/HotelEmptyState.js +34 -0
  168. package/dist/modules/search-form/hotel/components/HotelGallery.d.ts +6 -0
  169. package/dist/modules/search-form/hotel/components/HotelGallery.js +77 -0
  170. package/dist/modules/search-form/hotel/components/HotelList.d.ts +9 -0
  171. package/dist/modules/search-form/hotel/components/HotelList.js +12 -0
  172. package/dist/modules/search-form/hotel/components/HotelMap.d.ts +26 -0
  173. package/dist/modules/search-form/hotel/components/HotelMap.js +707 -0
  174. package/dist/modules/search-form/hotel/components/HotelPin.d.ts +7 -0
  175. package/dist/modules/search-form/hotel/components/HotelPin.js +1 -0
  176. package/dist/modules/search-form/hotel/hotel-filters/HotelFilters.d.ts +29 -0
  177. package/dist/modules/search-form/hotel/hotel-filters/HotelFilters.js +339 -0
  178. package/dist/modules/search-form/hotel/hotel-filters/HotelFiltersBar.d.ts +3 -0
  179. package/dist/modules/search-form/hotel/hotel-filters/HotelFiltersBar.js +2 -0
  180. package/dist/modules/search-form/hotel/hotel-filters/HotelFiltersContent.d.ts +2 -0
  181. package/dist/modules/search-form/hotel/hotel-filters/HotelFiltersContent.js +1 -0
  182. package/dist/modules/search-form/hotel/hotel-filters/HotelFiltersDynamic.d.ts +2 -0
  183. package/dist/modules/search-form/hotel/hotel-filters/HotelFiltersDynamic.js +1 -0
  184. package/dist/modules/search-form/hotel/hotel-filters/HotelFiltersMobilePopup.d.ts +2 -0
  185. package/dist/modules/search-form/hotel/hotel-filters/HotelFiltersMobilePopup.js +1 -0
  186. package/dist/modules/search-form/hotel/hotel-filters/RangeBlock.d.ts +1 -0
  187. package/dist/modules/search-form/hotel/hotel-filters/RangeBlock.js +1 -0
  188. package/dist/modules/search-form/hotel/hotel-filters/index.d.ts +1 -0
  189. package/dist/modules/search-form/hotel/hotel-filters/index.js +1 -0
  190. package/dist/modules/tabs/FlightsTab.d.ts +1 -0
  191. package/dist/modules/tabs/FlightsTab.js +1 -0
  192. package/dist/modules/tabs/HotelsTab.d.ts +1 -0
  193. package/dist/modules/tabs/HotelsTab.js +1 -0
  194. package/dist/store/index.d.ts +22 -0
  195. package/dist/store/index.js +19 -0
  196. package/dist/theme/colors.d.ts +56 -0
  197. package/dist/theme/colors.js +56 -0
  198. package/dist/theme/formStyles.d.ts +76 -0
  199. package/dist/theme/formStyles.js +78 -0
  200. package/dist/utils/applyWidgetColors.d.ts +1 -0
  201. package/dist/utils/applyWidgetColors.js +2 -0
  202. package/dist/utils/dateTime.d.ts +2 -0
  203. package/dist/utils/dateTime.js +23 -0
  204. package/dist/utils/fetchSSE.d.ts +23 -0
  205. package/dist/utils/fetchSSE.js +104 -0
  206. package/dist/utils/getToken.d.ts +3 -0
  207. package/dist/utils/getToken.js +12 -0
  208. package/package.json +64 -0
@@ -0,0 +1,21 @@
1
+ export type Location = {
2
+ id: number | string;
3
+ code: string;
4
+ title: string;
5
+ titleEn: string;
6
+ isAirport?: boolean;
7
+ city?: {
8
+ id: number;
9
+ title: string;
10
+ titleEn: string;
11
+ };
12
+ country?: {
13
+ id: number;
14
+ title: string;
15
+ titleEn: string;
16
+ };
17
+ };
18
+ export declare function useAirportSearch(query: string): {
19
+ locations: Location[];
20
+ loading: boolean;
21
+ };
@@ -0,0 +1,48 @@
1
+ import { useState, useEffect } from "react";
2
+ import { useApiToken } from "../../utils/getToken";
3
+ const API_URL = "https://api.travel-code.com/v1/data/airports";
4
+ export function useAirportSearch(query) {
5
+ const token = useApiToken();
6
+ const [locations, setLocations] = useState([]);
7
+ const [loading, setLoading] = useState(false);
8
+ useEffect(() => {
9
+ if (query.length < 2) {
10
+ setLocations([]);
11
+ return;
12
+ }
13
+ const controller = new AbortController();
14
+ const delay = setTimeout(() => {
15
+ setLoading(true);
16
+ fetch(`${API_URL}?search=${encodeURIComponent(query)}&scope=city,country&limit=20`, {
17
+ method: "GET",
18
+ headers: {
19
+ "Content-Type": "application/json",
20
+ Authorization: `Bearer ${token}`,
21
+ },
22
+ signal: controller.signal,
23
+ })
24
+ .then((res) => {
25
+ if (!res.ok)
26
+ throw new Error(`Ошибка ${res.status}`);
27
+ return res.json();
28
+ })
29
+ .then((data) => {
30
+ const validLocations = (Array.isArray(data) ? data : []).filter((loc) => loc.code);
31
+ setLocations(validLocations);
32
+ })
33
+ .catch((err) => {
34
+ if (err.name !== "AbortError") {
35
+ console.error("Ошибка загрузки аэропортов:", err);
36
+ }
37
+ })
38
+ .finally(() => {
39
+ setLoading(false);
40
+ });
41
+ }, 800);
42
+ return () => {
43
+ clearTimeout(delay);
44
+ controller.abort();
45
+ };
46
+ }, [query]);
47
+ return { locations, loading };
48
+ }
@@ -0,0 +1,7 @@
1
+ export declare const useCarOffers: () => {
2
+ offers: any[];
3
+ loading: boolean;
4
+ error: string | null;
5
+ fetchOffers: (payload: any) => Promise<void>;
6
+ reset: () => void;
7
+ };
@@ -0,0 +1,47 @@
1
+ import { useState } from "react";
2
+ export const useCarOffers = () => {
3
+ const [offers, setOffers] = useState([]);
4
+ const [loading, setLoading] = useState(false);
5
+ const [error, setError] = useState(null);
6
+ const reset = () => {
7
+ setOffers([]);
8
+ setLoading(false);
9
+ setError(null);
10
+ };
11
+ const fetchOffers = async (payload) => {
12
+ setLoading(true);
13
+ setError(null);
14
+ try {
15
+ const response = await fetch("https://api.travelhub.by/discovercars/request", {
16
+ method: "POST",
17
+ headers: { "Content-Type": "application/json" },
18
+ body: JSON.stringify({
19
+ endpoint: "/v1/offers",
20
+ method: "POST",
21
+ data: payload,
22
+ }),
23
+ });
24
+ if (!response.ok) {
25
+ throw new Error(`Ошибка DiscoverCars API: ${response.status} ${response.statusText}`);
26
+ }
27
+ const json = await response.json();
28
+ console.log("✅ Ответ DiscoverCars API:", json);
29
+ if (Array.isArray(json.data)) {
30
+ setOffers(json.data);
31
+ }
32
+ else {
33
+ console.warn("⚠️ Неверный формат ответа:", json);
34
+ setOffers([]);
35
+ }
36
+ }
37
+ catch (err) {
38
+ console.error("❌ Ошибка при загрузке офферов:", err);
39
+ setError(err.message);
40
+ setOffers([]);
41
+ }
42
+ finally {
43
+ setLoading(false);
44
+ }
45
+ };
46
+ return { offers, loading, error, fetchOffers, reset };
47
+ };
@@ -0,0 +1,4 @@
1
+ export declare function useCountriesByCodes(codes: string[] | null | undefined): {
2
+ countries: any[] | null;
3
+ loading: boolean;
4
+ };
@@ -0,0 +1,60 @@
1
+ import { useEffect, useState, useRef } from "react";
2
+ import { useApiToken } from "../../utils/getToken";
3
+ const FLAG_BASE = "https://cdn.travel-code.com/images/remaster/flags";
4
+ export function useCountriesByCodes(codes) {
5
+ const [countries, setCountries] = useState(null);
6
+ const [loading, setLoading] = useState(false);
7
+ const token = useApiToken();
8
+ const cacheRef = useRef({});
9
+ const codesKey = codes ? JSON.stringify(codes) : "";
10
+ useEffect(() => {
11
+ if (!codes || codes.length === 0) {
12
+ setCountries((prev) => (prev === null ? prev : null));
13
+ return;
14
+ }
15
+ const notCached = codes.filter((c) => !cacheRef.current[c]);
16
+ if (notCached.length === 0) {
17
+ const result = codes.map((c) => cacheRef.current[c]);
18
+ setCountries((prev) => {
19
+ if (prev && prev.length === result.length && prev.every((c, i) => c === result[i])) {
20
+ return prev;
21
+ }
22
+ return result;
23
+ });
24
+ return;
25
+ }
26
+ const load = async () => {
27
+ setLoading(true);
28
+ try {
29
+ const joined = notCached.join(",");
30
+ const res = await fetch(`https://api.travel-code.com/v1/data/countries?filter[countryCodes]=${joined}`, {
31
+ headers: {
32
+ "Content-Type": "application/json",
33
+ Authorization: `Bearer ${token}`,
34
+ },
35
+ });
36
+ const data = await res.json();
37
+ if (Array.isArray(data)) {
38
+ data.forEach((c) => {
39
+ cacheRef.current[c.code] = {
40
+ code: c.code,
41
+ title: c.title,
42
+ titleEn: c.titleEn,
43
+ flag: `${FLAG_BASE}/${c.code}.svg`,
44
+ };
45
+ });
46
+ setCountries(codes.map((c) => cacheRef.current[c]));
47
+ }
48
+ }
49
+ catch (e) {
50
+ console.error("Ошибка загрузки countries:", e);
51
+ setCountries(null);
52
+ }
53
+ finally {
54
+ setLoading(false);
55
+ }
56
+ };
57
+ load();
58
+ }, [codesKey, token]);
59
+ return { countries, loading };
60
+ }
@@ -0,0 +1,11 @@
1
+ export type ApiCountry = {
2
+ id: number;
3
+ code: string;
4
+ title: string;
5
+ titleEn: string;
6
+ };
7
+ export declare function useCountriesSearch(query: string): {
8
+ results: ApiCountry[];
9
+ loading: boolean;
10
+ error: string | null;
11
+ };
@@ -0,0 +1,55 @@
1
+ import { useState, useEffect, useRef } from "react";
2
+ import { useApiToken } from "../../utils/getToken";
3
+ const API_URL = "https://api.travel-code.com/v1/data/countries";
4
+ export function useCountriesSearch(query) {
5
+ const token = useApiToken();
6
+ const [results, setResults] = useState([]);
7
+ const [loading, setLoading] = useState(false);
8
+ const [error, setError] = useState(null);
9
+ const cacheRef = useRef({});
10
+ const controllerRef = useRef(null);
11
+ useEffect(() => {
12
+ if (!query || query.trim().length < 2) {
13
+ setResults([]);
14
+ return;
15
+ }
16
+ if (cacheRef.current[query]) {
17
+ setResults(cacheRef.current[query]);
18
+ return;
19
+ }
20
+ const fetchCountries = async () => {
21
+ setLoading(true);
22
+ setError(null);
23
+ if (controllerRef.current)
24
+ controllerRef.current.abort();
25
+ controllerRef.current = new AbortController();
26
+ try {
27
+ const res = await fetch(`${API_URL}?search=${encodeURIComponent(query)}`, {
28
+ method: "GET",
29
+ headers: {
30
+ "Content-Type": "application/json",
31
+ Authorization: `Bearer ${token}`,
32
+ },
33
+ signal: controllerRef.current.signal,
34
+ });
35
+ if (!res.ok)
36
+ throw new Error(`Ошибка ${res.status}`);
37
+ const data = await res.json();
38
+ cacheRef.current[query] = data;
39
+ setResults(data);
40
+ }
41
+ catch (err) {
42
+ if (err.name !== "AbortError") {
43
+ setError("Ошибка загрузки стран");
44
+ console.error(err);
45
+ }
46
+ }
47
+ finally {
48
+ setLoading(false);
49
+ }
50
+ };
51
+ const timeout = setTimeout(fetchCountries, 400);
52
+ return () => clearTimeout(timeout);
53
+ }, [query]);
54
+ return { results, loading, error };
55
+ }
@@ -0,0 +1,7 @@
1
+ export declare function useDefaultHotelLocation(defaultId?: string | null, lang?: "ru" | "en"): {
2
+ defaultItem: {
3
+ id: string;
4
+ name: string;
5
+ } | null;
6
+ loadingDefault: boolean;
7
+ };
@@ -0,0 +1,44 @@
1
+ import { useState, useEffect } from "react";
2
+ import { useApiToken } from "../../utils/getToken";
3
+ const API_URL = "https://api.travel-code.com/v1/data/hotel-location?id=";
4
+ export function useDefaultHotelLocation(defaultId, lang = "ru") {
5
+ const token = useApiToken();
6
+ const [defaultItem, setDefaultItem] = useState(null);
7
+ const [loadingDefault, setLoadingDefault] = useState(false);
8
+ useEffect(() => {
9
+ if (!defaultId)
10
+ return;
11
+ const load = async () => {
12
+ try {
13
+ setLoadingDefault(true);
14
+ const res = await fetch(`${API_URL}${defaultId}`, {
15
+ headers: {
16
+ "Content-Type": "application/json",
17
+ Authorization: `Bearer ${token}`,
18
+ },
19
+ });
20
+ if (!res.ok)
21
+ return;
22
+ const data = await res.json();
23
+ const result = data?.result;
24
+ if (result) {
25
+ const name = lang === "en"
26
+ ? result.nameEn || result.name || ""
27
+ : result.nameRu || result.name || "";
28
+ setDefaultItem({
29
+ id: String(defaultId),
30
+ name,
31
+ });
32
+ }
33
+ }
34
+ catch (err) {
35
+ console.warn("Ошибка загрузки default локации:", err);
36
+ }
37
+ finally {
38
+ setLoadingDefault(false);
39
+ }
40
+ };
41
+ load();
42
+ }, [defaultId, lang, token]);
43
+ return { defaultItem, loadingDefault };
44
+ }
@@ -0,0 +1 @@
1
+ export declare const useDetectNationality: () => string;
@@ -0,0 +1,20 @@
1
+ import { useState, useEffect } from "react";
2
+ export const useDetectNationality = () => {
3
+ const [code, setCode] = useState("");
4
+ useEffect(() => {
5
+ const detect = async () => {
6
+ try {
7
+ const res = await fetch("https://ipwho.is/");
8
+ const data = await res.json();
9
+ if (data?.success !== false && data.country_code) {
10
+ setCode(data.country_code);
11
+ }
12
+ }
13
+ catch (e) {
14
+ console.warn("IP detect failed:", e);
15
+ }
16
+ };
17
+ detect();
18
+ }, []);
19
+ return code;
20
+ };
@@ -0,0 +1,20 @@
1
+ export declare function useFlightSearch(): {
2
+ results: any[];
3
+ filtersData: any;
4
+ airlinesData: Record<string, string>;
5
+ dictionaries: any;
6
+ currencySign: any;
7
+ loading: boolean;
8
+ isFiltering: boolean;
9
+ error: string | null;
10
+ status: string;
11
+ sessionId: string | null;
12
+ total: number;
13
+ limit: number;
14
+ offset: number;
15
+ isPageLoading: boolean;
16
+ startSearch: (params: any) => Promise<any>;
17
+ applyFilters: (sid: string | null, filters?: Record<string, any>, page?: number) => Promise<void>;
18
+ fetchPage: (page: number) => Promise<void>;
19
+ reset: () => void;
20
+ };
@@ -0,0 +1,243 @@
1
+ import { useState, useCallback, useRef } from "react";
2
+ import { useApiToken } from "../../utils/getToken";
3
+ const API_BASE = "https://api.travel-code.com/v1/search/flights";
4
+ const MAX_POLL_ATTEMPTS = 50;
5
+ const POLL_INTERVAL = 1200;
6
+ export function useFlightSearch() {
7
+ const token = useApiToken();
8
+ const [results, setResults] = useState([]);
9
+ const [filtersData, setFiltersData] = useState(null);
10
+ const [airlinesData, setAirlinesData] = useState({});
11
+ const [loading, setLoading] = useState(false);
12
+ const [isFiltering, setIsFiltering] = useState(false);
13
+ const [error, setError] = useState(null);
14
+ const [status, setStatus] = useState("idle");
15
+ const [sessionId, setSessionId] = useState(null);
16
+ const [dictionaries, setDictionaries] = useState(null);
17
+ const [currencySign, setCurrencySign] = useState(null);
18
+ const [total, setTotal] = useState(0);
19
+ const [limit, setLimit] = useState(25);
20
+ const [offset, setOffset] = useState(0);
21
+ const [activeFilters, setActiveFilters] = useState({});
22
+ const [isPageLoading, setIsPageLoading] = useState(false);
23
+ const prevTotalRef = useRef(0);
24
+ const pollingRef = useRef(false);
25
+ const startSearch = useCallback(async (params) => {
26
+ setLoading(true);
27
+ setIsFiltering(false);
28
+ setError(null);
29
+ setResults([]);
30
+ setFiltersData(null);
31
+ setAirlinesData({});
32
+ setStatus("pending");
33
+ setTotal(0);
34
+ prevTotalRef.current = 0;
35
+ pollingRef.current = true;
36
+ try {
37
+ console.log("[FlightSearch] TOKEN:", token ? `${token.substring(0, 8)}...` : "EMPTY!");
38
+ console.log("[FlightSearch] POST params:", JSON.stringify(params));
39
+ const initRes = await fetch(API_BASE, {
40
+ method: "POST",
41
+ headers: {
42
+ "Content-Type": "application/json",
43
+ Authorization: `Bearer ${token}`,
44
+ },
45
+ body: JSON.stringify(params),
46
+ });
47
+ console.log("[FlightSearch] Init response status:", initRes.status);
48
+ const initData = await initRes.json();
49
+ console.log("[FlightSearch] Init response:", JSON.stringify(initData).substring(0, 500));
50
+ const cacheId = initData.cacheId;
51
+ if (!cacheId) {
52
+ console.error("[FlightSearch] No cacheId! Full response:", JSON.stringify(initData));
53
+ setError(initData.message || "Не удалось начать поиск");
54
+ setStatus("failed");
55
+ return;
56
+ }
57
+ setSessionId(cacheId);
58
+ let attempt = 0;
59
+ while (pollingRef.current && attempt < MAX_POLL_ATTEMPTS) {
60
+ attempt++;
61
+ const res = await fetch(`${API_BASE}/${cacheId}?embedded=filters,airlines,dictionaries`, {
62
+ headers: {
63
+ Authorization: `Bearer ${token}`,
64
+ },
65
+ });
66
+ const data = await res.json();
67
+ console.log(`[FlightSearch] Poll #${attempt}: status=${data.status}, items=${data.items?.length || 0}, total=${data.total}`);
68
+ setStatus(data.status || "unknown");
69
+ if (Array.isArray(data.items))
70
+ setResults(data.items);
71
+ if (data.filters)
72
+ setFiltersData(data.filters);
73
+ if (data.airlines)
74
+ setAirlinesData(data.airlines);
75
+ if (data.dictionaries)
76
+ setDictionaries(data.dictionaries);
77
+ if (data.currencySign)
78
+ setCurrencySign(data.currencySign);
79
+ if (typeof data.total === "number")
80
+ setTotal(data.total);
81
+ if (typeof data.limit === "number")
82
+ setLimit(data.limit);
83
+ if (typeof data.offset === "number")
84
+ setOffset(data.offset);
85
+ if (data.status === "failed") {
86
+ setError("Ошибка поиска: сервер вернул статус failed");
87
+ setStatus("failed");
88
+ break;
89
+ }
90
+ if (["success_partially", "success"].includes(data.status))
91
+ break;
92
+ await new Promise((r) => setTimeout(r, POLL_INTERVAL));
93
+ }
94
+ if (attempt >= MAX_POLL_ATTEMPTS) {
95
+ setError(`Превышено количество запросов (${MAX_POLL_ATTEMPTS})`);
96
+ setStatus("timeout");
97
+ }
98
+ return cacheId;
99
+ }
100
+ catch (err) {
101
+ console.error("Ошибка поиска:", err);
102
+ setError(err.message || "Ошибка при поиске");
103
+ setStatus("failed");
104
+ }
105
+ finally {
106
+ setLoading(false);
107
+ pollingRef.current = false;
108
+ }
109
+ }, [token]);
110
+ const reset = () => {
111
+ pollingRef.current = false;
112
+ setResults([]);
113
+ setFiltersData(null);
114
+ setAirlinesData({});
115
+ setDictionaries(null);
116
+ setCurrencySign(null);
117
+ setLoading(false);
118
+ setIsFiltering(false);
119
+ setError(null);
120
+ setStatus("idle");
121
+ setSessionId(null);
122
+ setTotal(0);
123
+ setLimit(25);
124
+ setOffset(0);
125
+ setActiveFilters({});
126
+ setIsPageLoading(false);
127
+ };
128
+ const applyFilters = useCallback(async (sid, filters = {}, page = 1) => {
129
+ if (!sid)
130
+ return;
131
+ setIsFiltering(true);
132
+ setActiveFilters(filters);
133
+ try {
134
+ const newOffset = (page - 1) * limit;
135
+ const query = new URLSearchParams();
136
+ Object.entries(filters).forEach(([key, value]) => {
137
+ if (value !== undefined && value !== null && value !== "") {
138
+ if (Array.isArray(value)) {
139
+ value.forEach((v) => query.append(`filter[${key}][]`, String(v)));
140
+ }
141
+ else {
142
+ query.append(`filter[${key}]`, String(value));
143
+ }
144
+ }
145
+ });
146
+ query.append("limit", String(limit));
147
+ query.append("offset", String(newOffset));
148
+ const res = await fetch(`${API_BASE}/${sid}?${query.toString()}&embedded=filters,airlines,dictionaries`, {
149
+ headers: {
150
+ Authorization: `Bearer ${token}`,
151
+ },
152
+ });
153
+ const data = await res.json();
154
+ if (data.status === "failed") {
155
+ setError("Ошибка фильтрации: сервер вернул статус failed");
156
+ setStatus("failed");
157
+ return;
158
+ }
159
+ setResults(data.items || []);
160
+ if (data.filters)
161
+ setFiltersData(data.filters);
162
+ if (data.airlines)
163
+ setAirlinesData(data.airlines);
164
+ if (data.dictionaries)
165
+ setDictionaries(data.dictionaries);
166
+ if (data.currencySign)
167
+ setCurrencySign(data.currencySign);
168
+ if (typeof data.total === "number")
169
+ setTotal(data.total);
170
+ if (typeof data.limit === "number")
171
+ setLimit(data.limit);
172
+ if (typeof data.offset === "number")
173
+ setOffset(data.offset);
174
+ }
175
+ catch (err) {
176
+ console.error("Ошибка фильтрации:", err);
177
+ setError(err.message || "Ошибка при фильтрации");
178
+ }
179
+ finally {
180
+ setIsFiltering(false);
181
+ }
182
+ }, [limit, token]);
183
+ const fetchPage = useCallback(async (page) => {
184
+ if (!sessionId)
185
+ return;
186
+ const newOffset = (page - 1) * limit;
187
+ setIsPageLoading(true);
188
+ try {
189
+ const query = new URLSearchParams();
190
+ Object.entries(activeFilters).forEach(([key, value]) => {
191
+ if (value !== undefined && value !== null && value !== "") {
192
+ if (Array.isArray(value)) {
193
+ value.forEach((v) => query.append(`filter[${key}][]`, String(v)));
194
+ }
195
+ else {
196
+ query.append(`filter[${key}]`, String(value));
197
+ }
198
+ }
199
+ });
200
+ query.append("limit", String(limit));
201
+ query.append("offset", String(newOffset));
202
+ const res = await fetch(`${API_BASE}/${sessionId}?${query.toString()}&embedded=airlines`, { headers: { Authorization: `Bearer ${token}` } });
203
+ const data = await res.json();
204
+ if (Array.isArray(data.items))
205
+ setResults(data.items);
206
+ if (typeof data.total === "number")
207
+ setTotal(data.total);
208
+ if (typeof data.limit === "number")
209
+ setLimit(data.limit);
210
+ if (typeof data.offset === "number")
211
+ setOffset(data.offset);
212
+ if (data.airlines)
213
+ setAirlinesData(data.airlines);
214
+ }
215
+ catch (err) {
216
+ console.error("Ошибка пагинации:", err);
217
+ setError(err.message || "Ошибка при загрузке страницы");
218
+ }
219
+ finally {
220
+ setIsPageLoading(false);
221
+ }
222
+ }, [sessionId, limit, activeFilters, token]);
223
+ return {
224
+ results,
225
+ filtersData,
226
+ airlinesData,
227
+ dictionaries,
228
+ currencySign,
229
+ loading,
230
+ isFiltering,
231
+ error,
232
+ status,
233
+ sessionId,
234
+ total,
235
+ limit,
236
+ offset,
237
+ isPageLoading,
238
+ startSearch,
239
+ applyFilters,
240
+ fetchPage,
241
+ reset,
242
+ };
243
+ }
@@ -0,0 +1,7 @@
1
+ export declare const useHotelDetails: (searchParams?: Record<string, any>) => {
2
+ hotelData: any;
3
+ loading: boolean;
4
+ error: string | null;
5
+ fetchHotelDetails: (hotelId: number) => Promise<any>;
6
+ clearCache: () => Promise<void>;
7
+ };