Skip to content

Commit

Permalink
Add clear and save buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasatisocco committed Jul 24, 2023
1 parent 40957e1 commit ea58280
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class AdminUsersCubit extends Cubit<AdminUsersState> {

final FirestoreRepository _firestoreRepository;
late final StreamSubscription<dynamic> _userListSubscription;
UserDataModel? _shadowUser;

Future<void> init() async {
emit(const AdminFetchingUsers());
Expand All @@ -30,6 +31,7 @@ class AdminUsersCubit extends Cubit<AdminUsersState> {
final shadowState = state as AdminUsersFetched;
user = shadowState.user;
if (user != null) {
_shadowUser = user;
user = users.firstWhere(
(element) => element.authId == user!.authId,
orElse: () => user!,
Expand All @@ -49,9 +51,52 @@ class AdminUsersCubit extends Cubit<AdminUsersState> {
final user = shadowState.users.firstWhere(
(element) => element.authId == userId,
);
_shadowUser = user;
emit(shadowState.copyWith(user: user));
}

void updateSelectedUser(UserDataModel user) {
if (state is! AdminUsersFetched) return;
final shadowState = state as AdminUsersFetched;
emit(const AdminFetchingUsers());
final users = shadowState.users.map((e) {
if (e.authId == user.authId) {
return user;
}
return e;
}).toList();
emit(shadowState.copyWith(users: users, user: user));
}

bool get isUserUpdated {
if (state is! AdminUsersFetched) return false;
final shadowState = state as AdminUsersFetched;
if (_shadowUser == null) return false;
if (shadowState.user == null) return false;
return shadowState.user != _shadowUser;
}

void clearUser() {
if (state is! AdminUsersFetched) return;
final shadowState = state as AdminUsersFetched;
emit(shadowState.copyWith(user: _shadowUser));
}

Future<void> updateUser() async {
if (state is! AdminUsersFetched) return;
final shadowState = state as AdminUsersFetched;
final user = shadowState.user;
if (user == null) return;
try {
emit(shadowState.copyWith(isUpdating: true));
await _firestoreRepository.updateUser(user.toMap(), user.id!);
_shadowUser = user;
emit(shadowState.copyWith());
} catch (_) {
emit(shadowState.copyWith(user: _shadowUser));
}
}

@override
Future<void> close() {
_userListSubscription.cancel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,30 @@ class AdminFetchingUsers extends AdminUsersState {
}

class AdminUsersFetched extends AdminUsersState {
const AdminUsersFetched({required this.users, this.user});
const AdminUsersFetched({
required this.users,
this.user,
this.isUpdating = false,
});

final List<UserDataModel> users;
final UserDataModel? user;
final bool isUpdating;

AdminUsersFetched copyWith({
List<UserDataModel>? users,
UserDataModel? user,
bool isUpdating = false,
}) {
return AdminUsersFetched(
users: users ?? this.users,
user: user ?? this.user,
isUpdating: isUpdating,
);
}

@override
List<Object> get props => [users, user ?? ''];
List<Object> get props => [users, user ?? '', isUpdating];
}

class AdminUsersError extends AdminUsersState {
Expand Down
41 changes: 40 additions & 1 deletion lib/admin_panel/admin_page/widgets/user_info_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,37 @@ class UserInfoWidget extends StatelessWidget {
child: Column(
children: [
const SizedBox(height: 20),
SizedBox(
width: MediaQuery.sizeOf(context).width - 250,
child: Row(
children: [
const Spacer(),
ElevatedButton(
onPressed: context.read<AdminUsersCubit>().isUserUpdated
? () => context.read<AdminUsersCubit>().clearUser()
: null,
child: const Text('Reestablecer'),
),
const SizedBox(width: 20),
ElevatedButton(
onPressed: context.read<AdminUsersCubit>().isUserUpdated
? () => context.read<AdminUsersCubit>().updateUser()
: null,
child: state.isUpdating
? const SizedBox(
height: 20,
child: Center(
child: CircularProgressIndicator(
color: Colors.white,
),
),
)
: const Text('Actualizar'),
),
const SizedBox(width: 20),
],
),
),
SizedBox(
width: MediaQuery.sizeOf(context).width - 250,
child: Row(
Expand Down Expand Up @@ -99,7 +130,15 @@ class UserInfoWidget extends StatelessWidget {
trailing: Switch(
value: state.user?.isPaid ?? false,
activeTrackColor: Colors.green,
onChanged: (value) {},
onChanged: (value) {
final user = state.user;
if (user == null) return;
context
.read<AdminUsersCubit>()
.updateSelectedUser(
user.copyWith(isPaid: value),
);
},
),
),
),
Expand Down
4 changes: 3 additions & 1 deletion lib/l10n/arb/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,7 @@
"newTestPossible": "You can make a new gps in",
"cancel": "Cancel",
"sureToLogout": "Are you sure you want to logout?",
"localizeYourself": "Find where you are and explode your potential with professional coachee"
"localizeYourself": "Find where you are and explode your potential with professional coachee",
"update": "Update",
"clear": "Clear"
}
4 changes: 3 additions & 1 deletion lib/l10n/arb/app_es.arb
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,7 @@
"newTestPossible": "Ya puedes realizar un nuevo GPS",
"cancel": "Cancelar",
"sureToLogout": "¿Estás seguro que quieres cerrar sesión?",
"localizeYourself": "Ubícate dentro de tu camino de desarrollo y explota tu crecimiento con asesoramiento profesional."
"localizeYourself": "Ubícate dentro de tu camino de desarrollo y explota tu crecimiento con asesoramiento profesional.",
"update": "Actualizar",
"clear": "Limpiar"
}
20 changes: 19 additions & 1 deletion lib/welcome/models/user_date_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
import 'dart:convert';
import 'package:cloud_firestore/cloud_firestore.dart' show Timestamp;
import 'package:coaching/welcome/models/subscription.dart';
import 'package:equatable/equatable.dart';

class UserDataModel {
class UserDataModel extends Equatable {
const UserDataModel({
this.isPaid = false,
this.id,
Expand Down Expand Up @@ -170,6 +171,23 @@ class UserDataModel {
)
''';
}

@override
List<Object?> get props => [
id,
authId,
email,
name,
birthDate,
nationality,
residence,
certificateDate,
createdAt,
testIds,
status,
subscription,
isPaid,
];
}

enum Status {
Expand Down

0 comments on commit ea58280

Please sign in to comment.