check phone impl

This commit is contained in:
efrilm 2025-09-18 08:01:49 +07:00
parent 214dfe3262
commit 1ca1a45126
15 changed files with 275 additions and 80 deletions

View File

@ -3,6 +3,7 @@ import 'package:dartz/dartz.dart';
import 'package:freezed_annotation/freezed_annotation.dart'; import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart'; import 'package:injectable/injectable.dart';
import '../../../common/function/app_function.dart';
import '../../../domain/auth/auth.dart'; import '../../../domain/auth/auth.dart';
part 'check_phone_form_event.dart'; part 'check_phone_form_event.dart';
@ -40,7 +41,7 @@ class CheckPhoneFormBloc
if (phoneNumberValid) { if (phoneNumberValid) {
failureOrCheckPhone = await _repository.checkPhone( failureOrCheckPhone = await _repository.checkPhone(
phoneNumber: state.phoneNumber, phoneNumber: getNormalizePhone(state.phoneNumber),
); );
emit( emit(
state.copyWith( state.copyWith(

View File

@ -7,7 +7,7 @@ part 'date_extension.dart';
extension StringExt on String { extension StringExt on String {
CheckPhoneStatus toCheckPhoneStatus() { CheckPhoneStatus toCheckPhoneStatus() {
switch (this) { switch (this) {
case 'NO_REGISTERED': case 'NOT_REGISTERED':
return CheckPhoneStatus.notRegistered; return CheckPhoneStatus.notRegistered;
case 'PASSWORD_REQUIRED': case 'PASSWORD_REQUIRED':
return CheckPhoneStatus.passwordRequired; return CheckPhoneStatus.passwordRequired;

View File

@ -6,3 +6,10 @@ void dismissKeyboard(BuildContext context) {
FocusManager.instance.primaryFocus?.unfocus(); FocusManager.instance.primaryFocus?.unfocus();
} }
} }
String getNormalizePhone(String phoneNumber) {
final normalizedPhone = phoneNumber.startsWith('08')
? phoneNumber.replaceFirst('0', '')
: phoneNumber;
return '62$normalizedPhone';
}

View File

@ -41,6 +41,13 @@ class AuthRemoteDataProvider {
AuthFailure.dynamicErrorMessage('No. Telepon Tidak Boleh Kosong'), AuthFailure.dynamicErrorMessage('No. Telepon Tidak Boleh Kosong'),
); );
} }
if (response.data['errors'][0]['code'] == 304) {
return DC.error(
AuthFailure.dynamicErrorMessage(
response.data['errors'][0]['cause'],
),
);
}
} }
} }

View File

@ -78,20 +78,20 @@ extension GetItInjectableX on _i174.GetIt {
gh.factory<_i510.LoginFormBloc>( gh.factory<_i510.LoginFormBloc>(
() => _i510.LoginFormBloc(gh<_i995.IAuthRepository>()), () => _i510.LoginFormBloc(gh<_i995.IAuthRepository>()),
); );
gh.factory<_i869.CheckPhoneFormBloc>( gh.factory<_i627.ResendFormBloc>(
() => _i869.CheckPhoneFormBloc(gh<_i995.IAuthRepository>()), () => _i627.ResendFormBloc(gh<_i995.IAuthRepository>()),
);
gh.factory<_i260.RegisterFormBloc>(
() => _i260.RegisterFormBloc(gh<_i995.IAuthRepository>()),
);
gh.factory<_i521.VerifyFormBloc>(
() => _i521.VerifyFormBloc(gh<_i995.IAuthRepository>()),
); );
gh.factory<_i174.SetPasswordFormBloc>( gh.factory<_i174.SetPasswordFormBloc>(
() => _i174.SetPasswordFormBloc(gh<_i995.IAuthRepository>()), () => _i174.SetPasswordFormBloc(gh<_i995.IAuthRepository>()),
); );
gh.factory<_i627.ResendFormBloc>( gh.factory<_i260.RegisterFormBloc>(
() => _i627.ResendFormBloc(gh<_i995.IAuthRepository>()), () => _i260.RegisterFormBloc(gh<_i995.IAuthRepository>()),
);
gh.factory<_i869.CheckPhoneFormBloc>(
() => _i869.CheckPhoneFormBloc(gh<_i995.IAuthRepository>()),
);
gh.factory<_i521.VerifyFormBloc>(
() => _i521.VerifyFormBloc(gh<_i995.IAuthRepository>()),
); );
return this; return this;
} }

View File

@ -1,4 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import '../../../common/theme/theme.dart'; import '../../../common/theme/theme.dart';

View File

@ -7,12 +7,14 @@ class AppElevatedButton extends StatelessWidget {
required this.title, required this.title,
this.width = double.infinity, this.width = double.infinity,
this.height = 48.0, this.height = 48.0,
this.isLoading = false,
}); });
final Function()? onPressed; final Function()? onPressed;
final String title; final String title;
final double width; final double width;
final double height; final double height;
final bool isLoading;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -21,13 +23,29 @@ class AppElevatedButton extends StatelessWidget {
height: height, height: height,
child: ElevatedButton( child: ElevatedButton(
onPressed: onPressed, onPressed: onPressed,
child: Text( child: isLoading
title, ? Row(
style: AppStyle.lg.copyWith( mainAxisAlignment: MainAxisAlignment.center,
color: AppColor.white, crossAxisAlignment: CrossAxisAlignment.center,
fontWeight: FontWeight.w700, children: [
), SpinKitFadingCircle(color: AppColor.white, size: 24),
), SizedBox(width: 8),
Text(
'Loading',
style: AppStyle.lg.copyWith(
color: AppColor.white,
fontWeight: FontWeight.w700,
),
),
],
)
: Text(
title,
style: AppStyle.lg.copyWith(
color: AppColor.white,
fontWeight: FontWeight.w700,
),
),
), ),
); );
} }

