submitOfflineForm method
Offline pre-chat form: posts the first message with name and email.
Implementation
Future<void> submitOfflineForm({String? name, String? email, String? message}) async {
final token = _token;
if (token == null) return;
final text = (message ?? '').trim().isEmpty ? (_state.draft ?? '') : message!.trim();
if (text.isEmpty) return;
try {
final r = await _api.offlineMessage(token: token, message: text, name: name, email: email);
final sameConversation = _state.conversation?.id == r.conversation.id;
final cleanEmail = (email ?? '').trim();
final cleanName = (name ?? '').trim();
_emit(
_state.copyWith(
conversation: r.conversation,
needsEmail: false,
clearDraft: true,
visitorEmail: cleanEmail.isEmpty ? null : cleanEmail,
visitorName: cleanName.isEmpty ? null : cleanName,
timeline: sameConversation ? _state.timeline : Timeline.empty,
),
);
await _persist(
_stored.copyWith(
conversationId: r.conversation.id,
email: cleanEmail.isEmpty ? null : cleanEmail,
name: cleanName.isEmpty ? null : cleanName,
),
);
_send(ClientFrame.resume(conversationId: r.conversation.id, afterSeq: 0));
_addLocalNotice('emailSaved');
_event(const HatangConversion(HatangConversionKind.chatStarted));
} on HatangException catch (e) {
_emit(_state.copyWith(transientError: e));
}
}