feat: Simplify document upload by removing initial data handling and existing file name references

This commit is contained in:
Vaibhav Surve 2025-09-05 16:22:05 +05:30
parent e12e5ab13b
commit 4d11a2ccf0

View File

@ -13,12 +13,10 @@ import 'package:marco/helpers/widgets/my_snackbar.dart';
class DocumentUploadBottomSheet extends StatefulWidget { class DocumentUploadBottomSheet extends StatefulWidget {
final Function(Map<String, dynamic>) onSubmit; final Function(Map<String, dynamic>) onSubmit;
final Map<String, dynamic>? initialData;
const DocumentUploadBottomSheet({ const DocumentUploadBottomSheet({
Key? key, Key? key,
required this.onSubmit, required this.onSubmit,
this.initialData,
}) : super(key: key); }) : super(key: key);
@override @override
@ -35,55 +33,6 @@ class _DocumentUploadBottomSheetState extends State<DocumentUploadBottomSheet> {
final TextEditingController _descriptionController = TextEditingController(); final TextEditingController _descriptionController = TextEditingController();
File? selectedFile; File? selectedFile;
String? existingFileName;
@override
void initState() {
super.initState();
// Pre-fill if editing
if (widget.initialData != null) {
final data = widget.initialData!;
_docIdController.text = data["documentId"] ?? "";
_docNameController.text = data["name"] ?? "";
_descriptionController.text = data["description"] ?? "";
existingFileName = data["fileName"];
// Preselect category & type
WidgetsBinding.instance.addPostFrameCallback((_) async {
if (data["categoryName"] != null) {
final category = controller.categories.firstWhereOrNull(
(c) => c.name == data["categoryName"],
);
if (category != null) {
setState(() => controller.selectedCategory = category);
await controller.fetchDocumentTypes(category.id);
if (data["documentTypeName"] != null) {
final type = controller.documentTypes.firstWhereOrNull(
(t) => t.name == data["documentTypeName"],
);
if (type != null) {
setState(() => controller.selectedType = type);
}
}
}
}
});
// Prefill tags
if (data["tags"] != null) {
controller.enteredTags.value =
List<String>.from(data["tags"].map((t) => t["name"]));
}
// Prefill file info
controller.selectedFileName = data["fileName"];
controller.selectedFileContentType = data["contentType"];
controller.selectedFileSize = data["fileSize"];
}
}
@override @override
void dispose() { void dispose() {
@ -96,7 +45,7 @@ class _DocumentUploadBottomSheetState extends State<DocumentUploadBottomSheet> {
void _handleSubmit() { void _handleSubmit() {
if (!(_formKey.currentState?.validate() ?? false)) return; if (!(_formKey.currentState?.validate() ?? false)) return;
if (selectedFile == null && existingFileName == null) { if (selectedFile == null) {
showAppSnackbar( showAppSnackbar(
title: "Error", title: "Error",
message: "Please attach a document", message: "Please attach a document",
@ -173,7 +122,6 @@ class _DocumentUploadBottomSheetState extends State<DocumentUploadBottomSheet> {
setState(() { setState(() {
selectedFile = file; selectedFile = file;
existingFileName = null;
controller.selectedFileName = fileName; controller.selectedFileName = fileName;
controller.selectedFileBase64 = base64Data; controller.selectedFileBase64 = base64Data;
controller.selectedFileContentType = controller.selectedFileContentType =
@ -188,7 +136,7 @@ class _DocumentUploadBottomSheetState extends State<DocumentUploadBottomSheet> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BaseBottomSheet( return BaseBottomSheet(
title: widget.initialData == null ? "Upload Document" : "Edit Document", title: "Upload Document",
onCancel: () => Navigator.pop(context), onCancel: () => Navigator.pop(context),
onSubmit: _handleSubmit, onSubmit: _handleSubmit,
child: Form( child: Form(
@ -267,11 +215,9 @@ class _DocumentUploadBottomSheetState extends State<DocumentUploadBottomSheet> {
/// Single Attachment Section /// Single Attachment Section
AttachmentSectionSingle( AttachmentSectionSingle(
attachment: selectedFile, attachment: selectedFile,
existingFileName: existingFileName,
onPick: _pickFile, onPick: _pickFile,
onRemove: () => setState(() { onRemove: () => setState(() {
selectedFile = null; selectedFile = null;
existingFileName = null;
controller.selectedFileName = null; controller.selectedFileName = null;
controller.selectedFileBase64 = null; controller.selectedFileBase64 = null;
controller.selectedFileContentType = null; controller.selectedFileContentType = null;
@ -392,38 +338,18 @@ class _DocumentUploadBottomSheetState extends State<DocumentUploadBottomSheet> {
/// ---------------- Single Attachment Widget ---------------- /// ---------------- Single Attachment Widget ----------------
class AttachmentSectionSingle extends StatelessWidget { class AttachmentSectionSingle extends StatelessWidget {
final File? attachment; final File? attachment;
final String? existingFileName; // new
final VoidCallback onPick; final VoidCallback onPick;
final VoidCallback? onRemove; final VoidCallback? onRemove;
const AttachmentSectionSingle({ const AttachmentSectionSingle({
Key? key, Key? key,
this.attachment, this.attachment,
this.existingFileName,
required this.onPick, required this.onPick,
this.onRemove, this.onRemove,
}) : super(key: key); }) : super(key: key);
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (attachment == null && existingFileName != null) {
return Row(
children: [
const Icon(Icons.insert_drive_file, color: Colors.blueAccent),
const SizedBox(width: 8),
Expanded(
child: Text(existingFileName!,
style: const TextStyle(fontSize: 14, color: Colors.black87)),
),
if (onRemove != null)
IconButton(
icon: const Icon(Icons.close, color: Colors.red),
onPressed: onRemove,
),
],
);
}
final allowedImageExtensions = ['jpg', 'jpeg', 'png']; final allowedImageExtensions = ['jpg', 'jpeg', 'png'];
Widget buildTile(File file) { Widget buildTile(File file) {
@ -553,6 +479,7 @@ class AttachmentSectionSingle extends StatelessWidget {
} }
} }
// ---- Reusable Widgets ---- // ---- Reusable Widgets ----
class LabeledInput extends StatelessWidget { class LabeledInput extends StatelessWidget {