(checked: boolean)
| 336 | }, []) |
| 337 | |
| 338 | const handleToggleAutoTopup = (checked: boolean) => { |
| 339 | if (checked && userProfile?.auto_topup_blocked_reason) { |
| 340 | toast({ |
| 341 | title: 'Cannot Enable Auto Top-up', |
| 342 | description: userProfile.auto_topup_blocked_reason, |
| 343 | variant: 'destructive', |
| 344 | }) |
| 345 | return |
| 346 | } |
| 347 | |
| 348 | debouncedSaveSettings.cancel() |
| 349 | pendingSettings.current = null |
| 350 | |
| 351 | if (checked) { |
| 352 | if ( |
| 353 | threshold < MIN_THRESHOLD_CREDITS || |
| 354 | threshold > MAX_THRESHOLD_CREDITS || |
| 355 | topUpAmountDollars < MIN_TOPUP_DOLLARS || |
| 356 | topUpAmountDollars > MAX_TOPUP_DOLLARS |
| 357 | ) { |
| 358 | toast({ |
| 359 | title: 'Invalid Settings', |
| 360 | description: |
| 361 | 'Cannot enable auto top-up with current values. Please ensure they are within limits.', |
| 362 | variant: 'destructive', |
| 363 | }) |
| 364 | return |
| 365 | } |
| 366 | |
| 367 | setIsCheckingBalance(true) |
| 368 | fetchCurrentBalance() |
| 369 | .then((balance) => { |
| 370 | if (balance < threshold) { |
| 371 | setConfirmDialogBalance(balance) |
| 372 | setShowConfirmDialog(true) |
| 373 | } else { |
| 374 | enableAutoTopup() |
| 375 | } |
| 376 | }) |
| 377 | .catch(() => { |
| 378 | enableAutoTopup() |
| 379 | }) |
| 380 | .finally(() => { |
| 381 | setIsCheckingBalance(false) |
| 382 | }) |
| 383 | } else { |
| 384 | setIsEnabled(false) |
| 385 | autoTopupMutation.mutate( |
| 386 | { |
| 387 | auto_topup_enabled: false, |
| 388 | auto_topup_threshold: null, |
| 389 | auto_topup_amount: null, |
| 390 | }, |
| 391 | { |
| 392 | onSuccess: () => { |
| 393 | toast({ title: 'Auto Top-up disabled.' }) |
| 394 | }, |
| 395 | onError: () => { |
no test coverage detected