forked from eatvio/eatvio_app
feat(feed): Comment creation API and state update
This commit is contained in:
@@ -73,6 +73,27 @@ class BlogPostBloc extends Bloc<BlogPostEvent, BlogPostState> {
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _createComment(
|
||||
CreateComment event, Emitter<BlogPostState> emit) async {
|
||||
if (state is BlogPostLoadedState) {
|
||||
var currentState = state as BlogPostLoadedState;
|
||||
// Отправляем данные по 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']));
|
||||
var updatedState = currentState.copyWith(
|
||||
comments: updatedComments,
|
||||
commentController:
|
||||
TextEditingController(), // Clear the comment input field
|
||||
);
|
||||
emit(updatedState); // Emit the updated state to trigger a screen update
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FutureOr<void> _deleteEntry(
|
||||
DeleteEntry event, Emitter<BlogPostState> emit) async {
|
||||
final box = await Hive.openBox('token');
|
||||
@@ -150,25 +171,6 @@ class BlogPostBloc extends Bloc<BlogPostEvent, BlogPostState> {
|
||||
return list;
|
||||
}
|
||||
|
||||
FutureOr<void> _createComment(
|
||||
CreateComment event, Emitter<BlogPostState> emit) async {
|
||||
if (state is BlogPostLoadedState) {
|
||||
var currentState = state as BlogPostLoadedState;
|
||||
var updatedState = currentState.copyWith(
|
||||
commentController: TextEditingController(text: event.body),
|
||||
);
|
||||
|
||||
// Отправляем данные по HTTP маршруту и ждем ответ
|
||||
var response = await _sendCommentData(
|
||||
currentState.token, event.body, currentState.blog_post?['id']);
|
||||
print(response.body);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
emit(updatedState); // Передаем обновленное состояние в emit
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<http.Response> _sendCommentData(
|
||||
String? token, String body, int id) async {
|
||||
String url = dotenv.env['SERVER_ROOT']! + '/feed/createComment/$id';
|
||||
|
||||
@@ -34,6 +34,9 @@ class FeedBloc extends Bloc<FeedEvent, FeedState> {
|
||||
}
|
||||
final response = await http.get(
|
||||
Uri.parse(dotenv.env['SERVER_ROOT']! + '/feed/index'),
|
||||
headers: {
|
||||
'Authorization': 'Bearer $token',
|
||||
},
|
||||
);
|
||||
print('Got response');
|
||||
if (response.statusCode == 200) {
|
||||
|
||||
@@ -118,7 +118,6 @@ class FeedScreen extends StatelessWidget {
|
||||
itemCount: state.posts?.length ?? 0,
|
||||
itemBuilder: (context, index) {
|
||||
final post = state.posts?[index];
|
||||
print(post['author']['name']);
|
||||
return Container(
|
||||
margin: EdgeInsets.only(bottom: 10),
|
||||
decoration: BoxDecoration(
|
||||
@@ -161,7 +160,7 @@ class FeedScreen extends StatelessWidget {
|
||||
onTap: () => Navigator.pushNamed(
|
||||
context,
|
||||
AppRoutes.blogPostScreen,
|
||||
arguments: post?['id'].toString(),
|
||||
arguments: post?['post']['id'].toString(),
|
||||
),
|
||||
trailing: PopupMenuButton<String>(
|
||||
onSelected: (value) => print("tap"),
|
||||
|
||||
Reference in New Issue
Block a user