40 lines
1.1 KiB
Dart
40 lines
1.1 KiB
Dart
import 'dart:async';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import '../../../core/network/dio_client.dart';
|
|
import '../domain/business_profile.dart';
|
|
|
|
class BusinessProfileNotifier extends AsyncNotifier<BusinessProfile?> {
|
|
@override
|
|
FutureOr<BusinessProfile?> build() async {
|
|
return _fetchProfile();
|
|
}
|
|
|
|
Future<BusinessProfile?> _fetchProfile() async {
|
|
final response = await DioClient().dio.get('/business/profile');
|
|
if (response.statusCode == 200) {
|
|
return BusinessProfile.fromJson(response.data);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Future<void> updateProfile(BusinessProfile profile) async {
|
|
state = const AsyncValue.loading();
|
|
try {
|
|
final response = await DioClient().dio.post(
|
|
'/business/profile',
|
|
data: profile.toJson(),
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
state = AsyncValue.data(BusinessProfile.fromJson(response.data));
|
|
}
|
|
} catch (e, stack) {
|
|
state = AsyncValue.error(e, stack);
|
|
}
|
|
}
|
|
}
|
|
|
|
final businessProfileProvider = AsyncNotifierProvider<BusinessProfileNotifier, BusinessProfile?>(() {
|
|
return BusinessProfileNotifier();
|
|
});
|