View File

@ -11,6 +11,7 @@ class AppTextFormField extends StatelessWidget {
this.suffixIcon, this.suffixIcon,
this.keyboardType, this.keyboardType,
this.onChanged, this.onChanged,
this.validator,
}); });
final String? hintText; final String? hintText;
@ -21,6 +22,7 @@ class AppTextFormField extends StatelessWidget {
final Widget? suffixIcon; final Widget? suffixIcon;
final TextInputType? keyboardType; final TextInputType? keyboardType;
final ValueChanged<String>? onChanged; final ValueChanged<String>? onChanged;
final String? Function(String?)? validator;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -39,6 +41,7 @@ class AppTextFormField extends StatelessWidget {
color: AppColor.textPrimary, color: AppColor.textPrimary,
fontWeight: FontWeight.w500, fontWeight: FontWeight.w500,
), ),
validator: validator,
decoration: InputDecoration( decoration: InputDecoration(
hintText: hintText, hintText: hintText,
prefixIcon: prefixIcon, prefixIcon: prefixIcon,

View File

@ -0,0 +1,54 @@
import 'package:another_flushbar/flushbar.dart';
import 'package:flutter/material.dart';
import '../../../common/theme/theme.dart';
import '../../../domain/auth/auth.dart';
class AppFlushbar {
static void showSuccess(BuildContext context, String message) {
Flushbar(
messageText: Text(
message,
style: AppStyle.lg.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
icon: const Icon(Icons.check_circle, color: Colors.white),
duration: const Duration(seconds: 2),
flushbarPosition: FlushbarPosition.BOTTOM,
backgroundColor: AppColor.secondary,
borderRadius: BorderRadius.circular(12),
margin: const EdgeInsets.all(12),
).show(context);
}
static void showError(BuildContext context, String message) {
Flushbar(
messageText: Text(
message,
style: AppStyle.lg.copyWith(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 16,
),
),
icon: const Icon(Icons.error, color: Colors.white),
duration: const Duration(seconds: 3),
flushbarPosition: FlushbarPosition.BOTTOM,
backgroundColor: AppColor.error,
borderRadius: BorderRadius.circular(12),
margin: const EdgeInsets.all(12),
).show(context);
}
static void showAuthFailureToast(BuildContext context, AuthFailure failure) =>
showError(
context,
failure.map(
serverError: (value) => value.failure.toStringFormatted(context),
dynamicErrorMessage: (value) => value.erroMessage,
unexpectedError: (value) => 'Terjadi kesalahan, silahkan coba lagi',
),
);
}

View File

@ -1,78 +1,131 @@
import 'dart:developer';
import 'package:auto_route/auto_route.dart'; import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../application/auth/check_phone_form/check_phone_form_bloc.dart';
import '../../../../common/theme/theme.dart'; import '../../../../common/theme/theme.dart';
import '../../../../domain/auth/auth.dart';
import '../../../../injection.dart';
import '../../../components/button/button.dart'; import '../../../components/button/button.dart';
import '../../../components/toast/flushbar.dart';
import '../../../router/app_router.gr.dart'; import '../../../router/app_router.gr.dart';
import 'widgets/phone_field.dart'; import 'widgets/phone_field.dart';
@RoutePage() @RoutePage()
class LoginPage extends StatelessWidget { class LoginPage extends StatelessWidget implements AutoRouteWrapper {
const LoginPage({super.key}); const LoginPage({super.key});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return BlocListener<CheckPhoneFormBloc, CheckPhoneFormState>(
appBar: AppBar(title: const Text('Masuk')), listenWhen: (p, c) =>
body: Padding( p.failureOrCheckPhoneOption != c.failureOrCheckPhoneOption,
padding: const EdgeInsets.symmetric(horizontal: 24.0), listener: (context, state) {
child: Column( state.failureOrCheckPhoneOption.fold(
crossAxisAlignment: CrossAxisAlignment.start, () => null,
children: [ (either) => either.fold(
const SizedBox(height: 40), (f) => AppFlushbar.showAuthFailureToast(context, f),
(data) {
// Title AppFlushbar.showSuccess(context, data.message);
LoginPhoneField(), Future.delayed(Duration(milliseconds: 1000), () {
log(data.toString());
const SizedBox(height: 50), if (data.status.isNotRegistered) {
context.router.push(RegisterRoute());
// Continue Button } else if (data.status.isPasswordRequired) {
AppElevatedButton( context.router.push(
onPressed: () { PasswordRoute(phoneNumber: data.phoneNumber),
context.router.push(RegisterRoute()); );
}, }
title: 'Lanjutkan', });
), },
),
const SizedBox(height: 24), );
},
// Terms and Conditions child: Scaffold(
Center( appBar: AppBar(title: const Text('Masuk')),
child: RichText( body: BlocBuilder<CheckPhoneFormBloc, CheckPhoneFormState>(
textAlign: TextAlign.center, builder: (context, state) {
text: TextSpan( return Padding(
style: AppStyle.md.copyWith( padding: const EdgeInsets.symmetric(horizontal: 24.0),
color: AppColor.textSecondary, child: Form(
height: 1.4, autovalidateMode: state.showErrorMessages
), ? AutovalidateMode.always
: AutovalidateMode.disabled,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
const TextSpan( const SizedBox(height: 40),
text: 'Dengan masuk Enaklo, kamu telah\nmenyetujui ',
// Title
LoginPhoneField(),
const SizedBox(height: 50),
// Continue Button
AppElevatedButton(
onPressed: state.isSubmitting
? null
: () {
context.read<CheckPhoneFormBloc>().add(
CheckPhoneFormEvent.submitted(),
);
},
isLoading: state.isSubmitting,
title: 'Lanjutkan',
), ),
TextSpan(
text: 'Syarat & Ketentuan', const SizedBox(height: 24),
style: AppStyle.md.copyWith(
color: AppColor.primary, // Terms and Conditions
fontWeight: FontWeight.w600, Center(
), child: RichText(
), textAlign: TextAlign.center,
const TextSpan(text: ' dan\n'), text: TextSpan(
TextSpan( style: AppStyle.md.copyWith(
text: 'Kebijakan Privasi', color: AppColor.textSecondary,
style: AppStyle.md.copyWith( height: 1.4,
color: AppColor.primary, ),
fontWeight: FontWeight.w600, children: [
const TextSpan(
text:
'Dengan masuk Enaklo, kamu telah\nmenyetujui ',
),
TextSpan(
text: 'Syarat & Ketentuan',
style: AppStyle.md.copyWith(
color: AppColor.primary,
fontWeight: FontWeight.w600,
),
),
const TextSpan(text: ' dan\n'),
TextSpan(
text: 'Kebijakan Privasi',
style: AppStyle.md.copyWith(
color: AppColor.primary,
fontWeight: FontWeight.w600,
),
),
],
),
), ),
), ),
const SizedBox(height: 40),
], ],
), ),
), ),
), );
},
const SizedBox(height: 40),
],
), ),
), ),
); );
} }
@override
Widget wrappedRoute(BuildContext context) => BlocProvider(
create: (context) => getIt<CheckPhoneFormBloc>(),
child: this,
);
} }

