- Added validation when adding todos to restrict having todos without any assignees

This commit is contained in:
Dejan
2024-11-20 20:36:09 +01:00
parent e113d78575
commit b1a5d4c171

View File

@ -111,6 +111,19 @@ const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
repeatPickerRef.current?.toggleExpandable(true); repeatPickerRef.current?.toggleExpandable(true);
} }
const validateTodo = () => {
if (!todo?.title) {
Alert.alert('Alert', 'Title field cannot be empty');
return false;
}
if (!selectedAssignees || selectedAssignees?.length === 0) {
Alert.alert('Alert', 'Cannot have a todo without any assignees');
return false;
}
return true;
}
return ( return (
<Dialog <Dialog
bottom={true} bottom={true}
@ -151,20 +164,18 @@ const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
onPress={() => { onPress={() => {
try { try {
if (addChoreDialogProps.selectedTodo) { if (addChoreDialogProps.selectedTodo) {
if (!todo?.title) {
Alert.alert('Alert', 'Title field cannot be empty'); if (validateTodo()) {
return;
}
updateToDo({ updateToDo({
...todo, ...todo,
points: points, points: points,
assignees: selectedAssignees assignees: selectedAssignees
}); });
} else { } else {
if (!todo?.title) {
Alert.alert('Alert', 'Title field cannot be empty');
return; return;
} }
} else {
if (validateTodo()) {
addToDo({ addToDo({
...todo, ...todo,
done: false, done: false,
@ -172,6 +183,9 @@ const AddChoreDialog = (addChoreDialogProps: IAddChoreDialog) => {
assignees: selectedAssignees, assignees: selectedAssignees,
repeatDays: todo.repeatDays ?? [] repeatDays: todo.repeatDays ?? []
}); });
} else {
return;
}
} }
handleClose(); handleClose();
} catch (error) { } catch (error) {