Refactor validation

Signed-off-by: Nikolaos Karaolidis <nick@karaolidis.com>
This commit is contained in:
2023-07-21 16:34:44 +03:00
parent b7dd1c77af
commit 3c303e0304
17 changed files with 256 additions and 151 deletions

View File

@@ -4,7 +4,11 @@ import { Appbar, Button, useTheme } from 'react-native-paper';
import { useNavigation } from '@react-navigation/native';
import { useRealm } from '@realm/react';
import styles from '../styles';
import { generateRandomColor } from '../utilities';
import {
generateRandomColor,
validateColor,
validateTagName,
} from '../utilities';
import { useDimensions } from '../contexts';
import { Tag } from '../database';
import { TagEditor } from '../components';
@@ -15,18 +19,16 @@ const AddTag = () => {
const { orientation } = useDimensions();
const realm = useRealm();
const [tagName, setTagName] = useState('newTag');
const [tagColor, setTagColor] = useState(generateRandomColor());
const [validatedTagColor, setValidatedTagColor] = useState(tagColor);
const [tagNameError, setTagNameError] = useState<string | undefined>();
const [tagColorError, setTagColorError] = useState<string | undefined>();
const [tagName, setTagName] = useState(validateTagName('newTag'));
const [tagColor, setTagColor] = useState(
validateColor(generateRandomColor()),
);
const handleSave = () => {
realm.write(() => {
realm.create(Tag.schema.name, {
name: tagName,
color: tagColor,
name: tagName.parsed,
color: tagColor.parsed,
});
});
@@ -54,12 +56,6 @@ const AddTag = () => {
setTagName={setTagName}
tagColor={tagColor}
setTagColor={setTagColor}
validatedTagColor={validatedTagColor}
setValidatedTagColor={setValidatedTagColor}
tagNameError={tagNameError}
setTagNameError={setTagNameError}
tagColorError={tagColorError}
setTagColorError={setTagColorError}
/>
</View>
<View style={[styles.flex, styles.justifyEnd]}>
@@ -67,7 +63,7 @@ const AddTag = () => {
mode="contained"
icon="floppy"
onPress={handleSave}
disabled={!!tagNameError || !!tagColorError}>
disabled={!tagName.valid || !tagColor.valid}>
Save
</Button>
</View>