region and access_management ui

This commit is contained in:
mohammad
2024-08-12 15:23:45 +03:00
parent cb0ebcca37
commit 3f1fdf4f93
3 changed files with 287 additions and 249 deletions

View File

@ -40,10 +40,8 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
if (_timer != null && _timer!.isActive) { if (_timer != null && _timer!.isActive) {
return; return;
} }
_remainingTime = 60; _remainingTime = 1;
add(UpdateTimerEvent( add(UpdateTimerEvent(remainingTime: _remainingTime, isButtonEnabled: false));
remainingTime: _remainingTime, isButtonEnabled: false));
debugPrint('_remainingTime=$_remainingTime');
_remainingTime = (await AuthenticationAPI.sendOtp(email: forgetEmailController.text,regionUuid: regionUuid))!; _remainingTime = (await AuthenticationAPI.sendOtp(email: forgetEmailController.text,regionUuid: regionUuid))!;
_timer = Timer.periodic(const Duration(seconds: 1), (timer) { _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
_remainingTime--; _remainingTime--;
@ -125,7 +123,6 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
} catch (failure) { } catch (failure) {
validate='Something went wrong'; validate='Something went wrong';
emit(const LoginFailure(error: 'Something went wrong')); emit(const LoginFailure(error: 'Something went wrong'));
// emit(LoginFailure(error: failure.toString()));
return; return;
} }
if (token.accessTokenIsNotEmpty) { if (token.accessTokenIsNotEmpty) {
@ -187,6 +184,8 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
return 'Email is required'; return 'Email is required';
} else if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value)) { } else if (!RegExp(r'^[^@]+@[^@]+\.[^@]+').hasMatch(value)) {
return 'Enter a valid email address'; return 'Enter a valid email address';
}else if (regionUuid=='') {
return 'Please select your region';
} }
return null; return null;
} }

View File

