forked from eatvio/eatvio_app
feature(registration): iter 7
This commit is contained in:
@@ -1,7 +1,10 @@
|
||||
import 'package:eatvio_app/app_router.dart';
|
||||
import 'package:eatvio_app/core/app_export.dart';
|
||||
import 'package:eatvio_app/core/token_storage.dart';
|
||||
import 'package:eatvio_app/data/models/userRegistrationModel/user_registration_model.dart';
|
||||
import 'package:eatvio_app/features/registration/presentation/bloc/registration_cubit/registration_state.dart';
|
||||
import 'package:eatvio_app/features/registration/repositories/registration_repository.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
class RegistrationCubit extends Cubit<RegistrationState> {
|
||||
final RegistrationRepository registrationRepository;
|
||||
@@ -42,12 +45,13 @@ class RegistrationCubit extends Cubit<RegistrationState> {
|
||||
|
||||
updateSheet(UserRegistrationSheet sheet) => emit(RegistrationState(sheet: sheet));
|
||||
|
||||
tryRegistration() async {
|
||||
Future<void> tryRegistration() async {
|
||||
try {
|
||||
final response = await registrationRepository.tryRegister(state.sheet);
|
||||
print(response);
|
||||
final token = await registrationRepository.tryRegister(state.sheet);
|
||||
await TokenStorage.saveTokenInStorage(token);
|
||||
AppRouter.navigatorKey.currentContext?.go('/my-profile-screen');
|
||||
} catch (e) {
|
||||
print(e);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,12 +19,15 @@ mixin _$RegistrationState {
|
||||
UserRegistrationSheet get sheet => throw _privateConstructorUsedError;
|
||||
|
||||
@JsonKey(ignore: true)
|
||||
$RegistrationStateCopyWith<RegistrationState> get copyWith => throw _privateConstructorUsedError;
|
||||
$RegistrationStateCopyWith<RegistrationState> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class $RegistrationStateCopyWith<$Res> {
|
||||
factory $RegistrationStateCopyWith(RegistrationState value, $Res Function(RegistrationState) then) = _$RegistrationStateCopyWithImpl<$Res, RegistrationState>;
|
||||
factory $RegistrationStateCopyWith(
|
||||
RegistrationState value, $Res Function(RegistrationState) then) =
|
||||
_$RegistrationStateCopyWithImpl<$Res, RegistrationState>;
|
||||
@useResult
|
||||
$Res call({UserRegistrationSheet sheet});
|
||||
|
||||
@@ -32,7 +35,8 @@ abstract class $RegistrationStateCopyWith<$Res> {
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class _$RegistrationStateCopyWithImpl<$Res, $Val extends RegistrationState> implements $RegistrationStateCopyWith<$Res> {
|
||||
class _$RegistrationStateCopyWithImpl<$Res, $Val extends RegistrationState>
|
||||
implements $RegistrationStateCopyWith<$Res> {
|
||||
_$RegistrationStateCopyWithImpl(this._value, this._then);
|
||||
|
||||
// ignore: unused_field
|
||||
@@ -63,8 +67,10 @@ class _$RegistrationStateCopyWithImpl<$Res, $Val extends RegistrationState> impl
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
abstract class _$$RegistrationStateImplCopyWith<$Res> implements $RegistrationStateCopyWith<$Res> {
|
||||
factory _$$RegistrationStateImplCopyWith(_$RegistrationStateImpl value, $Res Function(_$RegistrationStateImpl) then) =
|
||||
abstract class _$$RegistrationStateImplCopyWith<$Res>
|
||||
implements $RegistrationStateCopyWith<$Res> {
|
||||
factory _$$RegistrationStateImplCopyWith(_$RegistrationStateImpl value,
|
||||
$Res Function(_$RegistrationStateImpl) then) =
|
||||
__$$RegistrationStateImplCopyWithImpl<$Res>;
|
||||
@override
|
||||
@useResult
|
||||
@@ -75,9 +81,12 @@ abstract class _$$RegistrationStateImplCopyWith<$Res> implements $RegistrationSt
|
||||
}
|
||||
|
||||
/// @nodoc
|
||||
class __$$RegistrationStateImplCopyWithImpl<$Res> extends _$RegistrationStateCopyWithImpl<$Res, _$RegistrationStateImpl>
|
||||
class __$$RegistrationStateImplCopyWithImpl<$Res>
|
||||
extends _$RegistrationStateCopyWithImpl<$Res, _$RegistrationStateImpl>
|
||||
implements _$$RegistrationStateImplCopyWith<$Res> {
|
||||
__$$RegistrationStateImplCopyWithImpl(_$RegistrationStateImpl _value, $Res Function(_$RegistrationStateImpl) _then) : super(_value, _then);
|
||||
__$$RegistrationStateImplCopyWithImpl(_$RegistrationStateImpl _value,
|
||||
$Res Function(_$RegistrationStateImpl) _then)
|
||||
: super(_value, _then);
|
||||
|
||||
@pragma('vm:prefer-inline')
|
||||
@override
|
||||
@@ -109,7 +118,9 @@ class _$RegistrationStateImpl implements _RegistrationState {
|
||||
@override
|
||||
bool operator ==(Object other) {
|
||||
return identical(this, other) ||
|
||||
(other.runtimeType == runtimeType && other is _$RegistrationStateImpl && (identical(other.sheet, sheet) || other.sheet == sheet));
|
||||
(other.runtimeType == runtimeType &&
|
||||
other is _$RegistrationStateImpl &&
|
||||
(identical(other.sheet, sheet) || other.sheet == sheet));
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -118,15 +129,19 @@ class _$RegistrationStateImpl implements _RegistrationState {
|
||||
@JsonKey(ignore: true)
|
||||
@override
|
||||
@pragma('vm:prefer-inline')
|
||||
_$$RegistrationStateImplCopyWith<_$RegistrationStateImpl> get copyWith => __$$RegistrationStateImplCopyWithImpl<_$RegistrationStateImpl>(this, _$identity);
|
||||
_$$RegistrationStateImplCopyWith<_$RegistrationStateImpl> get copyWith =>
|
||||
__$$RegistrationStateImplCopyWithImpl<_$RegistrationStateImpl>(
|
||||
this, _$identity);
|
||||
}
|
||||
|
||||
abstract class _RegistrationState implements RegistrationState {
|
||||
const factory _RegistrationState({required final UserRegistrationSheet sheet}) = _$RegistrationStateImpl;
|
||||
const factory _RegistrationState(
|
||||
{required final UserRegistrationSheet sheet}) = _$RegistrationStateImpl;
|
||||
|
||||
@override
|
||||
UserRegistrationSheet get sheet;
|
||||
@override
|
||||
@JsonKey(ignore: true)
|
||||
_$$RegistrationStateImplCopyWith<_$RegistrationStateImpl> get copyWith => throw _privateConstructorUsedError;
|
||||
_$$RegistrationStateImplCopyWith<_$RegistrationStateImpl> get copyWith =>
|
||||
throw _privateConstructorUsedError;
|
||||
}
|
||||
|
||||
@@ -113,8 +113,8 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
backgroundColor: appTheme.blueA400,
|
||||
onPressed: () {
|
||||
if (canGoNext(state)) {
|
||||
onPressed: () async {
|
||||
if (await canGoNext(state)) {
|
||||
setState(() {
|
||||
pageController.nextPage(
|
||||
duration: const Duration(milliseconds: 350),
|
||||
@@ -197,7 +197,7 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
bool canGoNext(state) {
|
||||
Future<bool> canGoNext(state) async {
|
||||
switch (step) {
|
||||
case 2:
|
||||
List<String> errors = validateCredentialsFields(state.sheet.email, state.sheet.name, state.sheet.password, state.sheet.passwordConfirmation);
|
||||
@@ -285,8 +285,30 @@ class _RegistrationScreenState extends State<RegistrationScreen> {
|
||||
return false;
|
||||
}
|
||||
case 15:
|
||||
registrationCubit.tryRegistration();
|
||||
return true;
|
||||
try {
|
||||
showDialog(
|
||||
barrierDismissible: false,
|
||||
useSafeArea: false,
|
||||
context: context,
|
||||
builder: (context) => PopScope(
|
||||
canPop: false,
|
||||
child: Center(
|
||||
child: CircularProgressIndicator(),
|
||||
),
|
||||
),
|
||||
);
|
||||
await registrationCubit.tryRegistration();
|
||||
Navigator.pop(context);
|
||||
} catch (e) {
|
||||
Navigator.pop(context);
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: Text('Ошибка при попытке регистрации'),
|
||||
),
|
||||
);
|
||||
}
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -6,9 +6,9 @@ class RegistrationRepository {
|
||||
|
||||
RegistrationRepository({required this.registrationService});
|
||||
|
||||
tryRegister(UserRegistrationSheet sheet) async {
|
||||
Future<String> tryRegister(UserRegistrationSheet sheet) async {
|
||||
try {
|
||||
return await registrationService.register(sheet: sheet.copyWith(country: 'Россия', city: 'Москва').toJson());
|
||||
return (await registrationService.register(sheet: sheet.toJson()))['token']!;
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,7 @@ abstract class RegistrationService {
|
||||
factory RegistrationService(Dio dio, {String baseUrl}) = _RegistrationService;
|
||||
|
||||
@POST('/api/register')
|
||||
Future<void> register({
|
||||
Future<Map<String, String>> register({
|
||||
@Body() required Map<String, dynamic> sheet,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -19,32 +19,38 @@ class _RegistrationService implements RegistrationService {
|
||||
String? baseUrl;
|
||||
|
||||
@override
|
||||
Future<void> register({required Map<String, dynamic> sheet}) async {
|
||||
Future<Map<String, String>> register(
|
||||
{required Map<String, dynamic> sheet}) async {
|
||||
final _extra = <String, dynamic>{};
|
||||
final queryParameters = <String, dynamic>{};
|
||||
final _headers = <String, dynamic>{};
|
||||
final _data = <String, dynamic>{};
|
||||
_data.addAll(sheet);
|
||||
await _dio.fetch<void>(_setStreamType<void>(Options(
|
||||
final _result = await _dio.fetch<Map<String, dynamic>>(
|
||||
_setStreamType<Map<String, String>>(Options(
|
||||
method: 'POST',
|
||||
headers: _headers,
|
||||
extra: _extra,
|
||||
)
|
||||
.compose(
|
||||
_dio.options,
|
||||
'/api/register',
|
||||
queryParameters: queryParameters,
|
||||
data: _data,
|
||||
)
|
||||
.copyWith(
|
||||
baseUrl: _combineBaseUrls(
|
||||
_dio.options.baseUrl,
|
||||
baseUrl,
|
||||
))));
|
||||
.compose(
|
||||
_dio.options,
|
||||
'/api/register',
|
||||
queryParameters: queryParameters,
|
||||
data: _data,
|
||||
)
|
||||
.copyWith(
|
||||
baseUrl: _combineBaseUrls(
|
||||
_dio.options.baseUrl,
|
||||
baseUrl,
|
||||
))));
|
||||
final _value = _result.data!.cast<String, String>();
|
||||
return _value;
|
||||
}
|
||||
|
||||
RequestOptions _setStreamType<T>(RequestOptions requestOptions) {
|
||||
if (T != dynamic && !(requestOptions.responseType == ResponseType.bytes || requestOptions.responseType == ResponseType.stream)) {
|
||||
if (T != dynamic &&
|
||||
!(requestOptions.responseType == ResponseType.bytes ||
|
||||
requestOptions.responseType == ResponseType.stream)) {
|
||||
if (T == String) {
|
||||
requestOptions.responseType = ResponseType.plain;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user