| 1 |
import { |
| 2 |
useQuery, |
| 3 |
UseQueryOptions, |
| 4 |
useMutation, |
| 5 |
useQueryClient, |
| 6 |
} from "@tanstack/react-query"; |
| 7 |
import { apiClient } from "../lib/api-client"; |
| 8 |
import { __ } from "../lib/i18n"; |
| 9 |
import { useToast } from "../components/ui/toast"; |
| 10 |
|
| 11 |
export interface ModuleDefinition { |
| 12 |
slug: string; |
| 13 |
name: string; |
| 14 |
description?: string; |
| 15 |
category?: string; |
| 16 |
version?: string; |
| 17 |
docs_url?: string; |
| 18 |
is_core?: boolean; |
| 19 |
is_premium?: boolean; |
| 20 |
is_available?: boolean; |
| 21 |
requires_pro?: boolean; |
| 22 |
purchase_url?: string; |
| 23 |
video_url?: string; |
| 24 |
enabled: boolean; |
| 25 |
tags?: string[]; |
| 26 |
updated_at?: string | null; |
| 27 |
settings_page?: string; |
| 28 |
} |
| 29 |
|
| 30 |
interface ModulesResponse { |
| 31 |
data?: ModuleDefinition[]; |
| 32 |
} |
| 33 |
|
| 34 |
const fetchModules = async (): Promise<ModuleDefinition[]> => { |
| 35 |
const response: ModulesResponse = await apiClient.get("/modules"); |
| 36 |
if (Array.isArray(response)) { |
| 37 |
return response; |
| 38 |
} |
| 39 |
if (Array.isArray(response?.data)) { |
| 40 |
return response.data; |
| 41 |
} |
| 42 |
return []; |
| 43 |
}; |
| 44 |
|
| 45 |
export const useModulesQuery = ( |
| 46 |
options?: Partial<UseQueryOptions<ModuleDefinition[], Error>>, |
| 47 |
) => { |
| 48 |
return useQuery<ModuleDefinition[], Error>({ |
| 49 |
queryKey: ["modules"], |
| 50 |
queryFn: fetchModules, |
| 51 |
staleTime: 0, |
| 52 |
...options, |
| 53 |
}); |
| 54 |
}; |
| 55 |
|
| 56 |
interface TogglePayload { |
| 57 |
slug: string; |
| 58 |
enabled: boolean; |
| 59 |
name?: string; |
| 60 |
} |
| 61 |
|
| 62 |
const formatNamesList = (names: string[]) => { |
| 63 |
if (names.length <= 2) { |
| 64 |
return names.join(", "); |
| 65 |
} |
| 66 |
return `${names.slice(0, 2).join(", ")} ${__("and", "yatra")} ${names.length - 2} ${__("more", "yatra")}`; |
| 67 |
}; |
| 68 |
|
| 69 |
export const useToggleModule = () => { |
| 70 |
const queryClient = useQueryClient(); |
| 71 |
const { showToast } = useToast(); |
| 72 |
|
| 73 |
return useMutation({ |
| 74 |
mutationFn: async ({ slug, enabled }: TogglePayload) => { |
| 75 |
const response: ModulesResponse = await apiClient.post( |
| 76 |
`/modules/${slug}/toggle`, |
| 77 |
{ enabled }, |
| 78 |
); |
| 79 |
return Array.isArray(response?.data) ? response.data : []; |
| 80 |
}, |
| 81 |
onSuccess: (data, variables) => { |
| 82 |
queryClient.setQueryData(["modules"], data); |
| 83 |
|
| 84 |
// Update global admin variables for navigation |
| 85 |
if (window.yatraAdmin) { |
| 86 |
// Update module-specific flags based on the enabled modules |
| 87 |
const enabledModules = data.filter((m) => m.enabled); |
| 88 |
|
| 89 |
// Check for specific modules that affect navigation |
| 90 |
window.yatraAdmin.emailAutomationEnabled = enabledModules.some( |
| 91 |
(m) => m.slug === "email_automation" || m.slug === "email-automation", |
| 92 |
); |
| 93 |
window.yatraAdmin.tripConsentEnabled = enabledModules.some( |
| 94 |
(m) => m.slug === "trip_consent" || m.slug === "trip-consent", |
| 95 |
); |
| 96 |
window.yatraAdmin.additionalServicesEnabled = enabledModules.some( |
| 97 |
(m) => |
| 98 |
m.slug === "additional_services" || |
| 99 |
m.slug === "additional-services", |
| 100 |
); |
| 101 |
window.yatraAdmin.abandonedBookingRecoveryEnabled = enabledModules.some( |
| 102 |
(m) => |
| 103 |
m.slug === "abandoned_booking_recovery" || |
| 104 |
m.slug === "abandoned-booking-recovery", |
| 105 |
); |
| 106 |
window.yatraAdmin.dynamicPricingEnabled = enabledModules.some( |
| 107 |
(m) => m.slug === "dynamic_pricing" || m.slug === "dynamic-pricing", |
| 108 |
); |
| 109 |
window.yatraAdmin.flexiblePaymentsEnabled = enabledModules.some( |
| 110 |
(m) => m.slug === "flexible_payments", |
| 111 |
); |
| 112 |
window.yatraAdmin.dynamicFormFieldEnabled = enabledModules.some( |
| 113 |
(m) => m.slug === "dynamic_form_field", |
| 114 |
); |
| 115 |
window.yatraAdmin.showGoogleCalendarSettingsUI = enabledModules.some( |
| 116 |
(m) => m.slug === "google_calendar" || m.slug === "google-calendar", |
| 117 |
); |
| 118 |
window.yatraAdmin.advancedDiscountEnabled = enabledModules.some( |
| 119 |
(m) => |
| 120 |
m.slug === "advanced_discount" || m.slug === "advanced-discount", |
| 121 |
); |
| 122 |
// Add flags for Pro feature modules |
| 123 |
window.yatraAdmin.showMailchimpSettingsUI = enabledModules.some( |
| 124 |
(m) => m.slug === "mailchimp", |
| 125 |
); |
| 126 |
window.yatraAdmin.showFacebookPixelSettingsUI = enabledModules.some( |
| 127 |
(m) => m.slug === "facebook_pixel", |
| 128 |
); |
| 129 |
window.yatraAdmin.showGoogleAnalyticsSettingsUI = enabledModules.some( |
| 130 |
(m) => m.slug === "google_analytics", |
| 131 |
); |
| 132 |
// Note: availabilityModuleEnabled and departuresModuleEnabled removed - now FREE features |
| 133 |
|
| 134 |
// Trigger a navigation refresh by updating a custom event |
| 135 |
window.dispatchEvent( |
| 136 |
new CustomEvent("yatra-modules-updated", { |
| 137 |
detail: { |
| 138 |
enabledModules: enabledModules, |
| 139 |
updatedModule: variables, |
| 140 |
}, |
| 141 |
}), |
| 142 |
); |
| 143 |
|
| 144 |
// Force a layout re-render by updating the URL key |
| 145 |
const urlKey = (window as any).__yatraUrlKey || 0; |
| 146 |
(window as any).__yatraUrlKey = urlKey + 1; |
| 147 |
window.dispatchEvent(new CustomEvent("yatra-force-nav-refresh")); |
| 148 |
} |
| 149 |
|
| 150 |
const label = variables.name || variables.slug; |
| 151 |
showToast( |
| 152 |
variables.enabled |
| 153 |
? __("{module} enabled successfully.", "yatra").replace( |
| 154 |
"{module}", |
| 155 |
label, |
| 156 |
) |
| 157 |
: __("{module} disabled successfully.", "yatra").replace( |
| 158 |
"{module}", |
| 159 |
label, |
| 160 |
), |
| 161 |
"success", |
| 162 |
); |
| 163 |
}, |
| 164 |
onError: (error: Error) => { |
| 165 |
showToast( |
| 166 |
error.message || __("Failed to update module.", "yatra"), |
| 167 |
"error", |
| 168 |
); |
| 169 |
}, |
| 170 |
}); |
| 171 |
}; |
| 172 |
|
| 173 |
export const useBulkToggleModules = () => { |
| 174 |
const queryClient = useQueryClient(); |
| 175 |
const { showToast } = useToast(); |
| 176 |
|
| 177 |
return useMutation({ |
| 178 |
mutationFn: async (items: TogglePayload[]) => { |
| 179 |
const response: ModulesResponse = await apiClient.post( |
| 180 |
"/modules/bulk-toggle", |
| 181 |
{ items }, |
| 182 |
); |
| 183 |
return Array.isArray(response?.data) ? response.data : []; |
| 184 |
}, |
| 185 |
onSuccess: (data, variables = []) => { |
| 186 |
queryClient.setQueryData(["modules"], data); |
| 187 |
|
| 188 |
// Update global admin variables for navigation |
| 189 |
if (window.yatraAdmin && data) { |
| 190 |
// Update module-specific flags based on the enabled modules |
| 191 |
const enabledModules = data.filter((m) => m.enabled); |
| 192 |
|
| 193 |
// Check for specific modules that affect navigation |
| 194 |
window.yatraAdmin.emailAutomationEnabled = enabledModules.some( |
| 195 |
(m) => m.slug === "email_automation" || m.slug === "email-automation", |
| 196 |
); |
| 197 |
window.yatraAdmin.tripConsentEnabled = enabledModules.some( |
| 198 |
(m) => m.slug === "trip_consent" || m.slug === "trip-consent", |
| 199 |
); |
| 200 |
window.yatraAdmin.additionalServicesEnabled = enabledModules.some( |
| 201 |
(m) => |
| 202 |
m.slug === "additional_services" || |
| 203 |
m.slug === "additional-services", |
| 204 |
); |
| 205 |
window.yatraAdmin.abandonedBookingRecoveryEnabled = enabledModules.some( |
| 206 |
(m) => |
| 207 |
m.slug === "abandoned_booking_recovery" || |
| 208 |
m.slug === "abandoned-booking-recovery", |
| 209 |
); |
| 210 |
window.yatraAdmin.dynamicPricingEnabled = enabledModules.some( |
| 211 |
(m) => m.slug === "dynamic_pricing" || m.slug === "dynamic-pricing", |
| 212 |
); |
| 213 |
window.yatraAdmin.flexiblePaymentsEnabled = enabledModules.some( |
| 214 |
(m) => m.slug === "flexible_payments", |
| 215 |
); |
| 216 |
window.yatraAdmin.dynamicFormFieldEnabled = enabledModules.some( |
| 217 |
(m) => m.slug === "dynamic_form_field", |
| 218 |
); |
| 219 |
window.yatraAdmin.showGoogleCalendarSettingsUI = enabledModules.some( |
| 220 |
(m) => m.slug === "google_calendar" || m.slug === "google-calendar", |
| 221 |
); |
| 222 |
window.yatraAdmin.advancedDiscountEnabled = enabledModules.some( |
| 223 |
(m) => |
| 224 |
m.slug === "advanced_discount" || m.slug === "advanced-discount", |
| 225 |
); |
| 226 |
// Add flags for Pro feature modules |
| 227 |
window.yatraAdmin.showMailchimpSettingsUI = enabledModules.some( |
| 228 |
(m) => m.slug === "mailchimp", |
| 229 |
); |
| 230 |
window.yatraAdmin.showFacebookPixelSettingsUI = enabledModules.some( |
| 231 |
(m) => m.slug === "facebook_pixel", |
| 232 |
); |
| 233 |
window.yatraAdmin.showGoogleAnalyticsSettingsUI = enabledModules.some( |
| 234 |
(m) => m.slug === "google_analytics", |
| 235 |
); |
| 236 |
// Note: availabilityModuleEnabled and departuresModuleEnabled removed - now FREE features |
| 237 |
|
| 238 |
// Trigger a navigation refresh by updating a custom event |
| 239 |
window.dispatchEvent( |
| 240 |
new CustomEvent("yatra-modules-updated", { |
| 241 |
detail: { |
| 242 |
enabledModules: enabledModules, |
| 243 |
updatedModules: variables || [], |
| 244 |
}, |
| 245 |
}), |
| 246 |
); |
| 247 |
|
| 248 |
// Force a layout re-render by updating the URL key |
| 249 |
const urlKey = (window as any).__yatraUrlKey || 0; |
| 250 |
(window as any).__yatraUrlKey = urlKey + 1; |
| 251 |
window.dispatchEvent(new CustomEvent("yatra-force-nav-refresh")); |
| 252 |
} |
| 253 |
|
| 254 |
if (variables.length > 0) { |
| 255 |
const names = variables.map((item) => item.name || item.slug); |
| 256 |
const summary = formatNamesList(names); |
| 257 |
const message = variables[0].enabled |
| 258 |
? __("Enabled: {modules}", "yatra").replace("{modules}", summary) |
| 259 |
: __("Disabled: {modules}", "yatra").replace("{modules}", summary); |
| 260 |
showToast(message, "success"); |
| 261 |
} else { |
| 262 |
showToast(__("Modules updated successfully.", "yatra"), "success"); |
| 263 |
} |
| 264 |
}, |
| 265 |
onError: (error: Error) => { |
| 266 |
showToast( |
| 267 |
error.message || __("Failed to update modules.", "yatra"), |
| 268 |
"error", |
| 269 |
); |
| 270 |
}, |
| 271 |
}); |
| 272 |
}; |
| 273 |
|