From fc8301bbe11f4cfdafaef96d8d3be190d37f6911 Mon Sep 17 00:00:00 2001 From: maxxkim Date: Wed, 15 May 2024 14:49:19 +0300 Subject: [PATCH] feat(feed): Fetching author and comments for blog posts --- .../blog_post_screen/bloc/blog_post_bloc.dart | 36 ++++++++----------- .../bloc/blog_post_state.dart | 4 +-- 2 files changed, 16 insertions(+), 24 deletions(-) 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 7c9f9ab3..3c136862 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 @@ -49,8 +49,11 @@ class BlogPostBloc extends Bloc { comments: currentState.comments)); } else { emit(BlogPostLoadedState(blog_post: data)); - Future> author = _fetchAuthor(data['id']); + final author = await _fetchAuthor(data['id']); emit(BlogPostLoadedState(blog_post: data, author: author)); + final comments = [await _fetchComments(data['id'])]; + emit(BlogPostLoadedState( + blog_post: data, author: author, comments: comments)); } } else { // Один или несколько ключевых полей отсутствуют, обрабатываем ошибку @@ -90,7 +93,7 @@ class BlogPostBloc extends Bloc { } } - Future> _fetchAuthor(int id) async { + _fetchAuthor(int id) async { final box = await Hive.openBox('token'); final token = box.get('token'); @@ -100,18 +103,19 @@ class BlogPostBloc extends Bloc { ); } - final response = await http.post( + final response = await http.get( Uri.parse(dotenv.env['SERVER_ROOT']! + '/feed/getAuthor/$id'), headers: { 'Authorization': 'Bearer $token', }, ); - + print(response.body); final data = jsonDecode(response.body); + print(data); return data; } - FutureOr _fetchComments(int id) async { + _fetchComments(int id) async { final box = await Hive.openBox('token'); final token = box.get('token'); if (token == null) { @@ -119,27 +123,15 @@ class BlogPostBloc extends Bloc { AppRoutes.authScreen, ); } - final response = await http.post( + final response = await http.get( Uri.parse(dotenv.env['SERVER_ROOT']! + '/feed/getComments/$id'), headers: { 'Authorization': 'Bearer $token', }, ); - if (response.statusCode == 200) { - final data = jsonDecode(response.body); - if (data is List>) { - print(data); - var currentState = state as BlogPostLoadedState; - emit(currentState.copyWith( - blog_postModelObj: currentState.blog_postModelObj, - blog_post: currentState.blog_post, - author: currentState.author, - comments: currentState.comments)); - } else { - emit(BlogPostErrorState('Неверный формат комментариев')); - } - } else { - emit(BlogPostErrorState('Ошибка сервера: ${response.statusCode}')); - } + print(response.body); + final data = jsonDecode(response.body); + print(data); + return data; } } diff --git a/lib/presentation/feed/blog_post_screen/bloc/blog_post_state.dart b/lib/presentation/feed/blog_post_screen/bloc/blog_post_state.dart index d95856a0..610b528b 100644 --- a/lib/presentation/feed/blog_post_screen/bloc/blog_post_state.dart +++ b/lib/presentation/feed/blog_post_screen/bloc/blog_post_state.dart @@ -28,14 +28,14 @@ class BlogPostLoadedState extends BlogPostState { BlogPostModel? blog_postModelObj; Map? blog_post; Map? author; - List?>? comments; + List? comments; @override List get props => [blog_postModelObj, blog_post, author, comments]; BlogPostLoadedState copyWith( {BlogPostModel? blog_postModelObj, Map? blog_post, Map? author, - List?>? comments}) { + List? comments}) { return BlogPostLoadedState( blog_postModelObj: blog_postModelObj ?? this.blog_postModelObj, blog_post: blog_post ?? this.blog_post,