Verified the real contracts against the backend before changing anything.
Chat (three defects, one root cause):
- every chat endpoint is keyed by the chat id, not the service id. The app
called POST /chat/start, threw away the id it returned and kept using the
service id, so every later request 404'd.
- messages arrive as {data, meta}; reading the body as a bare list threw and
surfaced as an empty conversation.
- the bloc created the chat and then never emitted ChatLoaded (the else hung
off `if (chat == null)`), leaving a permanent spinner. Sending a message
emitted nothing at all, so it vanished until reopening.
Messages now render optimistically and roll back if the send fails, and the
screen distinguishes "loading" from "could not open" with a retry.
Appointments:
- a null range1_hour2 parsed as 00:00, so new appointments were born
"Caducado" and every action was hidden. It now falls back to the start time.
- service requests validate the HTTP status and tolerate an empty body: a 4xx
was treated as success and a 204 as failure.
- creating a service with no id in the response no longer reports success and
navigates to a service that does not exist.
- dispatching LoadService from build() looped forever on failure; the three
detail screens now load once and offer a retry.
Ratings:
- both sides read userScored, so only one of the two could ever rate. The
client side now reads professionalScored.
- the screen closed before the request finished, killing the provider mid
flight while addComment swallowed every error. It now waits for confirmation.
- score/reputation parsing tolerates integers and numeric strings instead of
emptying the review list.
Also: guarded map lookups in ScoreBloc, a nullable name in the search list,
and error states with retry where a failure used to shimmer forever.
Verified against the backend: `accepted` is the status the API expects, so the
suspected spelling bug was a false alarm and was left alone.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
78 lines
2.4 KiB
Dart
78 lines
2.4 KiB
Dart
import 'dart:developer';
|
|
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:chat_repository/chat_repository.dart';
|
|
|
|
part 'chat_event.dart';
|
|
part 'chat_state.dart';
|
|
|
|
class ChatBloc extends Bloc<ChatEvent, ChatState> {
|
|
final ApiChatRepository _chatRepository;
|
|
|
|
/// Resolved once per conversation. Every chat endpoint is keyed by the chat
|
|
/// id, which the app used to confuse with the service id.
|
|
String? _chatId;
|
|
|
|
ChatBloc({
|
|
required ApiChatRepository chatRepository,
|
|
}) : _chatRepository = chatRepository,
|
|
super(ChatInitial()) {
|
|
on<LoadChatEvent>(_onLoadChatEvent);
|
|
on<SendMessageEvent>(_onSendMessageEvent);
|
|
}
|
|
|
|
void _onLoadChatEvent(LoadChatEvent event, Emitter<ChatState> emit) async {
|
|
emit(ChatLoading());
|
|
try {
|
|
final chat = await _chatRepository.loadConversation(event.professionalId);
|
|
_chatId = chat.id;
|
|
emit(ChatLoaded(chat: chat));
|
|
} catch (e) {
|
|
log(e.toString());
|
|
emit(ChatFailure());
|
|
}
|
|
}
|
|
|
|
void _onSendMessageEvent(
|
|
SendMessageEvent event, Emitter<ChatState> emit) async {
|
|
final current = state;
|
|
// Show the message immediately; the reload below reconciles with the
|
|
// backend. Previously nothing was emitted at all and the message vanished.
|
|
if (current is ChatLoaded) {
|
|
emit(ChatLoaded(
|
|
chat: ChatEntity(
|
|
id: current.chat.id,
|
|
userId: current.chat.userId,
|
|
professionalId: current.chat.professionalId,
|
|
messages: [...current.chat.messages, event.message],
|
|
),
|
|
));
|
|
}
|
|
|
|
try {
|
|
final chatId = _chatId;
|
|
if (chatId == null || chatId.isEmpty) {
|
|
throw Exception('El chat aún no está abierto');
|
|
}
|
|
await _chatRepository.sendMessage(chatId, event.message);
|
|
|
|
final messages = await _chatRepository.fetchMessages(chatId);
|
|
emit(ChatLoaded(
|
|
chat: ChatEntity(
|
|
id: chatId,
|
|
userId: current is ChatLoaded ? current.chat.userId : '',
|
|
professionalId:
|
|
current is ChatLoaded ? current.chat.professionalId : '',
|
|
messages: messages,
|
|
),
|
|
));
|
|
} catch (e) {
|
|
log(e.toString());
|
|
// Roll the optimistic message back so nobody believes it was delivered.
|
|
if (current is ChatLoaded) emit(ChatLoaded(chat: current.chat));
|
|
emit(SendMessageFailure());
|
|
}
|
|
}
|
|
}
|