@ -10,11 +10,11 @@ import 'package:syncrow_web/pages/common/first_layer.dart';
import 'package:syncrow_web/utils/color_manager.dart'; import 'package:syncrow_web/utils/color_manager.dart';
import 'package:syncrow_web/utils/constants/assets.dart'; import 'package:syncrow_web/utils/constants/assets.dart';
import 'package:syncrow_web/utils/style.dart'; import 'package:syncrow_web/utils/style.dart';
class ForgetPasswordWebPage extends StatelessWidget { class ForgetPasswordWebPage extends StatelessWidget {
const ForgetPasswordWebPage({super.key}); const ForgetPasswordWebPage({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
body: BlocProvider( body: BlocProvider(
create: (context) => AuthBloc()..add(RegionInitialEvent()), create: (context) => AuthBloc()..add(RegionInitialEvent()),
@ -22,8 +22,7 @@ class ForgetPasswordWebPage extends StatelessWidget {
listener: (context, state) { listener: (context, state) {
if (state is SuccessForgetState) { if (state is SuccessForgetState) {
Navigator.of(context).pop(); Navigator.of(context).pop();
} } else if (state is FailureForgetState) {
else if (state is FailureForgetState) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text(state.error), content: Text(state.error),
@ -47,13 +46,15 @@ class ForgetPasswordWebPage extends StatelessWidget {
late ScrollController _scrollController; late ScrollController _scrollController;
_scrollController = ScrollController(); _scrollController = ScrollController();
void _scrollToCenter() { void _scrollToCenter() {
final double middlePosition = _scrollController.position.maxScrollExtent / 2; final double middlePosition =
_scrollController.position.maxScrollExtent / 2;
_scrollController.animateTo( _scrollController.animateTo(
middlePosition, middlePosition,
duration: const Duration(seconds: 1), duration: const Duration(seconds: 1),
curve: Curves.easeInOut, curve: Curves.easeInOut,
); );
} }
WidgetsBinding.instance.addPostFrameCallback((_) { WidgetsBinding.instance.addPostFrameCallback((_) {
_scrollToCenter(); _scrollToCenter();
}); });
@ -90,8 +91,10 @@ class ForgetPasswordWebPage extends StatelessWidget {
child: Container( child: Container(
decoration: BoxDecoration( decoration: BoxDecoration(
color: Colors.white.withOpacity(0.1), color: Colors.white.withOpacity(0.1),
borderRadius: const BorderRadius.all(Radius.circular(30)), borderRadius:
border: Border.all(color: ColorsManager.graysColor.withOpacity(0.2)), const BorderRadius.all(Radius.circular(30)),
border: Border.all(
color: ColorsManager.graysColor.withOpacity(0.2)),
), ),
child: Form( child: Form(
key: forgetBloc.forgetFormKey, key: forgetBloc.forgetFormKey,
@ -123,7 +126,8 @@ class ForgetPasswordWebPage extends StatelessWidget {
children: [ children: [
Text( Text(
"Country/Region", "Country/Region",
style: Theme.of(context).textTheme.bodySmall, style:
Theme.of(context).textTheme.bodySmall,
), ),
const SizedBox(height: 10), const SizedBox(height: 10),
SizedBox( SizedBox(
@ -143,20 +147,23 @@ class ForgetPasswordWebPage extends StatelessWidget {
'Select your region/country', 'Select your region/country',
textAlign: TextAlign.center, textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis, overflow: TextOverflow.ellipsis,
), ),
), ),
), ),
isDense: true, isDense: true,
style: const TextStyle(color: Colors.black), style:
items:forgetBloc.regionList!.map((RegionModel region) { const TextStyle(color: Colors.black),
items: forgetBloc.regionList!
.map((RegionModel region) {
return DropdownMenuItem<String>( return DropdownMenuItem<String>(
value: region.id, value: region.id,
child: Text(region.name), child: Text(region.name),
); );
}).toList(), }).toList(),
onChanged: (String? value) { onChanged: (String? value) {
forgetBloc.add(SelectRegionEvent(val: value!,)); forgetBloc.add(SelectRegionEvent(
val: value!,
));
}, },
), ),
) )
@ -167,16 +174,21 @@ class ForgetPasswordWebPage extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
children: [ children: [
Text("Account", Text(
style: Theme.of(context).textTheme.bodySmall, "Account",
style:
Theme.of(context).textTheme.bodySmall,
), ),
const SizedBox(height: 10), const SizedBox(height: 10),
SizedBox( SizedBox(
child: TextFormField( child: TextFormField(
validator: forgetBloc.validateEmail, validator: forgetBloc.validateEmail,
controller: forgetBloc.forgetEmailController, controller:
decoration: textBoxDecoration()!.copyWith(hintText: 'Enter your email'), forgetBloc.forgetEmailController,
style: const TextStyle(color: Colors.black), decoration: textBoxDecoration()!.copyWith(
hintText: 'Enter your email'),
style:
const TextStyle(color: Colors.black),
), ),
), ),
], ],
@ -186,13 +198,17 @@ class ForgetPasswordWebPage extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
children: [ children: [
Text("One Time Password", Text(
style: Theme.of(context).textTheme.bodySmall,), "One Time Password",
style:
Theme.of(context).textTheme.bodySmall,
),
const SizedBox(height: 10), const SizedBox(height: 10),
SizedBox( SizedBox(
child: TextFormField( child: TextFormField(
validator: forgetBloc.validateCode, validator: forgetBloc.validateCode,
keyboardType: TextInputType.visiblePassword, keyboardType:
TextInputType.visiblePassword,
controller: forgetBloc.forgetOtp, controller: forgetBloc.forgetOtp,
decoration: textBoxDecoration()!.copyWith( decoration: textBoxDecoration()!.copyWith(
hintText: 'Enter Code', hintText: 'Enter Code',
@ -201,13 +217,15 @@ class ForgetPasswordWebPage extends StatelessWidget {
child: Center( child: Center(
child: InkWell( child: InkWell(
onTap: () { onTap: () {
BlocProvider.of<AuthBloc>(context).add(StartTimerEvent()); BlocProvider.of<AuthBloc>(
context)
.add(StartTimerEvent());
}, },
child: child: Text(
Text(
'Get Code ${state is TimerState && !state.isButtonEnabled ? "(${BlocProvider.of<AuthBloc>(context).formattedTime(state.remainingTime)}) " : ""}', 'Get Code ${state is TimerState && !state.isButtonEnabled ? "(${BlocProvider.of<AuthBloc>(context).formattedTime(state.remainingTime)}) " : ""}',
style: TextStyle( style: TextStyle(
color: state is TimerState && !state.isButtonEnabled color: state is TimerState &&
!state.isButtonEnabled
? Colors.grey ? Colors.grey
: ColorsManager.btnColor, : ColorsManager.btnColor,
), ),
@ -216,7 +234,8 @@ class ForgetPasswordWebPage extends StatelessWidget {
), ),
), ),
), ),
style: const TextStyle(color: Colors.black), style:
const TextStyle(color: Colors.black),
), ),
), ),
], ],
@ -226,18 +245,24 @@ class ForgetPasswordWebPage extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.start,
children: [ children: [
Text("Password", Text(
style: Theme.of(context).textTheme.bodySmall,), "Password",
style:
Theme.of(context).textTheme.bodySmall,
),
const SizedBox(height: 10), const SizedBox(height: 10),
SizedBox( SizedBox(
child: TextFormField( child: TextFormField(
validator: forgetBloc.passwordValidator, validator: forgetBloc.passwordValidator,
keyboardType: TextInputType.visiblePassword, keyboardType:
controller: forgetBloc.forgetPasswordController, TextInputType.visiblePassword,
controller:
forgetBloc.forgetPasswordController,
decoration: textBoxDecoration()!.copyWith( decoration: textBoxDecoration()!.copyWith(
hintText: 'At least 8 characters', hintText: 'At least 8 characters',
), ),
style: const TextStyle(color: Colors.black), style:
const TextStyle(color: Colors.black),
), ),
), ),
], ],
@ -256,7 +281,9 @@ class ForgetPasswordWebPage extends StatelessWidget {
backgroundColor: ColorsManager.btnColor, backgroundColor: ColorsManager.btnColor,
child: const Text('Submit'), child: const Text('Submit'),
onPressed: () { onPressed: () {
if (forgetBloc.forgetFormKey.currentState!.validate()) { if (forgetBloc
.forgetFormKey.currentState!
.validate()) {
forgetBloc.add(ChangePasswordEvent()); forgetBloc.add(ChangePasswordEvent());
} }
}, },
@ -265,9 +292,17 @@ class ForgetPasswordWebPage extends StatelessWidget {
], ],
), ),
const SizedBox(height: 10.0), const SizedBox(height: 10.0),
SizedBox(child: Text(forgetBloc.validate, SizedBox(
style: const TextStyle(fontWeight: FontWeight.w700,color: ColorsManager.red ),),), child: Text(
SizedBox(height: 10,), forgetBloc.validate,
style: const TextStyle(
fontWeight: FontWeight.w700,
color: ColorsManager.red),
),
),
SizedBox(
height: 10,
),
SizedBox( SizedBox(
width: size.width * 0.2, width: size.width * 0.2,
child: Row( child: Row(
@ -304,7 +339,6 @@ class ForgetPasswordWebPage extends StatelessWidget {
), ),
], ],
), ),
) ));
);
} }
} }

View File

@ -45,33 +45,38 @@ class AuthenticationAPI {
"regionUuid": regionUuid "regionUuid": regionUuid
}, },
showServerMessage: true, showServerMessage: true,
options: Options(),
expectedResponseModel: (json) { expectedResponseModel: (json) {
Map<String, dynamic> parsedJson = jsonDecode(json); return 30;
int cooldown = parsedJson['data']['cooldown'];
debugPrint('Cooldown: $cooldown seconds');
return cooldown;
} }
); );
return response; return 30;
} on DioError catch (e) { } on DioError catch (e) {
if (e.response != null) { if (e.response != null) {
if (e.response!.statusCode == 400) { if (e.response!.statusCode == 400) {
// Handle 400 Bad Request // Handle 400 Bad Request
final errorData = e.response!.data; final errorData = e.response!.data;
String errorMessage = errorData['message'] ?? 'Unknown error'; String errorMessage = errorData['message'];
int cooldown = errorData['data']['cooldown'] ?? 0; debugPrint('Unexpected Error: $errorMessage');
if(errorMessage=='User not found'){
return 1;
}else{
int cooldown = errorData['data']['cooldown'] ?? 1;
return cooldown; return cooldown;
}
} else { } else {
debugPrint('Error: ${e.response!.statusCode} - ${e.response!.statusMessage}'); debugPrint('Error: ${e.response!.statusCode} - ${e.response!.statusMessage}');
return 1;
} }
} else { } else {
debugPrint('Error: ${e.message}'); debugPrint('Error: ${e.message}');
return 1;
} }
return null; return 1;
} catch (e) { } catch (e) {
debugPrint('Unexpected Error: $e'); debugPrint('Unexpected Error: $e');
return null; return 1;
} }
} }
@ -79,7 +84,7 @@ class AuthenticationAPI {
{required String email, required String otpCode}) async { {required String email, required String otpCode}) async {
final response = await HTTPService().post( final response = await HTTPService().post(
path: ApiEndpoints.verifyOtp, path: ApiEndpoints.verifyOtp,
body: {"email": email, "type": "VERIFICATION", "otpCode": otpCode}, body: {"email": email, "type": "PASSWORD", "otpCode": otpCode},
showServerMessage: true, showServerMessage: true,
expectedResponseModel: (json) { expectedResponseModel: (json) {
if (json['message'] == 'Otp Verified Successfully') { if (json['message'] == 'Otp Verified Successfully') {