From 0381d2816ba0cfb957e2f0f61188f71b869f87a5 Mon Sep 17 00:00:00 2001 From: Tal Leibman Date: Fri, 14 Feb 2020 14:19:35 +0200 Subject: [PATCH] JournalEdit: add ColorPicker and imporve validation Fixes #75 --- src/Journals/JournalEdit.tsx | 49 +++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/src/Journals/JournalEdit.tsx b/src/Journals/JournalEdit.tsx index f05ec22..29ffed4 100644 --- a/src/Journals/JournalEdit.tsx +++ b/src/Journals/JournalEdit.tsx @@ -16,6 +16,8 @@ import ConfirmationDialog from '../widgets/ConfirmationDialog'; import * as EteSync from 'etesync'; import { SyncInfo } from '../SyncGate'; +import ColorPicker from '../widgets/ColorPicker'; +import { defaultColor, colorHtmlToInt, colorIntToHtml } from '../journal-processors'; interface PropsType { syncInfo: SyncInfo; @@ -25,13 +27,23 @@ interface PropsType { onCancel: () => void; } +interface FormErrors { + displayName?: string; + color?: string; +} + export default function JournalEdit(props: PropsType) { + const [errors, setErrors] = React.useState({}); const [showDeleteDialog, setShowDeleteDialog] = React.useState(false); const [info, setInfo] = React.useState(); + const [selectedColor, setSelectedColor] = React.useState(''); React.useEffect(() => { if (props.item !== undefined) { setInfo(props.item); + if (props.item.color) { + setSelectedColor(colorIntToHtml(props.item.color)); + } } else { setInfo({ uid: EteSync.genUid(), @@ -48,10 +60,28 @@ export default function JournalEdit(props: PropsType) { function onSubmit(e: React.FormEvent) { e.preventDefault(); + const saveErrors: FormErrors = {}; + const fieldRequired = 'This field is required!'; const { onSave } = props; - const item = new EteSync.CollectionInfo(info); + const displayName = info?.displayName; + const color = colorHtmlToInt(selectedColor); + + if (!displayName) { + saveErrors.displayName = fieldRequired; + } + + if (selectedColor && !color) { + saveErrors.color = 'Must be of the form #RRGGBB or #RRGGBBAA or empty'; + } + + if (Object.keys(saveErrors).length > 0) { + setErrors(saveErrors); + return; + } + + const item = new EteSync.CollectionInfo({ ...info, color: color }); onSave(item, props.item); } @@ -79,6 +109,20 @@ export default function JournalEdit(props: PropsType) { CALENDAR: 'Calendar', TASKS: 'Task List', }; + let collectionColorBox: React.ReactNode; + switch (info.type) { + case 'CALENDAR': + case 'TASKS': + collectionColorBox = ( + setSelectedColor(color)} + error={errors.color} + /> + ); + break; + } return ( <> @@ -108,6 +152,8 @@ export default function JournalEdit(props: PropsType) { onChange={(event: React.ChangeEvent<{ value: string }>) => setInfo({ ...info, displayName: event.target.value })} style={styles.fullWidth} margin="normal" + error={!!errors.displayName} + helperText={errors.displayName} /> + {collectionColorBox}