feat(contact): streamline validation logic and enhance UI for adding contacts
This commit is contained in:
parent
33d267f18e
commit
574e7df447
@ -101,7 +101,7 @@ class AddContactController extends GetxController {
|
|||||||
.whereType<String>()
|
.whereType<String>()
|
||||||
.toList();
|
.toList();
|
||||||
|
|
||||||
// === Per-field Validation with Specific Messages ===
|
// === Required validations only for name, organization, and bucket ===
|
||||||
if (name.trim().isEmpty) {
|
if (name.trim().isEmpty) {
|
||||||
showAppSnackbar(
|
showAppSnackbar(
|
||||||
title: "Missing Name",
|
title: "Missing Name",
|
||||||
@ -120,51 +120,6 @@ class AddContactController extends GetxController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (emails.isEmpty) {
|
|
||||||
showAppSnackbar(
|
|
||||||
title: "Missing Email",
|
|
||||||
message: "Please add at least one email.",
|
|
||||||
type: SnackbarType.warning,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (phones.isEmpty) {
|
|
||||||
showAppSnackbar(
|
|
||||||
title: "Missing Phone Number",
|
|
||||||
message: "Please add at least one phone number.",
|
|
||||||
type: SnackbarType.warning,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (address.trim().isEmpty) {
|
|
||||||
showAppSnackbar(
|
|
||||||
title: "Missing Address",
|
|
||||||
message: "Please enter the address.",
|
|
||||||
type: SnackbarType.warning,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (description.trim().isEmpty) {
|
|
||||||
showAppSnackbar(
|
|
||||||
title: "Missing Description",
|
|
||||||
message: "Please enter a description.",
|
|
||||||
type: SnackbarType.warning,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (selectedCategory.value.trim().isEmpty || categoryId == null) {
|
|
||||||
showAppSnackbar(
|
|
||||||
title: "Missing Category",
|
|
||||||
message: "Please select a contact category.",
|
|
||||||
type: SnackbarType.warning,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (selectedBucket.value.trim().isEmpty || bucketId == null) {
|
if (selectedBucket.value.trim().isEmpty || bucketId == null) {
|
||||||
showAppSnackbar(
|
showAppSnackbar(
|
||||||
title: "Missing Bucket",
|
title: "Missing Bucket",
|
||||||
@ -174,25 +129,7 @@ class AddContactController extends GetxController {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (selectedProjects.isEmpty || projectIds.isEmpty) {
|
// === Build body (include optional fields if available) ===
|
||||||
showAppSnackbar(
|
|
||||||
title: "Missing Projects",
|
|
||||||
message: "Please select at least one project.",
|
|
||||||
type: SnackbarType.warning,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (enteredTags.isEmpty) {
|
|
||||||
showAppSnackbar(
|
|
||||||
title: "Missing Tags",
|
|
||||||
message: "Please enter at least one tag.",
|
|
||||||
type: SnackbarType.warning,
|
|
||||||
);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// === Submit if all validations passed ===
|
|
||||||
try {
|
try {
|
||||||
final tagObjects = enteredTags.map((tagName) {
|
final tagObjects = enteredTags.map((tagName) {
|
||||||
final tagId = tagsMap[tagName];
|
final tagId = tagsMap[tagName];
|
||||||
@ -205,14 +142,15 @@ class AddContactController extends GetxController {
|
|||||||
if (id != null) "id": id,
|
if (id != null) "id": id,
|
||||||
"name": name.trim(),
|
"name": name.trim(),
|
||||||
"organization": organization.trim(),
|
"organization": organization.trim(),
|
||||||
"contactCategoryId": categoryId,
|
if (selectedCategory.value.isNotEmpty && categoryId != null)
|
||||||
"projectIds": projectIds,
|
"contactCategoryId": categoryId,
|
||||||
|
if (projectIds.isNotEmpty) "projectIds": projectIds,
|
||||||
"bucketIds": [bucketId],
|
"bucketIds": [bucketId],
|
||||||
"tags": tagObjects,
|
if (enteredTags.isNotEmpty) "tags": tagObjects,
|
||||||
"contactEmails": emails,
|
if (emails.isNotEmpty) "contactEmails": emails,
|
||||||
"contactPhones": phones,
|
if (phones.isNotEmpty) "contactPhones": phones,
|
||||||
"address": address.trim(),
|
if (address.trim().isNotEmpty) "address": address.trim(),
|
||||||
"description": description.trim(),
|
if (description.trim().isNotEmpty) "description": description.trim(),
|
||||||
};
|
};
|
||||||
|
|
||||||
logSafe("${id != null ? 'Updating' : 'Creating'} contact");
|
logSafe("${id != null ? 'Updating' : 'Creating'} contact");
|
||||||
|
|||||||
@ -25,7 +25,7 @@ class _AddContactBottomSheetState extends State<AddContactBottomSheet> {
|
|||||||
final addressController = TextEditingController();
|
final addressController = TextEditingController();
|
||||||
final descriptionController = TextEditingController();
|
final descriptionController = TextEditingController();
|
||||||
final tagTextController = TextEditingController();
|
final tagTextController = TextEditingController();
|
||||||
|
final RxBool showAdvanced = false.obs;
|
||||||
final RxList<TextEditingController> emailControllers =
|
final RxList<TextEditingController> emailControllers =
|
||||||
<TextEditingController>[].obs;
|
<TextEditingController>[].obs;
|
||||||
final RxList<RxString> emailLabels = <RxString>[].obs;
|
final RxList<RxString> emailLabels = <RxString>[].obs;
|
||||||
@ -514,185 +514,202 @@ class _AddContactBottomSheetState extends State<AddContactBottomSheet> {
|
|||||||
borderRadius: const BorderRadius.vertical(top: Radius.circular(24)),
|
borderRadius: const BorderRadius.vertical(top: Radius.circular(24)),
|
||||||
),
|
),
|
||||||
child: Padding(
|
child: Padding(
|
||||||
padding: const EdgeInsets.fromLTRB(20, 16, 20, 32),
|
padding: const EdgeInsets.fromLTRB(20, 16, 20, 32),
|
||||||
child: Form(
|
child: Form(
|
||||||
key: formKey,
|
key: formKey,
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Center(
|
Center(
|
||||||
child: MyText.titleMedium(
|
child: MyText.titleMedium(
|
||||||
widget.existingContact != null
|
widget.existingContact != null
|
||||||
? "Edit Contact"
|
? "Edit Contact"
|
||||||
: "Create New Contact",
|
: "Create New Contact",
|
||||||
fontWeight: 700,
|
fontWeight: 700,
|
||||||
),
|
|
||||||
),
|
|
||||||
MySpacing.height(24),
|
|
||||||
_sectionLabel("Basic Info"),
|
|
||||||
MySpacing.height(16),
|
|
||||||
_buildTextField("Name", nameController),
|
|
||||||
MySpacing.height(16),
|
|
||||||
_buildOrganizationField(),
|
|
||||||
MySpacing.height(24),
|
|
||||||
_sectionLabel("Contact Info"),
|
|
||||||
MySpacing.height(16),
|
|
||||||
Obx(() => _buildEmailList()),
|
|
||||||
TextButton.icon(
|
|
||||||
onPressed: () {
|
|
||||||
emailControllers.add(TextEditingController());
|
|
||||||
emailLabels.add('Office'.obs);
|
|
||||||
},
|
|
||||||
icon: const Icon(Icons.add),
|
|
||||||
label: const Text("Add Email"),
|
|
||||||
),
|
|
||||||
Obx(() => _buildPhoneList()),
|
|
||||||
TextButton.icon(
|
|
||||||
onPressed: () {
|
|
||||||
phoneControllers.add(TextEditingController());
|
|
||||||
phoneLabels.add('Work'.obs);
|
|
||||||
},
|
|
||||||
icon: const Icon(Icons.add),
|
|
||||||
label: const Text("Add Phone"),
|
|
||||||
),
|
|
||||||
MySpacing.height(24),
|
|
||||||
_sectionLabel("Other Details"),
|
|
||||||
MySpacing.height(16),
|
|
||||||
MyText.labelMedium("Category"),
|
|
||||||
MySpacing.height(8),
|
|
||||||
_popupSelector(
|
|
||||||
hint: "Select Category",
|
|
||||||
selectedValue: controller.selectedCategory,
|
|
||||||
options: controller.categories,
|
|
||||||
),
|
|
||||||
MySpacing.height(16),
|
|
||||||
MyText.labelMedium("Select Projects"),
|
|
||||||
MySpacing.height(8),
|
|
||||||
GestureDetector(
|
|
||||||
onTap: () async {
|
|
||||||
await showDialog(
|
|
||||||
context: context,
|
|
||||||
builder: (BuildContext context) {
|
|
||||||
return AlertDialog(
|
|
||||||
title: const Text('Select Projects'),
|
|
||||||
content: Obx(() {
|
|
||||||
return SizedBox(
|
|
||||||
width: double.maxFinite,
|
|
||||||
child: ListView(
|
|
||||||
shrinkWrap: true,
|
|
||||||
children:
|
|
||||||
controller.globalProjects.map((project) {
|
|
||||||
final isSelected = controller
|
|
||||||
.selectedProjects
|
|
||||||
.contains(project);
|
|
||||||
return Theme(
|
|
||||||
data: Theme.of(context).copyWith(
|
|
||||||
unselectedWidgetColor: Colors
|
|
||||||
.black, // checkbox border when not selected
|
|
||||||
checkboxTheme: CheckboxThemeData(
|
|
||||||
fillColor: MaterialStateProperty
|
|
||||||
.resolveWith<Color>((states) {
|
|
||||||
if (states.contains(
|
|
||||||
MaterialState.selected)) {
|
|
||||||
return Colors
|
|
||||||
.white; // fill when selected
|
|
||||||
}
|
|
||||||
return Colors.transparent;
|
|
||||||
}),
|
|
||||||
checkColor: MaterialStateProperty.all(
|
|
||||||
Colors.black), // check mark color
|
|
||||||
side: const BorderSide(
|
|
||||||
color: Colors.black,
|
|
||||||
width: 2), // border color
|
|
||||||
shape: RoundedRectangleBorder(
|
|
||||||
borderRadius:
|
|
||||||
BorderRadius.circular(4),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
child: CheckboxListTile(
|
|
||||||
dense: true,
|
|
||||||
title: Text(project),
|
|
||||||
value: isSelected,
|
|
||||||
onChanged: (bool? selected) {
|
|
||||||
if (selected == true) {
|
|
||||||
controller.selectedProjects
|
|
||||||
.add(project);
|
|
||||||
} else {
|
|
||||||
controller.selectedProjects
|
|
||||||
.remove(project);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}).toList(),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
actions: [
|
|
||||||
TextButton(
|
|
||||||
onPressed: () => Navigator.pop(context),
|
|
||||||
child: const Text('Done'),
|
|
||||||
),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
},
|
|
||||||
);
|
|
||||||
},
|
|
||||||
child: Container(
|
|
||||||
height: 48,
|
|
||||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
|
||||||
decoration: BoxDecoration(
|
|
||||||
color: Colors.grey.shade100,
|
|
||||||
borderRadius: BorderRadius.circular(12),
|
|
||||||
border: Border.all(color: Colors.grey.shade300),
|
|
||||||
),
|
),
|
||||||
alignment: Alignment.centerLeft,
|
|
||||||
child: Obx(() {
|
|
||||||
final selected = controller.selectedProjects;
|
|
||||||
return Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
||||||
children: [
|
|
||||||
Expanded(
|
|
||||||
child: Text(
|
|
||||||
selected.isEmpty
|
|
||||||
? "Select Projects"
|
|
||||||
: selected.join(', '),
|
|
||||||
overflow: TextOverflow.ellipsis,
|
|
||||||
style: const TextStyle(fontSize: 14),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
const Icon(Icons.expand_more, size: 20),
|
|
||||||
],
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
),
|
),
|
||||||
),
|
MySpacing.height(24),
|
||||||
MySpacing.height(16),
|
_sectionLabel("Required Fields"),
|
||||||
MyText.labelMedium("Select Bucket"),
|
MySpacing.height(12),
|
||||||
MySpacing.height(8),
|
_buildTextField("Name", nameController),
|
||||||
_popupSelector(
|
MySpacing.height(16),
|
||||||
hint: "Select Bucket",
|
_buildOrganizationField(),
|
||||||
selectedValue: controller.selectedBucket,
|
MySpacing.height(16),
|
||||||
options: controller.buckets,
|
MyText.labelMedium("Select Bucket"),
|
||||||
),
|
MySpacing.height(8),
|
||||||
MySpacing.height(16),
|
_popupSelector(
|
||||||
MyText.labelMedium("Tags"),
|
hint: "Select Bucket",
|
||||||
MySpacing.height(8),
|
selectedValue: controller.selectedBucket,
|
||||||
_tagInputSection(),
|
options: controller.buckets,
|
||||||
MySpacing.height(16),
|
),
|
||||||
_buildTextField("Address", addressController, maxLines: 2),
|
MySpacing.height(24),
|
||||||
MySpacing.height(16),
|
|
||||||
_buildTextField("Description", descriptionController,
|
// Toggle for Advanced Section
|
||||||
maxLines: 2),
|
Obx(() => GestureDetector(
|
||||||
MySpacing.height(24),
|
onTap: () => showAdvanced.toggle(),
|
||||||
_buildActionButtons(),
|
child: Row(
|
||||||
],
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
),
|
children: [
|
||||||
),
|
MyText.labelLarge("Advanced Details (Optional)",
|
||||||
),
|
fontWeight: 600),
|
||||||
|
Icon(showAdvanced.value
|
||||||
|
? Icons.expand_less
|
||||||
|
: Icons.expand_more),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)),
|
||||||
|
Obx(() => showAdvanced.value
|
||||||
|
? Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
MySpacing.height(24),
|
||||||
|
_sectionLabel("Contact Info"),
|
||||||
|
MySpacing.height(16),
|
||||||
|
_buildEmailList(),
|
||||||
|
TextButton.icon(
|
||||||
|
onPressed: () {
|
||||||
|
emailControllers.add(TextEditingController());
|
||||||
|
emailLabels.add('Office'.obs);
|
||||||
|
},
|
||||||
|
icon: const Icon(Icons.add),
|
||||||
|
label: const Text("Add Email"),
|
||||||
|
),
|
||||||
|
_buildPhoneList(),
|
||||||
|
TextButton.icon(
|
||||||
|
onPressed: () {
|
||||||
|
phoneControllers.add(TextEditingController());
|
||||||
|
phoneLabels.add('Work'.obs);
|
||||||
|
},
|
||||||
|
icon: const Icon(Icons.add),
|
||||||
|
label: const Text("Add Phone"),
|
||||||
|
),
|
||||||
|
MySpacing.height(24),
|
||||||
|
_sectionLabel("Other Details"),
|
||||||
|
MySpacing.height(16),
|
||||||
|
MyText.labelMedium("Category"),
|
||||||
|
MySpacing.height(8),
|
||||||
|
_popupSelector(
|
||||||
|
hint: "Select Category",
|
||||||
|
selectedValue: controller.selectedCategory,
|
||||||
|
options: controller.categories,
|
||||||
|
),
|
||||||
|
MySpacing.height(16),
|
||||||
|
MyText.labelMedium("Select Projects"),
|
||||||
|
MySpacing.height(8),
|
||||||
|
_projectSelectorUI(),
|
||||||
|
MySpacing.height(16),
|
||||||
|
MyText.labelMedium("Tags"),
|
||||||
|
MySpacing.height(8),
|
||||||
|
_tagInputSection(),
|
||||||
|
MySpacing.height(16),
|
||||||
|
_buildTextField("Address", addressController,
|
||||||
|
maxLines: 2),
|
||||||
|
MySpacing.height(16),
|
||||||
|
_buildTextField(
|
||||||
|
"Description", descriptionController,
|
||||||
|
maxLines: 2),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
: SizedBox()),
|
||||||
|
|
||||||
|
MySpacing.height(24),
|
||||||
|
_buildActionButtons(),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
)),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget _projectSelectorUI() {
|
||||||
|
return GestureDetector(
|
||||||
|
onTap: () async {
|
||||||
|
await showDialog(
|
||||||
|
context: context,
|
||||||
|
builder: (BuildContext context) {
|
||||||
|
return AlertDialog(
|
||||||
|
title: const Text('Select Projects'),
|
||||||
|
content: Obx(() {
|
||||||
|
return SizedBox(
|
||||||
|
width: double.maxFinite,
|
||||||
|
child: ListView(
|
||||||
|
shrinkWrap: true,
|
||||||
|
children: controller.globalProjects.map((project) {
|
||||||
|
final isSelected =
|
||||||
|
controller.selectedProjects.contains(project);
|
||||||
|
return Theme(
|
||||||
|
data: Theme.of(context).copyWith(
|
||||||
|
unselectedWidgetColor: Colors.black,
|
||||||
|
checkboxTheme: CheckboxThemeData(
|
||||||
|
fillColor: MaterialStateProperty.resolveWith<Color>(
|
||||||
|
(states) {
|
||||||
|
if (states.contains(MaterialState.selected)) {
|
||||||
|
return Colors.white;
|
||||||
|
}
|
||||||
|
return Colors.transparent;
|
||||||
|
}),
|
||||||
|
checkColor: MaterialStateProperty.all(Colors.black),
|
||||||
|
side:
|
||||||
|
const BorderSide(color: Colors.black, width: 2),
|
||||||
|
shape: RoundedRectangleBorder(
|
||||||
|
borderRadius: BorderRadius.circular(4),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
child: CheckboxListTile(
|
||||||
|
dense: true,
|
||||||
|
title: Text(project),
|
||||||
|
value: isSelected,
|
||||||
|
onChanged: (bool? selected) {
|
||||||
|
if (selected == true) {
|
||||||
|
controller.selectedProjects.add(project);
|
||||||
|
} else {
|
||||||
|
controller.selectedProjects.remove(project);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}).toList(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
actions: [
|
||||||
|
TextButton(
|
||||||
|
onPressed: () => Navigator.pop(context),
|
||||||
|
child: const Text('Done'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
child: Container(
|
||||||
|
height: 48,
|
||||||
|
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||||
|
decoration: BoxDecoration(
|
||||||
|
color: Colors.grey.shade100,
|
||||||
|
borderRadius: BorderRadius.circular(12),
|
||||||
|
border: Border.all(color: Colors.grey.shade300),
|
||||||
|
),
|
||||||
|
alignment: Alignment.centerLeft,
|
||||||
|
child: Obx(() {
|
||||||
|
final selected = controller.selectedProjects;
|
||||||
|
return Row(
|
||||||
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||||
|
children: [
|
||||||
|
Expanded(
|
||||||
|
child: Text(
|
||||||
|
selected.isEmpty ? "Select Projects" : selected.join(', '),
|
||||||
|
overflow: TextOverflow.ellipsis,
|
||||||
|
style: const TextStyle(fontSize: 14),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const Icon(Icons.expand_more, size: 20),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user