feat: enhance admin and profile repositories with token handling; update dropdown initial values in various screens
This commit is contained in:
@@ -2,6 +2,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../../../core/api/api_client.dart';
|
||||
import '../../../core/api/api_paths.dart';
|
||||
import '../../../core/api/guarded_api_call.dart';
|
||||
import '../../auth/data/auth_providers.dart';
|
||||
import '../domain/user_admin.dart';
|
||||
|
||||
final adminRepositoryProvider = Provider<AdminRepository>((ref) {
|
||||
@@ -14,26 +15,31 @@ class AdminRepository {
|
||||
|
||||
AdminRepository(this._apiClient, this._ref);
|
||||
|
||||
Future<String?> _token() => _ref.read(authStateProvider.future);
|
||||
|
||||
Future<List<UserAdmin>> listUsers() async {
|
||||
final token = await _token();
|
||||
final data = await guardedApiCall(
|
||||
_ref,
|
||||
() => _apiClient.getJson(UserApiPaths.list),
|
||||
() => _apiClient.getJson(UserApiPaths.list, token: token),
|
||||
);
|
||||
return (data as List<dynamic>).map((e) => UserAdmin.fromJson(e as Map<String, dynamic>)).toList();
|
||||
}
|
||||
|
||||
Future<UserAdmin> setRole(int userId, String newRole) async {
|
||||
final token = await _token();
|
||||
final data = await guardedApiCall(
|
||||
_ref,
|
||||
() => _apiClient.patchJson(UserApiPaths.setRole(userId), body: {'role': newRole}),
|
||||
() => _apiClient.patchJson(UserApiPaths.setRole(userId), body: {'role': newRole}, token: token),
|
||||
);
|
||||
return UserAdmin.fromJson(data);
|
||||
}
|
||||
|
||||
Future<UserAdmin> setPremium(int userId, {required bool isPremium}) async {
|
||||
final token = await _token();
|
||||
final data = await guardedApiCall(
|
||||
_ref,
|
||||
() => _apiClient.patchJson(UserApiPaths.setPremium(userId), body: {'isPremium': isPremium}),
|
||||
() => _apiClient.patchJson(UserApiPaths.setPremium(userId), body: {'isPremium': isPremium}, token: token),
|
||||
);
|
||||
return UserAdmin.fromJson(data);
|
||||
}
|
||||
@@ -44,6 +50,7 @@ class AdminRepository {
|
||||
required String password,
|
||||
String role = 'user',
|
||||
}) async {
|
||||
final token = await _token();
|
||||
final data = await guardedApiCall(
|
||||
_ref,
|
||||
() => _apiClient.postJson(UserApiPaths.list, body: {
|
||||
@@ -51,21 +58,25 @@ class AdminRepository {
|
||||
'email': email,
|
||||
'password': password,
|
||||
'role': role,
|
||||
}),
|
||||
}, token: token),
|
||||
);
|
||||
return UserAdmin.fromJson(data as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
Future<void> deleteUser(int userId) => guardedApiCall(
|
||||
_ref,
|
||||
() => _apiClient.deleteJson(UserApiPaths.delete(userId)),
|
||||
);
|
||||
Future<void> deleteUser(int userId) async {
|
||||
final token = await _token();
|
||||
return guardedApiCall(
|
||||
_ref,
|
||||
() => _apiClient.deleteJson(UserApiPaths.delete(userId), token: token),
|
||||
);
|
||||
}
|
||||
|
||||
/// Returns `{ temporaryPassword, to, subject, body }`.
|
||||
Future<Map<String, dynamic>> resetPassword(int userId) async {
|
||||
final token = await _token();
|
||||
final result = await guardedApiCall<dynamic>(
|
||||
_ref,
|
||||
() => _apiClient.postJson(UserApiPaths.resetPassword(userId)),
|
||||
() => _apiClient.postJson(UserApiPaths.resetPassword(userId), token: token),
|
||||
);
|
||||
return (result as Map<String, dynamic>);
|
||||
}
|
||||
|
||||
@@ -379,7 +379,7 @@ class _CreateUserDialogState extends State<_CreateUserDialog> {
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
DropdownButtonFormField<String>(
|
||||
value: _role,
|
||||
initialValue: _role,
|
||||
decoration: const InputDecoration(labelText: 'Roll'),
|
||||
items: const [
|
||||
DropdownMenuItem(value: 'user', child: Text('Användare')),
|
||||
|
||||
Reference in New Issue
Block a user