✨ Site basis
This commit is contained in:
@@ -6,7 +6,7 @@ part of 'messages_notifier.dart';
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$messagesNotifierHash() => r'fc9c99024a0801efa4894f250aea8bdc6127a0b6';
|
||||
String _$messagesNotifierHash() => r'27e5d686d9204ba39adbd1838cf4a6eaea0ac85f';
|
||||
|
||||
/// Copied from Dart SDK
|
||||
class _SystemHash {
|
||||
|
||||
@@ -6,7 +6,7 @@ part of 'file_references.dart';
|
||||
// RiverpodGenerator
|
||||
// **************************************************************************
|
||||
|
||||
String _$fileReferencesHash() => r'464562fbdc9452d8a5ffbd2d9d9343cdb43f1876';
|
||||
String _$fileReferencesHash() => r'd66c678c221f61978bdb242b98e6dbe31d0c204b';
|
||||
|
||||
/// Copied from Dart SDK
|
||||
class _SystemHash {
|
||||
|
||||
88
lib/pods/sites.dart
Normal file
88
lib/pods/sites.dart
Normal file
@@ -0,0 +1,88 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:island/models/publication_site.dart';
|
||||
import 'package:island/pods/network.dart';
|
||||
|
||||
class SiteNotifier
|
||||
extends
|
||||
AutoDisposeFamilyAsyncNotifier<
|
||||
SnPublicationSite,
|
||||
({String pubName, String? siteId})
|
||||
> {
|
||||
@override
|
||||
FutureOr<SnPublicationSite> build(
|
||||
({String pubName, String? siteId}) arg,
|
||||
) async {
|
||||
if (arg.siteId == null || arg.siteId!.isEmpty) {
|
||||
return SnPublicationSite(
|
||||
id: '',
|
||||
slug: '',
|
||||
name: '',
|
||||
publisherId: arg.pubName,
|
||||
accountId: '',
|
||||
createdAt: DateTime.now(),
|
||||
updatedAt: DateTime.now(),
|
||||
pages: [],
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
final client = ref.read(apiClientProvider);
|
||||
final response = await client.get('/sphere/sites/${arg.siteId}');
|
||||
return SnPublicationSite.fromJson(response.data);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> saveSite(SnPublicationSite site) async {
|
||||
state = const AsyncValue.loading();
|
||||
try {
|
||||
final client = ref.read(apiClientProvider);
|
||||
final url = '/sphere/sites';
|
||||
|
||||
final response =
|
||||
site.id.isEmpty
|
||||
? await client.post(url, data: site.toJson())
|
||||
: await client.patch('${url}/${site.id}', data: site.toJson());
|
||||
|
||||
state = AsyncValue.data(SnPublicationSite.fromJson(response.data));
|
||||
} catch (error, stackTrace) {
|
||||
state = AsyncValue.error(error, stackTrace);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> deleteSite() async {
|
||||
final siteId = arg.siteId;
|
||||
if (siteId == null || siteId.isEmpty) return;
|
||||
|
||||
state = const AsyncValue.loading();
|
||||
try {
|
||||
final client = ref.read(apiClientProvider);
|
||||
await client.delete('/sphere/sites/$siteId');
|
||||
state = AsyncValue.data(
|
||||
SnPublicationSite(
|
||||
id: '',
|
||||
slug: '',
|
||||
name: '',
|
||||
publisherId: arg.pubName,
|
||||
accountId: '',
|
||||
createdAt: DateTime.now(),
|
||||
updatedAt: DateTime.now(),
|
||||
pages: [],
|
||||
),
|
||||
);
|
||||
} catch (error, stackTrace) {
|
||||
state = AsyncValue.error(error, stackTrace);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
final siteNotifierProvider = AsyncNotifierProvider.autoDispose.family<
|
||||
SiteNotifier,
|
||||
SnPublicationSite,
|
||||
({String pubName, String? siteId})
|
||||
>(SiteNotifier.new);
|
||||
Reference in New Issue
Block a user