From 8c0021f4139e7b4a611245297835245934df9abd Mon Sep 17 00:00:00 2001 From: maxxkim Date: Thu, 23 May 2024 12:08:55 +0300 Subject: [PATCH] feat(feed):= Front-end validation and improved layouts for BloGPosts --- .../bloc/blog_post_create_bloc.dart | 8 +- .../bloc/blog_post_create_state.dart | 20 ++- .../blog_post_create_screen.dart | 164 ++++++++++++------ .../blog_post_screen/bloc/blog_post_bloc.dart | 3 +- .../feed/feed_screen/feed_screen.dart | 8 +- 5 files changed, 140 insertions(+), 63 deletions(-) diff --git a/lib/presentation/feed/blog_post_create_screen/bloc/blog_post_create_bloc.dart b/lib/presentation/feed/blog_post_create_screen/bloc/blog_post_create_bloc.dart index fee6cd51..e07d2dc6 100644 --- a/lib/presentation/feed/blog_post_create_screen/bloc/blog_post_create_bloc.dart +++ b/lib/presentation/feed/blog_post_create_screen/bloc/blog_post_create_bloc.dart @@ -36,9 +36,11 @@ class BlogPostCreateBloc blog_postModelObj: currentState.blog_postModelObj, title: currentState.title, description: currentState.description, - youtube_url: currentState.youtube_url)); + youtube_url: currentState.youtube_url, + token: currentState.token)); } else { emit(BlogPostCreateLoadedState( + token: token, title: TextEditingController(), description: TextEditingController(), youtube_url: TextEditingController())); @@ -71,7 +73,8 @@ class BlogPostCreateBloc String? description, ) async { String url = '${dotenv.env['SERVER_ROOT']!}/feed/create'; - + final box = await Hive.openBox('token'); + final token = box.get('token'); final Map credentials = { 'title': title, 'description': description, @@ -83,6 +86,7 @@ class BlogPostCreateBloc Uri.parse(url), headers: { 'Content-Type': 'application/json', + 'Authorization': 'Bearer $token', }, body: json.encode(credentials), encoding: utf8, diff --git a/lib/presentation/feed/blog_post_create_screen/bloc/blog_post_create_state.dart b/lib/presentation/feed/blog_post_create_screen/bloc/blog_post_create_state.dart index 0cc3c405..de3bab48 100644 --- a/lib/presentation/feed/blog_post_create_screen/bloc/blog_post_create_state.dart +++ b/lib/presentation/feed/blog_post_create_screen/bloc/blog_post_create_state.dart @@ -23,26 +23,32 @@ class BlogPostCreateState extends Equatable { class BlogPostCreateLoadedState extends BlogPostCreateState { BlogPostCreateLoadedState( - {this.blog_postModelObj, this.title, this.description, this.youtube_url}); + {this.token, + this.blog_postModelObj, + this.title, + this.description, + this.youtube_url}); BlogPostCreateModel? blog_postModelObj; TextEditingController? title; TextEditingController? description; TextEditingController? youtube_url; + String? token; @override List get props => [blog_postModelObj, title, description, youtube_url]; - BlogPostCreateLoadedState copyWith({ - BlogPostCreateModel? blog_postModelObj, - TextEditingController? title, - TextEditingController? description, - TextEditingController? youtube_url, - }) { + BlogPostCreateLoadedState copyWith( + {BlogPostCreateModel? blog_postModelObj, + TextEditingController? title, + TextEditingController? description, + TextEditingController? youtube_url, + String? token}) { return BlogPostCreateLoadedState( blog_postModelObj: blog_postModelObj ?? this.blog_postModelObj, title: title ?? this.title, description: description ?? this.description, youtube_url: youtube_url ?? this.youtube_url, + token: token ?? this.token, ); } } diff --git a/lib/presentation/feed/blog_post_create_screen/blog_post_create_screen.dart b/lib/presentation/feed/blog_post_create_screen/blog_post_create_screen.dart index 6950af55..fc1d7c14 100644 --- a/lib/presentation/feed/blog_post_create_screen/blog_post_create_screen.dart +++ b/lib/presentation/feed/blog_post_create_screen/blog_post_create_screen.dart @@ -3,6 +3,8 @@ import 'models/blog_post_create_model.dart'; import 'package:eatvio_app/core/app_export.dart'; import 'package:flutter/material.dart'; import 'package:eatvio_app/widgets/custom_bottom_bar.dart'; +import 'package:eatvio_app/widgets/custom_text_form_field.dart'; +import 'package:eatvio_app/widgets/custom_outlined_button.dart'; class BlogPostCreateScreen extends StatelessWidget { const BlogPostCreateScreen({Key? key}) : super(key: key); @@ -44,61 +46,123 @@ class BlogPostCreateScreen extends StatelessWidget { child: Scaffold( resizeToAvoidBottomInset: false, body: Container( - width: double.maxFinite, - decoration: AppDecoration.fillGray, - child: ListView(children: [ - SizedBox(height: 30.v), - Container( - margin: EdgeInsets.only( - left: 16.h, - right: 20.h, - bottom: 10.h, - ), - padding: EdgeInsets.all(12.h), - decoration: BoxDecoration( - color: Colors.white, - borderRadius: const BorderRadius.all(Radius.circular(12)), - boxShadow: [ - BoxShadow( - color: Colors.grey.withOpacity(0.5), - spreadRadius: 2, - blurRadius: 5, - offset: Offset(0, 3), + width: double.maxFinite, + decoration: AppDecoration.fillGray, + child: ListView(children: [ + SizedBox(height: 30.v), + Container( + margin: EdgeInsets.only( + left: 16.h, + right: 20.h, + bottom: 10.h, ), - ], - ), - child: Column(mainAxisSize: MainAxisSize.min, children: [ - SizedBox(height: 16.0), - TextFormField( - controller: state.title, - decoration: InputDecoration(labelText: 'Заголовок'), - ), - SizedBox(height: 16.0), - TextFormField( - controller: state.description, - decoration: InputDecoration(labelText: 'Описание'), - ), - SizedBox(height: 16.0), - TextFormField( - controller: state.youtube_url, - decoration: InputDecoration(labelText: 'Ссылка'), - ), - SizedBox(height: 16.0), - // Здесь нужно добавить виджет для загрузки картинки - ElevatedButton( - onPressed: () => context - .read() - .add(CreateBlogPost()), - child: Text('Создать'), - ) - ]), - ) - ]), - ), + padding: EdgeInsets.all(12.h), + decoration: BoxDecoration( + color: Colors.white, + borderRadius: + const BorderRadius.all(Radius.circular(12)), + boxShadow: [ + BoxShadow( + color: Colors.grey.withOpacity(0.5), + spreadRadius: 2, + blurRadius: 5, + offset: Offset(0, 3), + ), + ], + ), + child: Column(mainAxisSize: MainAxisSize.min, children: [ + Column( + children: [ + Text("Заголовок новости", + style: theme.textTheme.bodyLarge), + CustomTextFormField( + controller: state.title, + validator: _validateTitle, + ), + SizedBox(height: 8.0), + Text("Ссылка на YouTube", + style: theme.textTheme.bodyLarge), + CustomTextFormField( + controller: state.youtube_url, + validator: _validateYouTubeLink, + ), + SizedBox(height: 8.0), + Text("Описание", style: theme.textTheme.bodyLarge), + CustomTextFormField( + controller: state.description, + validator: _validateDescription, + maxLines: 6, + ), + SizedBox(height: 16.0), + // Add widget for image upload here + CustomOutlinedButton( + onPressed: () => _submitForm( + context, + state.title!.text, + state.description!.text, + state.youtube_url!.text), + buttonStyle: + CustomButtonStyles.outlinePrimaryTL15, + text: 'Создать', + ), + ], + ), + ])) + ])), bottomNavigationBar: _buildBottomBar(context))); } } +_submitForm( + BuildContext context, String title, String description, String youtubeURL) { + String? titleError = _validateTitle(title); + String? descriptionError = _validateDescription(description); + String? youtubeURLError = _validateYouTubeLink(youtubeURL); + + if (titleError != null || + descriptionError != null || + youtubeURLError != null) { + // Display error messages or handle invalid input as needed + // For example, show a snackbar with the error message + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(titleError ?? descriptionError ?? youtubeURLError!)), + ); + } else { + // Proceed with form submission + context.read().add(CreateBlogPost()); + } +} + +String? _validateTitle(value) { + if (value!.isEmpty) { + return 'Пожалуйста, введите заголовок'; + } + return null; +} + +String? _validateDescription(value) { + if (value!.isEmpty) { + return 'Пожалуйста, введите описание'; + } + return null; +} + +String? _validateYouTubeLink(value) { + if (value!.isEmpty) { + return 'Пожалуйста, введите ссылку'; + } else if (!_isValidUrl(value)) { + return 'Пожалуйста, введите корректную ссылку'; + } + return null; +} + +bool _isValidUrl(String url) { + final RegExp urlRegExp = + new RegExp(r"^(http|https):\/\/([\w-]+\.)+[\w-]+(\/[\w-./?%&=]*)?$"); + return urlRegExp.hasMatch(url); +} + _buildBottomBar(BuildContext context) { return CustomBottomBar(onChanged: (BottomBarEnum type) {}); } diff --git a/lib/presentation/feed/blog_post_screen/bloc/blog_post_bloc.dart b/lib/presentation/feed/blog_post_screen/bloc/blog_post_bloc.dart index e1a98497..d0655339 100644 --- a/lib/presentation/feed/blog_post_screen/bloc/blog_post_bloc.dart +++ b/lib/presentation/feed/blog_post_screen/bloc/blog_post_bloc.dart @@ -80,7 +80,6 @@ class BlogPostBloc extends Bloc { // Отправляем данные по HTTP маршруту и ждем ответ var response = await _sendCommentData( currentState.token, event.body, currentState.blog_post?['id']); - if (response.statusCode == 200) { var updatedComments = fillCommentList( await _fetchComments(currentState.blog_post?['id'])); @@ -159,7 +158,6 @@ class BlogPostBloc extends Bloc { List fillCommentList(List comments) { List list = []; for (int i = 0; i < comments.length; i++) { - print(comments[i]); list.add(CommentModel( id: comments[i]['id'].toString(), userImage: comments[i]['profile_photo_path'], @@ -175,6 +173,7 @@ class BlogPostBloc extends Bloc { String? token, String body, int id) async { String url = dotenv.env['SERVER_ROOT']! + '/feed/createComment/$id'; final Map credentials = { + 'id': id, 'body': body, }; diff --git a/lib/presentation/feed/feed_screen/feed_screen.dart b/lib/presentation/feed/feed_screen/feed_screen.dart index a7cd48fe..d5d25004 100644 --- a/lib/presentation/feed/feed_screen/feed_screen.dart +++ b/lib/presentation/feed/feed_screen/feed_screen.dart @@ -54,7 +54,7 @@ class FeedScreen extends StatelessWidget { crossAxisAlignment: CrossAxisAlignment.stretch, children: [ SizedBox(height: 20), - _buildSearch(), + _buildSearch(context), SizedBox(height: 20), _buildMenu(context, state), SizedBox(height: 20), @@ -69,7 +69,7 @@ class FeedScreen extends StatelessWidget { ); } - _buildSearch() { + _buildSearch(BuildContext context) { return Container( margin: EdgeInsets.only(bottom: 10), decoration: BoxDecoration( @@ -106,6 +106,10 @@ class FeedScreen extends StatelessWidget { flex: 1, child: CustomOutlinedButton( text: "+", + onPressed: () => Navigator.pushNamed( + context, + AppRoutes.blogPostCreateScreen, + ), buttonStyle: CustomButtonStyles.outlinePrimaryTL15, ), ),