From 15cc37c8a1608b47aa58789d0bad87e206012027 Mon Sep 17 00:00:00 2001 From: ernest-deriv Date: Tue, 16 Jul 2024 16:49:52 +0400 Subject: [PATCH] Updates in signup flow --- .../deriv_country_selection_layout.dart | 53 ++++++++++++++--- .../layouts/deriv_set_password_layout.dart | 58 +++++++++++++++++-- .../deriv_verification_done_layout.dart | 30 +++++++++- .../core/models/signup_page_model.dart | 8 +++ 4 files changed, 133 insertions(+), 16 deletions(-) diff --git a/packages/deriv_auth/lib/features/signup/presentation/layouts/deriv_country_selection_layout.dart b/packages/deriv_auth/lib/features/signup/presentation/layouts/deriv_country_selection_layout.dart index 60c564e59..1fcd50eff 100644 --- a/packages/deriv_auth/lib/features/signup/presentation/layouts/deriv_country_selection_layout.dart +++ b/packages/deriv_auth/lib/features/signup/presentation/layouts/deriv_country_selection_layout.dart @@ -2,8 +2,11 @@ import 'package:deriv_auth/core/extensions/context_extension.dart'; import 'package:deriv_auth/core/helpers/assets.dart'; import 'package:deriv_auth/core/helpers/country_selection_helper.dart'; import 'package:deriv_auth/features/signup/cubit/deriv_country_selection_cubit.dart'; +import 'package:deriv_auth/features/signup/cubit/signup_cubit.dart'; import 'package:deriv_auth/features/signup/models/deriv_residence_model.dart'; +import 'package:deriv_auth/features/signup/presentation/layouts/deriv_set_password_layout.dart'; import 'package:deriv_auth/features/signup/presentation/widgets/country_selection_list_widget.dart'; +import 'package:deriv_auth/features/single_entry/core/auth_data.dart'; import 'package:deriv_theme/deriv_theme.dart'; import 'package:deriv_ui/deriv_ui.dart'; import 'package:flutter/material.dart'; @@ -49,16 +52,31 @@ class _DerivCountrySelectionLayoutState late TextEditingController _textController; - late final DerivCountrySelectionCubit _countrySelectionCubit; - + DerivCountrySelectionCubit? _countrySelectionCubit; @override void initState() { super.initState(); - _countrySelectionCubit = DerivCountrySelectionCubit(widget.residences) - ..fetchResidenceCountries(); - _textController = TextEditingController(); + + _countrySelectionCubit = DerivCountrySelectionCubit(widget.residences); + _initializeResidences(); + } + + Future _initializeResidences() async { + try { + final List residencesList = await widget.residences; + + setState( + () { + _countrySelectionCubit = DerivCountrySelectionCubit( + Future>.value(residencesList), + )..fetchResidenceCountries(); + }, + ); + } on Exception catch (error) { + print('Error fetching residences: $error'); + } } @override @@ -154,7 +172,24 @@ class _DerivCountrySelectionLayoutState isConsentRequired: state.selectedCountryRequiresConsent, agreedToTerms: state.agreedToTerms, ), - onPressed: widget.onNextPressed, + onPressed: AuthData().data.signupPageModel.handleFlowFromPackage + ? () { + Navigator.pushReplacement>( + context, + MaterialPageRoute( + builder: (BuildContext context) => DerivSetPasswordLayout( + onDerivSignupState: (BuildContext BuildContext, + DerivSignupState DerivSignupState) {}, + onPreviousPressed: () {}, + residence: state.selectedCountry?.code, + verificationCode: widget.verificationCode, + affiliateToken: widget.affiliateToken, + ), + ), + ); + } + : widget.onNextPressed, child: Center( child: Text( context.derivAuthLocalization.actionNext, @@ -179,7 +214,7 @@ class _DerivCountrySelectionLayoutState countries: countries, onChanged: (int index) => setState( () { - _countrySelectionCubit.changeSelectedCountry( + _countrySelectionCubit!.changeSelectedCountry( selectedCountry: countries[index], ); }, @@ -207,7 +242,7 @@ class _DerivCountrySelectionLayoutState contentsVerticalAlignment: CrossAxisAlignment.start, value: state.agreedToTerms, onValueChanged: ({bool? isChecked}) => - _countrySelectionCubit.updateCountryConsentStatus( + _countrySelectionCubit!.updateCountryConsentStatus( agreedToTerms: isChecked, ), message: countryConsentMessage ?? @@ -275,7 +310,7 @@ class _DerivCountrySelectionLayoutState void dispose() { _focusNode.dispose(); - _countrySelectionCubit.close(); + _countrySelectionCubit!.close(); super.dispose(); } diff --git a/packages/deriv_auth/lib/features/signup/presentation/layouts/deriv_set_password_layout.dart b/packages/deriv_auth/lib/features/signup/presentation/layouts/deriv_set_password_layout.dart index d989c1306..7412f55ee 100644 --- a/packages/deriv_auth/lib/features/signup/presentation/layouts/deriv_set_password_layout.dart +++ b/packages/deriv_auth/lib/features/signup/presentation/layouts/deriv_set_password_layout.dart @@ -2,6 +2,7 @@ import 'package:deriv_auth/core/helpers/assets.dart'; import 'package:deriv_auth/core/states/auth_state_listener.dart'; import 'package:deriv_auth/deriv_auth.dart'; import 'package:deriv_auth/features/signup/presentation/widgets/password_policy_checker_widget.dart'; +import 'package:deriv_auth/features/single_entry/core/auth_data.dart'; import 'package:deriv_theme/deriv_theme.dart'; import 'package:deriv_ui/deriv_ui.dart'; import 'package:flutter/material.dart'; @@ -19,6 +20,7 @@ class DerivSetPasswordLayout extends StatefulWidget { this.authErrorStateHandler, this.utmModel, this.onAuthError, + this.affiliateToken, Key? key, }) : super(key: key); @@ -43,6 +45,9 @@ class DerivSetPasswordLayout extends StatefulWidget { /// Callback to be called when previous button is tapped. final VoidCallback onPreviousPressed; + /// Affiliate token. + final String? affiliateToken; + @override State createState() => _DerivSetPasswordLayoutState(); } @@ -125,17 +130,42 @@ class _DerivSetPasswordLayoutState extends State { children: [ Expanded(child: _buildPreviousButton()), const SizedBox(width: ThemeProvider.margin08), - Expanded( - child: BlocConsumer( - listener: widget.onDerivSignupState, - builder: (BuildContext context, DerivSignupState state) => - _buildStartTradingButton(state), + BlocConsumer( + listener: _onAuthState, + builder: (BuildContext context, DerivAuthState state) => Expanded( + child: BlocConsumer( + listener: + AuthData().data.signupPageModel.handleFlowFromPackage + ? (BuildContext context, DerivSignupState state) => + _onSignupState(state) + : widget.onDerivSignupState, + builder: (BuildContext context, DerivSignupState state) => + _buildStartTradingButton(state), + ), ), ), ], ), ); + void _onAuthState(BuildContext context, DerivAuthState state) { + if (state is DerivAuthLoggedInState) { + AuthData().data.loginPageModel.onLoggedIn.call(context, state); + } + } + + Future _onSignupState(DerivSignupState state) async { + if (state is DerivSignupErrorState) { + //TODO Handle state + } else if (state is DerivSignupDoneState) { + //TODO Handle state + + await context.read().tokenLogin( + state.account?.token ?? 'invalid_token', + ); + } + } + Widget _buildPasswordInput() => BaseTextField( focusNode: _passwordFocusNode, controller: _passwordTextController, @@ -178,7 +208,23 @@ class _DerivSetPasswordLayoutState extends State { ); Widget _buildPreviousButton() => SecondaryButton( - onPressed: widget.onPreviousPressed, + onPressed: AuthData().data.signupPageModel.handleFlowFromPackage + ? () { + Navigator.pushReplacement>( + context, + MaterialPageRoute( + builder: (BuildContext context) => + DerivCountrySelectionLayout( + affiliateToken: widget.affiliateToken, + onNextPressed: () {}, + verificationCode: widget.verificationCode!, + residences: AuthData().data.signupPageModel.residences, + ), + ), + ); + } + : widget.onPreviousPressed, child: Center( child: Text( context.derivAuthLocalization.actionPrevious, diff --git a/packages/deriv_auth/lib/features/signup/presentation/layouts/deriv_verification_done_layout.dart b/packages/deriv_auth/lib/features/signup/presentation/layouts/deriv_verification_done_layout.dart index 1e30b4649..8ac024235 100644 --- a/packages/deriv_auth/lib/features/signup/presentation/layouts/deriv_verification_done_layout.dart +++ b/packages/deriv_auth/lib/features/signup/presentation/layouts/deriv_verification_done_layout.dart @@ -1,5 +1,8 @@ import 'package:deriv_auth/core/extensions/context_extension.dart'; import 'package:deriv_auth/core/helpers/assets.dart'; +import 'package:deriv_auth/features/signup/models/deriv_residence_model.dart'; +import 'package:deriv_auth/features/signup/presentation/layouts/deriv_country_selection_layout.dart'; +import 'package:deriv_auth/features/single_entry/core/auth_data.dart'; import 'package:deriv_theme/deriv_theme.dart'; import 'package:deriv_ui/deriv_ui.dart'; import 'package:flutter/material.dart'; @@ -12,6 +15,7 @@ class DerivVerificationDoneLayout extends StatelessWidget { required this.verificationCode, required this.onContinuePressed, this.affiliateToken, + this.residences, Key? key, }) : super(key: key); @@ -24,6 +28,9 @@ class DerivVerificationDoneLayout extends StatelessWidget { /// Callback to be called when continue button is tapped. final VoidCallback onContinuePressed; + /// List of residences to be shown. + final Future>? residences; + @override Widget build(BuildContext context) => Scaffold( backgroundColor: context.theme.colors.primary, @@ -77,7 +84,28 @@ class DerivVerificationDoneLayout extends StatelessWidget { ); Widget _buildContinueButton(BuildContext context) => PrimaryButton( - onPressed: onContinuePressed, + onPressed: AuthData().data.signupPageModel.handleFlowFromPackage + ? () async { + final List residencesList = + await residences!; + + // Navigate to the new page once the residences are loaded + await Navigator.pushReplacement>( + context, + MaterialPageRoute( + builder: (BuildContext context) => + DerivCountrySelectionLayout( + affiliateToken: affiliateToken, + verificationCode: verificationCode, + onNextPressed: () {}, + residences: Future>.value( + residencesList), + ), + ), + ); + } + : onContinuePressed, child: Center( child: Text( context.derivAuthLocalization.actionContinue, diff --git a/packages/deriv_auth/lib/features/single_entry/core/models/signup_page_model.dart b/packages/deriv_auth/lib/features/single_entry/core/models/signup_page_model.dart index fcbfc3bf2..2d7543b5f 100644 --- a/packages/deriv_auth/lib/features/single_entry/core/models/signup_page_model.dart +++ b/packages/deriv_auth/lib/features/single_entry/core/models/signup_page_model.dart @@ -14,6 +14,8 @@ class SignupPageModel { required this.socialAuthStateHandler, required this.redirectURL, required this.onWebViewError, + required this.handleFlowFromPackage, + required this.residences, this.onSignupPressed, this.onSocialAuthButtonPressed, this.affiliateToken, @@ -52,4 +54,10 @@ class SignupPageModel { /// Callback to be called when social auth button is tapped. /// Give access to [SocialAuthDto] for 2FA. final SocialAuthCallback? onSocialAuthButtonPressed; + + /// Handle flow from package + final bool handleFlowFromPackage; + + /// List of residences to be shown. + final Future> residences; }