View File

@ -1,5 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../../application/auth/check_phone_form/check_phone_form_bloc.dart';
import '../../../../../common/theme/theme.dart'; import '../../../../../common/theme/theme.dart';
import '../../../../components/field/field.dart'; import '../../../../components/field/field.dart';
@ -45,6 +47,13 @@ class _LoginPhoneFieldState extends State<LoginPhoneField> {
controller: _controller, controller: _controller,
focusNode: _focusNode, focusNode: _focusNode,
keyboardType: TextInputType.phone, keyboardType: TextInputType.phone,
validator: (value) {
if (context.read<CheckPhoneFormBloc>().state.phoneNumber.isEmpty) {
return 'Masukkan no telepon';
}
return null;
},
prefixIcon: Padding( prefixIcon: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12), padding: const EdgeInsets.symmetric(horizontal: 12),
child: Text( child: Text(
@ -64,7 +73,9 @@ class _LoginPhoneFieldState extends State<LoginPhoneField> {
) )
: null, : null,
onChanged: (value) { onChanged: (value) {
setState(() {}); context.read<CheckPhoneFormBloc>().add(
CheckPhoneFormEvent.phoneNumberChanged(value),
);
}, },
); );
} }

View File

@ -7,12 +7,13 @@ import '../../../router/app_router.gr.dart';
@RoutePage() @RoutePage()
class PasswordPage extends StatelessWidget { class PasswordPage extends StatelessWidget {
const PasswordPage({super.key}); final String phoneNumber;
const PasswordPage({super.key, required this.phoneNumber});
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
appBar: AppBar(title: const Text('Buat Kata Sandi')), appBar: AppBar(title: const Text('Kata Sandi')),
body: Padding( body: Padding(
padding: const EdgeInsets.symmetric(horizontal: 24.0), padding: const EdgeInsets.symmetric(horizontal: 24.0),
child: Column( child: Column(

View File

@ -255,7 +255,7 @@ class LoginRoute extends _i33.PageRouteInfo<void> {
static _i33.PageInfo page = _i33.PageInfo( static _i33.PageInfo page = _i33.PageInfo(
name, name,
builder: (data) { builder: (data) {
return const _i12.LoginPage(); return _i33.WrappedRoute(child: const _i12.LoginPage());
}, },
); );
} }
@ -432,20 +432,41 @@ class OtpRoute extends _i33.PageRouteInfo<void> {
/// generated route for /// generated route for
/// [_i21.PasswordPage] /// [_i21.PasswordPage]
class PasswordRoute extends _i33.PageRouteInfo<void> { class PasswordRoute extends _i33.PageRouteInfo<PasswordRouteArgs> {
const PasswordRoute({List<_i33.PageRouteInfo>? children}) PasswordRoute({
: super(PasswordRoute.name, initialChildren: children); _i34.Key? key,
required String phoneNumber,
List<_i33.PageRouteInfo>? children,
}) : super(
PasswordRoute.name,
args: PasswordRouteArgs(key: key, phoneNumber: phoneNumber),
initialChildren: children,
);
static const String name = 'PasswordRoute'; static const String name = 'PasswordRoute';
static _i33.PageInfo page = _i33.PageInfo( static _i33.PageInfo page = _i33.PageInfo(
name, name,
builder: (data) { builder: (data) {
return const _i21.PasswordPage(); final args = data.argsAs<PasswordRouteArgs>();
return _i21.PasswordPage(key: args.key, phoneNumber: args.phoneNumber);
}, },
); );
} }
class PasswordRouteArgs {
const PasswordRouteArgs({this.key, required this.phoneNumber});
final _i34.Key? key;
final String phoneNumber;
@override
String toString() {
return 'PasswordRouteArgs{key: $key, phoneNumber: $phoneNumber}';
}
}
/// generated route for /// generated route for
/// [_i22.PaymentPage] /// [_i22.PaymentPage]
class PaymentRoute extends _i33.PageRouteInfo<void> { class PaymentRoute extends _i33.PageRouteInfo<void> {

View File

@ -17,6 +17,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "7.7.1" version: "7.7.1"
another_flushbar:
dependency: "direct main"
description:
name: another_flushbar
sha256: "2b99671c010a7d5770acf5cb24c9f508b919c3a7948b6af9646e773e7da7b757"
url: "https://pub.dev"
source: hosted
version: "1.12.32"
archive: archive:
dependency: transitive dependency: transitive
description: description:
@ -478,6 +486,14 @@ packages:
url: "https://pub.dev" url: "https://pub.dev"
source: hosted source: hosted
version: "5.0.0" version: "5.0.0"
flutter_spinkit:
dependency: "direct main"
description:
name: flutter_spinkit
sha256: "77850df57c00dc218bfe96071d576a8babec24cf58b2ed121c83cca4a2fdce7f"
url: "https://pub.dev"
source: hosted
version: "5.2.2"
flutter_svg: flutter_svg:
dependency: "direct main" dependency: "direct main"
description: description:

View File

@ -34,6 +34,8 @@ dependencies:
audioplayers: ^6.5.1 audioplayers: ^6.5.1
flutter_bloc: ^9.1.1 flutter_bloc: ^9.1.1
bloc: ^9.0.0 bloc: ^9.0.0
another_flushbar: ^1.12.32
flutter_spinkit: ^5.2.2
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: