import 'package:flutter/material.dart'; import 'package:go_router/go_router.dart'; import 'package:provider/provider.dart'; import 'package:pshared/models/payment/type.dart'; import 'package:pshared/models/recipient/recipient.dart'; import 'package:pshared/provider/recipient/provider.dart'; import 'package:pweb/app/router/pages.dart'; import 'package:pweb/app/router/payout_routes.dart'; import 'package:pweb/models/wallet.dart'; import 'package:pweb/pages/address_book/form/page.dart'; import 'package:pweb/pages/address_book/page/page.dart'; import 'package:pweb/pages/dashboard/dashboard.dart'; import 'package:pweb/pages/payment_methods/page.dart'; import 'package:pweb/pages/payout_page/page.dart'; import 'package:pweb/pages/payout_page/wallet/edit/page.dart'; import 'package:pweb/pages/report/page.dart'; import 'package:pweb/pages/settings/profile/page.dart'; import 'package:pweb/pages/wallet_top_up/page.dart'; import 'package:pweb/providers/wallets.dart'; import 'package:pweb/widgets/error/snackbar.dart'; import 'package:pweb/widgets/sidebar/destinations.dart'; import 'package:pweb/widgets/sidebar/page.dart'; import 'package:pweb/generated/i18n/app_localizations.dart'; RouteBase payoutShellRoute() => ShellRoute( builder: (context, state, child) => PageSelector( child: child, routerState: state, ), routes: [ GoRoute( name: PayoutRoutes.dashboard, path: routerPage(Pages.dashboard), pageBuilder: (context, _) => NoTransitionPage( child: DashboardPage( onRecipientSelected: (recipient) => _startPayment( context, recipient: recipient, returnTo: PayoutDestination.dashboard, ), onGoToPaymentWithoutRecipient: (type) => _startPayment( context, recipient: null, paymentType: type, returnTo: PayoutDestination.dashboard, ), onTopUp: (wallet) => _openWalletTopUp( context, wallet, returnTo: PayoutDestination.dashboard, ), ), ), ), GoRoute( name: PayoutRoutes.recipients, path: PayoutRoutes.recipientsPath, pageBuilder: (context, _) { final loc = AppLocalizations.of(context)!; return NoTransitionPage( child: RecipientAddressBookPage( onRecipientSelected: (recipient) => _startPayment( context, recipient: recipient, returnTo: PayoutDestination.recipients, ), onAddRecipient: () => _openAddRecipient(context), onEditRecipient: (recipient) => _openAddRecipient( context, recipient: recipient, ), onDeleteRecipient: (recipient) => executeActionWithNotification( context: context, action: () async => context.read().delete(recipient.id), successMessage: loc.recipientDeletedSuccessfully, errorMessage: loc.errorDeleteRecipient, ), ), ); }, ), GoRoute( name: PayoutRoutes.addRecipient, path: PayoutRoutes.addRecipientPath, pageBuilder: (context, _) { final recipient = context.read().currentObject; return NoTransitionPage( child: AddressBookRecipientForm( recipient: recipient, onSaved: (_) => context.goToPayout(PayoutDestination.recipients), ), ); }, ), GoRoute( name: PayoutRoutes.payment, path: PayoutRoutes.paymentPath, pageBuilder: (context, state) { final fallbackDestination = PayoutRoutes.fallbackFromState( state, defaultDestination: PayoutDestination.dashboard, ); return NoTransitionPage( child: PaymentPage( onBack: (_) => _popOrGo(context, fallbackDestination), initialPaymentType: PayoutRoutes.paymentTypeFromState(state), fallbackDestination: fallbackDestination, ), ); }, ), GoRoute( name: PayoutRoutes.settings, path: PayoutRoutes.settingsPath, pageBuilder: (_, __) => const NoTransitionPage( child: ProfileSettingsPage(), ), ), GoRoute( name: PayoutRoutes.reports, path: PayoutRoutes.reportsPath, pageBuilder: (_, __) => const NoTransitionPage( child: OperationHistoryPage(), ), ), GoRoute( name: PayoutRoutes.methods, path: PayoutRoutes.methodsPath, pageBuilder: (context, _) => NoTransitionPage( child: PaymentConfigPage( onWalletTap: (wallet) => _openWalletEdit( context, wallet, returnTo: PayoutDestination.methods, ), ), ), ), GoRoute( name: PayoutRoutes.editWallet, path: PayoutRoutes.editWalletPath, pageBuilder: (context, state) { final walletsProvider = context.read(); final wallet = walletsProvider.selectedWallet; final loc = AppLocalizations.of(context)!; final fallbackDestination = PayoutRoutes.fallbackFromState( state, defaultDestination: PayoutDestination.methods, ); return NoTransitionPage( child: wallet != null ? WalletEditPage( onBack: () => _popOrGo(context, fallbackDestination), ) : Center(child: Text(loc.noWalletSelected)), ); }, ), GoRoute( name: PayoutRoutes.walletTopUp, path: PayoutRoutes.walletTopUpPath, pageBuilder: (context, state) { final fallbackDestination = PayoutRoutes.fallbackFromState( state, defaultDestination: PayoutDestination.dashboard, ); return NoTransitionPage( child: WalletTopUpPage( onBack: () => _popOrGo(context, fallbackDestination), ), ); }, ), ], ); void _startPayment( BuildContext context, { Recipient? recipient, PaymentType? paymentType, required PayoutDestination returnTo, }) { context.read().setCurrentObject(recipient?.id); context.pushToPayment( paymentType: paymentType, returnTo: returnTo, ); } void _openAddRecipient( BuildContext context, { Recipient? recipient, }) { context.read().setCurrentObject(recipient?.id); context.pushNamed(PayoutRoutes.addRecipient); } void _openWalletEdit( BuildContext context, Wallet wallet, { required PayoutDestination returnTo, }) { context.read().selectWallet(wallet); context.pushToEditWallet(returnTo: returnTo); } void _openWalletTopUp( BuildContext context, Wallet wallet, { required PayoutDestination returnTo, }) { context.read().selectWallet(wallet); context.pushToWalletTopUp(returnTo: returnTo); } void _popOrGo(BuildContext context, PayoutDestination destination) { if (Navigator.of(context).canPop()) { Navigator.of(context).pop(); } else { context.goToPayout(destination); } }