Refactor HTTPInterceptor and add CustomSnackBar helper

Refactor HTTPInterceptor to handle error responses and add a CustomSnackBar
helper to display snack bars. This will improve error handling and user
feedback in the application.
This commit is contained in:
Mohammad Salameh
2024-04-15 12:02:34 +03:00
parent 590c70a7d8
commit cfc395e210
10 changed files with 287 additions and 188 deletions

View File

@ -32,21 +32,25 @@ class LoginForm extends StatelessWidget {
validator: (value) {
if (state is! AuthTokenError) {
if (value != null) {
if (value.isEmpty) {
if (value.isNotEmpty) {
if (RegExp(
r'^[a-zA-Z0-9._-]+@{1}[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$')
.hasMatch(value)) {
return null;
} else {
return 'Please enter a valid email';
}
} else {
return 'Please enter your email';
}
//Regex for email validation
if (!RegExp(
r'^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$')
.hasMatch(value)) {
return 'Please enter a valid email';
}
} else {
return 'Please enter your email';
}
}
return null;
},
onTapOutside: (event) {
formKey.currentState!.validate();
FocusScope.of(context).unfocus();
},
decoration: defaultInputDecoration(context,
hint: "Example@email.com"),
@ -62,9 +66,19 @@ class LoginForm extends StatelessWidget {
if (state is! AuthTokenError) {
if (value != null) {
if (value.isNotEmpty) {
if (value.length < 6) {
return 'Password must be at least 8 characters';
}
return null;
//TODO: uncomment this when the backend is ready
// if (value.length > 8) {
// if (RegExp(
// r'^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$')
// .hasMatch(value)) {
// return null;
// } else {
// return 'Password must contain at least one uppercase letter, one lowercase letter, one number and one special character';
// }
// } else {
// return 'Password must be at least 8 characters';
// }
} else {
return 'Please enter your password';
}
@ -73,7 +87,7 @@ class LoginForm extends StatelessWidget {
return null;
},
onTapOutside: (event) {
formKey.currentState!.validate();
FocusScope.of(context).unfocus();
},
obscureText: !AuthCubit.get(context).isPasswordVisible,
decoration: defaultInputDecoration(context,
@ -88,8 +102,16 @@ class LoginForm extends StatelessWidget {
children: [
Expanded(
child: DefaultButton(
isDone: state is AuthSuccess,
isDone: state is AuthLoginSuccess,
isLoading: state is AuthLoading,
// enabled: AuthCubit.get(context)
// .emailController
// .text
// .isNotEmpty &&
// AuthCubit.get(context)
// .passwordController
// .text
// .isNotEmpty,
customButtonStyle: ButtonStyle(
backgroundColor: MaterialStateProperty.all(
Colors.black.withOpacity(.25),
@ -103,8 +125,10 @@ class LoginForm extends StatelessWidget {
),
onPressed: () {
if (formKey.currentState!.validate()) {
AuthCubit.get(context).login();
FocusScope.of(context).unfocus();
if (state is! AuthLoginLoading) {
AuthCubit.get(context).login();
FocusScope.of(context).unfocus();
}
}
},
),