55 lines
1.8 KiB
Dart
55 lines
1.8 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import 'package:pshared/api/requests/payment/quote.dart';
|
|
import 'package:pshared/data/mapper/payment/intent/payment.dart';
|
|
import 'package:pshared/models/payment/intent.dart';
|
|
import 'package:pshared/models/payment/quote.dart';
|
|
import 'package:pshared/provider/organizations.dart';
|
|
import 'package:pshared/provider/resource.dart';
|
|
import 'package:pshared/service/payment/quotation.dart';
|
|
import 'package:uuid/uuid.dart';
|
|
|
|
|
|
class QuotationProvider extends ChangeNotifier {
|
|
Resource<PaymentQuote> _quotation = Resource(data: null, isLoading: false, error: null);
|
|
late OrganizationsProvider _organizations;
|
|
bool _isLoaded = false;
|
|
|
|
void update(OrganizationsProvider venue) {
|
|
_organizations = venue;
|
|
}
|
|
|
|
PaymentQuote? get quotation => _quotation.data;
|
|
|
|
bool get isReady => _isLoaded && !_quotation.isLoading && _quotation.error == null;
|
|
|
|
Future<PaymentQuote?> getQuotation(PaymentIntent intent) async {
|
|
if (!_organizations.isOrganizationSet) throw StateError('Organization is not set');
|
|
try {
|
|
_quotation = _quotation.copyWith(isLoading: true, error: null);
|
|
final response = await QuotationService.getQuotation(
|
|
_organizations.current.id,
|
|
QuotePaymentRequest(
|
|
idempotencyKey: Uuid().v4(),
|
|
intent: intent.toDTO(),
|
|
),
|
|
);
|
|
_isLoaded = true;
|
|
_quotation = _quotation.copyWith(data: response, isLoading: false);
|
|
} catch (e) {
|
|
_quotation = _quotation.copyWith(
|
|
error: e is Exception ? e : Exception(e.toString()),
|
|
isLoading: false,
|
|
);
|
|
}
|
|
notifyListeners();
|
|
return _quotation.data;
|
|
}
|
|
|
|
void reset() {
|
|
_quotation = Resource(data: null, isLoading: false, error: null);
|
|
_isLoaded = false;
|
|
notifyListeners();
|
|
}
|
|
}
|