Skip to content

Commit

Permalink
refactor: real api - user edit
Browse files Browse the repository at this point in the history
  • Loading branch information
cevheri committed Dec 30, 2024
1 parent f3b375c commit f5944a7
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 38 deletions.
29 changes: 5 additions & 24 deletions lib/presentation/screen/user/editor/user_editor_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,21 @@ import 'package:go_router/go_router.dart';

class UserEditorScreen extends StatelessWidget {
final String? id;
final String? username;
final EditorFormMode mode;

const UserEditorScreen({
super.key,
this.id,
this.username,
required this.mode,
});

@override
Widget build(BuildContext context) {
final bloc = context.read<UserBloc>();
final initialEvent = id != null ? UserFetchEvent(id!) : const UserEditorInit();
final userId = id ?? username ?? '';
final initialEvent = userId.isNotEmpty ? UserFetchEvent(userId) : const UserEditorInit();
bloc.add(initialEvent);
return UserEditorWidget(mode: mode);
}
Expand Down Expand Up @@ -149,29 +152,6 @@ class UserEditorWidget extends StatelessWidget {
'authorities': state.data?.authorities?.first ?? state.data?.authorities?.firstOrNull,
};
debugPrint("checkpoint initial value: $initialValue");
// return SingleChildScrollView(
// child: Padding(
// padding: const EdgeInsets.all(16.0),
// child: Center(
// child: ConstrainedBox(
// constraints: const BoxConstraints(maxWidth: 700),
// child: FormBuilder(
// key: _formKey,
// initialValue: initialValue,
// child: Column(
// crossAxisAlignment: CrossAxisAlignment.stretch,
// children: [
// ..._buildFormFields(context, state),
// const SizedBox(height: 20),
// if (mode == EditorFormMode.view) _backButtonField(context),
// if (mode != EditorFormMode.view) _submitButtonField(context, state),
// ],
// ),
// ),
// ),
// ),
// ),
// );
return ResponsiveFormBuilder(
formKey: _formKey,
initialValue: initialValue,
Expand Down Expand Up @@ -226,6 +206,7 @@ class UserEditorWidget extends StatelessWidget {
}

List<Widget> _buildFormFields(BuildContext context, UserState state) {
debugPrint("checkpoint build form fields: ${state.data?.login}");
return [
UserFormFields.usernameField(context, state.data?.login, enabled: mode == EditorFormMode.create),
UserFormFields.firstNameField(context, state.data?.firstName, enabled: mode != EditorFormMode.view),
Expand Down
19 changes: 10 additions & 9 deletions lib/presentation/screen/user/list/list_user_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_bloc_advance/configuration/padding_spacing.dart';
import 'package:flutter_bloc_advance/generated/l10n.dart';
import 'package:flutter_bloc_advance/presentation/screen/components/authority_lov_widget.dart';
import 'package:flutter_bloc_advance/presentation/screen/user/bloc/user.dart';
Expand Down Expand Up @@ -139,15 +140,13 @@ class UserSearchSection extends StatelessWidget {
key: formKey,
child: IntrinsicHeight(
child: Row(
spacing: Spacing.small,
mainAxisSize: MainAxisSize.max,
children: [
SizedBox(width: 75, child: Text(S.of(context).filter)),
const Flexible(flex: 2, child: AuthorityDropdown()),
const SizedBox(width: 10),
const SizedBox(width: 200, child: PaginationControls()),
const SizedBox(width: 10),
const Flexible(child: SearchNameField()),
const SizedBox(width: 10),
SearchActionButtons(formKey: formKey),
],
),
Expand Down Expand Up @@ -403,7 +402,7 @@ class UserTableRow extends StatelessWidget {
const SizedBox(width: 5),
UserTableCell(flex: 3, text: user.activated! ? "active" : "passive"),
const SizedBox(width: 5),
UserActionButtons(userId: user.id!, formKey: formKey),
UserActionButtons(userId: user.login!, formKey: formKey),
],
),
);
Expand Down Expand Up @@ -456,12 +455,14 @@ class UserTableCell extends StatelessWidget {
/// Handles edit, view, and delete operations.
/// Manages confirmation dialogs and navigation.
class UserActionButtons extends StatelessWidget {
final String userId;
final String? userId;
final String? username;
final GlobalKey<FormBuilderState> formKey;

const UserActionButtons({
super.key,
required this.userId,
this.userId,
this.username,
required this.formKey,
});

Expand Down Expand Up @@ -518,11 +519,11 @@ class UserActionButtons extends StatelessWidget {
}

void _handleEdit(BuildContext context) {
context.goNamed('userEdit', pathParameters: {'id': userId}); //then((_) => !context.mounted ? null : _refreshList(context));
context.goNamed('userEdit', pathParameters: {'id': userId ?? username ?? ""});
}

void _handleView(BuildContext context) {
context.goNamed('userView', pathParameters: {'id': userId}); //.then((_) => !context.mounted ? null : _refreshList(context));
context.goNamed('userView', pathParameters: {'id': userId ?? username ?? ""});
}

void _showDeleteConfirmation(BuildContext context) {
Expand All @@ -539,7 +540,7 @@ class UserActionButtons extends StatelessWidget {
TextButton(
onPressed: () {
Navigator.pop(context);
context.read<UserBloc>().add(UserDeleteEvent(userId));
context.read<UserBloc>().add(UserDeleteEvent(userId ?? username ?? ""));
late final StreamSubscription<UserState> subscription;
subscription = context.read<UserBloc>().stream.listen((state) {
if (state.status == UserStatus.deleteSuccess && context.mounted) {
Expand Down
11 changes: 6 additions & 5 deletions lib/routes/go_router_routes/app_go_router_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ class AppGoRouterConfig {
}

// check : when redirect the new page then load the account data
var accountBloc = context.read<AccountBloc>();
await Future.delayed(const Duration(microseconds: 500));
accountBloc.add(const AccountFetchEvent());
_log.debug("redirect - load event : accountBloc.add(AccountLoad())");

if(state.uri.toString() == ApplicationRoutesConstants.home) {
var accountBloc = context.read<AccountBloc>();
await Future.delayed(const Duration(microseconds: 500));
accountBloc.add(const AccountFetchEvent());
_log.debug("redirect - load event : accountBloc.add(AccountLoad())");
}
// check : when jwtToken is null then redirect to login page
if (!SecurityUtils.isUserLoggedIn() && !SecurityUtils.isAllowedPath(state.uri.toString()) && state.uri.toString() != ApplicationRoutesConstants.login) {
_log.debug("END: isUserLoggedIn is false and isAllowedPath is false");
Expand Down

0 comments on commit f5944a7

Please sign in to comment.