|
|
|
|
@@ -11,7 +11,6 @@ import 'package:pshared/models/payment/type.dart';
|
|
|
|
|
import 'package:pshared/models/wallet/chain_asset.dart';
|
|
|
|
|
import 'package:pshared/provider/accounts/employees.dart';
|
|
|
|
|
import 'package:pshared/provider/ledger.dart';
|
|
|
|
|
import 'package:pshared/provider/organizations.dart';
|
|
|
|
|
import 'package:pshared/provider/payment/wallets.dart';
|
|
|
|
|
import 'package:pshared/utils/currency.dart';
|
|
|
|
|
|
|
|
|
|
@@ -42,7 +41,7 @@ class _AddBalanceDialogState extends State<AddBalanceDialog> {
|
|
|
|
|
final _descriptionController = TextEditingController();
|
|
|
|
|
|
|
|
|
|
PaymentType _assetType = PaymentType.managedWallet;
|
|
|
|
|
String _ownerRef = orgOwnerRef;
|
|
|
|
|
String? _ownerRef;
|
|
|
|
|
Currency _managedCurrency = managedCurrencyDefault;
|
|
|
|
|
ChainNetwork _network = managedNetworkDefault;
|
|
|
|
|
Currency _ledgerCurrency = ledgerCurrencyDefault;
|
|
|
|
|
@@ -59,10 +58,7 @@ class _AddBalanceDialogState extends State<AddBalanceDialog> {
|
|
|
|
|
setState(() => _assetType = value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void _setOwnerRef(String? value) {
|
|
|
|
|
if (value == null) return;
|
|
|
|
|
setState(() => _ownerRef = value);
|
|
|
|
|
}
|
|
|
|
|
void _setOwnerRef(String? value) => setState(() => _ownerRef = value);
|
|
|
|
|
|
|
|
|
|
void _setManagedCurrency(Currency? value) {
|
|
|
|
|
if (value == null) return;
|
|
|
|
|
@@ -87,13 +83,17 @@ class _AddBalanceDialogState extends State<AddBalanceDialog> {
|
|
|
|
|
final name = _nameController.text.trim();
|
|
|
|
|
final description = _descriptionController.text.trim();
|
|
|
|
|
final employees = context.read<EmployeesProvider>().employees;
|
|
|
|
|
final effectiveOwnerRef = employees.any((employee) => employee.id == _ownerRef) ? _ownerRef : orgOwnerRef;
|
|
|
|
|
final isOrgWallet = effectiveOwnerRef == orgOwnerRef;
|
|
|
|
|
final owner = isOrgWallet ? null : employees.firstWhereOrNull((employee) => employee.id == effectiveOwnerRef);
|
|
|
|
|
final effectiveOwnerRef = employees.any((employee) => employee.id == _ownerRef)
|
|
|
|
|
? _ownerRef
|
|
|
|
|
: null;
|
|
|
|
|
final isOrgWallet = effectiveOwnerRef == null;
|
|
|
|
|
final owner = isOrgWallet
|
|
|
|
|
? null
|
|
|
|
|
: employees.firstWhereOrNull((employee) => employee.id == effectiveOwnerRef);
|
|
|
|
|
|
|
|
|
|
final errorMessage = _assetType == PaymentType.managedWallet
|
|
|
|
|
? l10n.errorCreateManagedWallet
|
|
|
|
|
: l10n.errorCreateLedgerAccount;
|
|
|
|
|
? l10n.errorCreateManagedWallet
|
|
|
|
|
: l10n.errorCreateLedgerAccount;
|
|
|
|
|
|
|
|
|
|
final result = await executeActionWithNotification<bool>(
|
|
|
|
|
context: context,
|
|
|
|
|
@@ -124,18 +124,18 @@ class _AddBalanceDialogState extends State<AddBalanceDialog> {
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
final l10n = AppLocalizations.of(context)!;
|
|
|
|
|
final orgName = context.select<OrganizationsProvider, String?>(
|
|
|
|
|
(provider) => provider.isOrganizationSet ? provider.current.name : null,
|
|
|
|
|
);
|
|
|
|
|
final screenWidth = MediaQuery.of(context).size.width;
|
|
|
|
|
final maxWidth = (screenWidth - 48).clamp(0.0, double.infinity);
|
|
|
|
|
final dialogWidth = maxWidth >= 360.0 ? (maxWidth > 520.0 ? 520.0 : maxWidth) : maxWidth;
|
|
|
|
|
final employeesProvider = context.watch<EmployeesProvider>();
|
|
|
|
|
final employees = employeesProvider.employees;
|
|
|
|
|
final isSaving = context.watch<WalletsProvider>().isLoading ||
|
|
|
|
|
context.watch<LedgerAccountsProvider>().isLoading;
|
|
|
|
|
|
|
|
|
|
final ownerItems = <DropdownMenuItem<String>>[
|
|
|
|
|
final ownerItems = <DropdownMenuItem<String?>>[
|
|
|
|
|
DropdownMenuItem(
|
|
|
|
|
value: orgOwnerRef,
|
|
|
|
|
child: Text(orgName ?? l10n.companyName),
|
|
|
|
|
value: null,
|
|
|
|
|
child: Text(l10n.organizationOwned),
|
|
|
|
|
),
|
|
|
|
|
...employees.map((employee) => DropdownMenuItem(
|
|
|
|
|
value: employee.id,
|
|
|
|
|
@@ -143,29 +143,30 @@ class _AddBalanceDialogState extends State<AddBalanceDialog> {
|
|
|
|
|
)),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
final resolvedOwnerRef = ownerItems.any((item) => item.value == _ownerRef)
|
|
|
|
|
? _ownerRef
|
|
|
|
|
: orgOwnerRef;
|
|
|
|
|
final resolvedOwnerRef = ownerItems.any((item) => item.value == _ownerRef) ? _ownerRef : null;
|
|
|
|
|
|
|
|
|
|
return AlertDialog(
|
|
|
|
|
title: Text(l10n.actionAddNew),
|
|
|
|
|
content: AddBalanceForm(
|
|
|
|
|
formKey: _formKey,
|
|
|
|
|
assetType: _assetType,
|
|
|
|
|
isSaving: isSaving,
|
|
|
|
|
ownerItems: ownerItems,
|
|
|
|
|
ownerValue: resolvedOwnerRef,
|
|
|
|
|
onAssetTypeChanged: _setAssetType,
|
|
|
|
|
onOwnerChanged: _setOwnerRef,
|
|
|
|
|
nameController: _nameController,
|
|
|
|
|
descriptionController: _descriptionController,
|
|
|
|
|
managedCurrency: _managedCurrency,
|
|
|
|
|
network: _network,
|
|
|
|
|
ledgerCurrency: _ledgerCurrency,
|
|
|
|
|
onManagedCurrencyChanged: _setManagedCurrency,
|
|
|
|
|
onNetworkChanged: _setNetwork,
|
|
|
|
|
onLedgerCurrencyChanged: _setLedgerCurrency,
|
|
|
|
|
showEmployeesLoading: employeesProvider.isLoading,
|
|
|
|
|
content: SizedBox(
|
|
|
|
|
width: dialogWidth,
|
|
|
|
|
child: AddBalanceForm(
|
|
|
|
|
formKey: _formKey,
|
|
|
|
|
assetType: _assetType,
|
|
|
|
|
isSaving: isSaving,
|
|
|
|
|
ownerItems: ownerItems,
|
|
|
|
|
ownerValue: resolvedOwnerRef,
|
|
|
|
|
onAssetTypeChanged: _setAssetType,
|
|
|
|
|
onOwnerChanged: _setOwnerRef,
|
|
|
|
|
nameController: _nameController,
|
|
|
|
|
descriptionController: _descriptionController,
|
|
|
|
|
managedCurrency: _managedCurrency,
|
|
|
|
|
network: _network,
|
|
|
|
|
ledgerCurrency: _ledgerCurrency,
|
|
|
|
|
onManagedCurrencyChanged: _setManagedCurrency,
|
|
|
|
|
onNetworkChanged: _setNetwork,
|
|
|
|
|
onLedgerCurrencyChanged: _setLedgerCurrency,
|
|
|
|
|
showEmployeesLoading: employeesProvider.isLoading,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
actions: [
|
|
|
|
|
DialogCancelButton(
|
|
|
|
|
|