feat(comment): improve comment submission feedback and validation messages

This commit is contained in:
Vaibhav Surve 2025-07-08 13:17:21 +05:30
parent 77e27ff98e
commit a8c890a60d
2 changed files with 62 additions and 69 deletions

View File

@ -15,14 +15,15 @@ class AddCommentController extends GetxController {
Future<void> submitComment() async {
if (note.value.trim().isEmpty) {
showAppSnackbar(
title: "Validation",
message: "Comment cannot be empty.",
title: "Missing Comment",
message: "Please enter a comment before submitting.",
type: SnackbarType.warning,
);
return;
}
isSubmitting.value = true;
try {
logSafe("Submitting comment for contactId: $contactId");
@ -34,32 +35,30 @@ class AddCommentController extends GetxController {
if (success) {
logSafe("Comment added successfully.");
// Get the directory controller
// Refresh UI
final directoryController = Get.find<DirectoryController>();
// Fetch latest comments for the contact to refresh UI
await directoryController.fetchCommentsForContact(contactId);
Get.back(result: true);
showAppSnackbar(
title: "Success",
message: "Comment added successfully.",
title: "Comment Added",
message: "Your comment has been successfully added.",
type: SnackbarType.success,
);
} else {
logSafe("Comment submission failed", level: LogLevel.error);
showAppSnackbar(
title: "Error",
message: "Failed to add comment.",
title: "Submission Failed",
message: "Unable to add the comment. Please try again later.",
type: SnackbarType.error,
);
}
} catch (e) {
logSafe("Error while submitting comment: $e", level: LogLevel.error);
showAppSnackbar(
title: "Error",
message: "Something went wrong.",
title: "Unexpected Error",
message: "Something went wrong while adding your comment.",
type: SnackbarType.error,
);
} finally {

View File

@ -23,7 +23,6 @@ class _AddCommentBottomSheetState extends State<AddCommentBottomSheet> {
void initState() {
super.initState();
controller = Get.put(AddCommentController(contactId: widget.contactId));
// Initialize empty editor for new comment
quillController = quill.QuillController.basic();
}
@ -43,7 +42,11 @@ class _AddCommentBottomSheetState extends State<AddCommentBottomSheet> {
color: Theme.of(context).cardColor,
borderRadius: const BorderRadius.vertical(top: Radius.circular(24)),
boxShadow: const [
BoxShadow(color: Colors.black12, blurRadius: 12, offset: Offset(0, -2)),
BoxShadow(
color: Colors.black12,
blurRadius: 12,
offset: Offset(0, -2),
),
],
),
child: Padding(
@ -67,12 +70,11 @@ class _AddCommentBottomSheetState extends State<AddCommentBottomSheet> {
CommentEditorCard(
controller: quillController,
onCancel: () => Get.back(),
onSave: (controller) async {
final delta = controller.document.toDelta();
onSave: (editorController) async {
final delta = editorController.document.toDelta();
final htmlOutput = _convertDeltaToHtml(delta);
this.controller.updateNote(htmlOutput);
await this.controller.submitComment();
if (mounted) Get.back();
controller.updateNote(htmlOutput);
await controller.submitComment();
},
),
],
@ -81,62 +83,54 @@ class _AddCommentBottomSheetState extends State<AddCommentBottomSheet> {
),
);
}
}
String _convertDeltaToHtml(dynamic delta) {
final buffer = StringBuffer();
bool inList = false;
String _convertDeltaToHtml(dynamic delta) {
final buffer = StringBuffer();
bool inList = false;
for (var op in delta.toList()) {
final data = op.data?.toString() ?? '';
final attr = op.attributes ?? {};
for (var op in delta.toList()) {
final data = op.data?.toString() ?? '';
final attr = op.attributes ?? {};
final isListItem = attr.containsKey('list');
final isListItem = attr.containsKey('list');
final trimmedData = data.trim();
// Start <ul> if list item starts
if (isListItem && !inList) {
buffer.write('<ul>');
inList = true;
if (isListItem && !inList) {
buffer.write('<ul>');
inList = true;
}
if (!isListItem && inList) {
buffer.write('</ul>');
inList = false;
}
if (isListItem && trimmedData.isEmpty) continue;
if (isListItem) buffer.write('<li>');
if (attr.containsKey('bold')) buffer.write('<strong>');
if (attr.containsKey('italic')) buffer.write('<em>');
if (attr.containsKey('underline')) buffer.write('<u>');
if (attr.containsKey('strike')) buffer.write('<s>');
if (attr.containsKey('link')) buffer.write('<a href="${attr['link']}">');
buffer.write(trimmedData.replaceAll('\n', ''));
if (attr.containsKey('link')) buffer.write('</a>');
if (attr.containsKey('strike')) buffer.write('</s>');
if (attr.containsKey('underline')) buffer.write('</u>');
if (attr.containsKey('italic')) buffer.write('</em>');
if (attr.containsKey('bold')) buffer.write('</strong>');
if (isListItem) {
buffer.write('</li>');
} else if (data.contains('\n')) {
buffer.write('<br>');
}
}
// Close <ul> if list ended
if (!isListItem && inList) {
buffer.write('</ul>');
inList = false;
}
// Skip empty list items
final trimmedData = data.trim();
if (isListItem && trimmedData.isEmpty) {
// don't write empty <li>
continue;
}
if (isListItem) buffer.write('<li>');
if (attr.containsKey('bold')) buffer.write('<strong>');
if (attr.containsKey('italic')) buffer.write('<em>');
if (attr.containsKey('underline')) buffer.write('<u>');
if (attr.containsKey('strike')) buffer.write('<s>');
if (attr.containsKey('link')) buffer.write('<a href="${attr['link']}">');
// Use trimmedData instead of raw data (removes trailing/leading spaces/newlines)
buffer.write(trimmedData.replaceAll('\n', ''));
if (attr.containsKey('link')) buffer.write('</a>');
if (attr.containsKey('strike')) buffer.write('</s>');
if (attr.containsKey('underline')) buffer.write('</u>');
if (attr.containsKey('italic')) buffer.write('</em>');
if (attr.containsKey('bold')) buffer.write('</strong>');
if (isListItem) {
buffer.write('</li>');
} else if (data.contains('\n')) {
buffer.write('<br>');
}
if (inList) buffer.write('</ul>');
return buffer.toString();
}
if (inList) buffer.write('</ul>');
return buffer.toString();
}