Skip to content

Commit

Permalink
add messages to update errors in console, fixup missed error in Date …
Browse files Browse the repository at this point in the history
…type
  • Loading branch information
wernst authored and matlin committed Jun 24, 2024
1 parent aac43bb commit 907c1d9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 20 deletions.
70 changes: 59 additions & 11 deletions packages/console/src/components/data-table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
updateTriplitSet,
updateTriplitValue,
} from 'src/utils/mutate-cells.js';
import { useToast } from 'src/hooks/useToast.js';

interface DataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
Expand Down Expand Up @@ -230,6 +231,7 @@ export function DataCell({
editable = true,
optional = false,
}: TriplitDataCellProps) {
const { toast } = useToast();
const [isEditing, setIsEditing] = useState(false);
const nullable = !!attributeDef?.options?.nullable;
useEffect(() => {
Expand Down Expand Up @@ -261,29 +263,45 @@ export function DataCell({
<SetCellEditor
set={value}
definition={attributeDef}
onChangeSet={(value, action) => {
updateTriplitSet(
onChangeSet={async (value, action) => {
const error = await updateTriplitSet(
attribute,
client,
collection,
entityId,
value,
action
);
if (error) {
toast({
title: 'Error',
description: error,
variant: 'destructive',
});
return;
}
}}
/>
) : attributeDef?.type === 'record' ? (
<RecordCellEditor
value={value}
definition={attributeDef}
onSubmit={(newValue) => {
updateTriplitValue(
onSubmit={async (newValue) => {
const error = await updateTriplitValue(
attribute,
client,
collection,
entityId,
newValue
);
if (error) {
toast({
title: 'Error',
description: error,
variant: 'destructive',
});
return;
}
setIsEditing(false);
}}
onBlur={() => setIsEditing(false)}
Expand All @@ -293,15 +311,24 @@ export function DataCell({
value={value}
definition={attributeDef as ValueAttributeDefinition}
onBlur={() => setIsEditing(false)}
onSubmit={(newValue: TriplitDataTypes) => {
if (newValue !== value)
updateTriplitValue(
onSubmit={async (newValue: TriplitDataTypes) => {
if (newValue !== value) {
const error = await updateTriplitValue(
attribute,
client,
collection,
entityId,
newValue
);
if (error) {
toast({
title: 'Error',
description: error,
variant: 'destructive',
});
return;
}
}
setIsEditing(false);
}}
/>
Expand All @@ -313,14 +340,22 @@ export function DataCell({
className="text-xs h-auto py-1 px-2 justify-self-start"
variant={'ghost'}
disabled={value === null}
onClick={(e) => {
updateTriplitValue(
onClick={async (e) => {
const error = await updateTriplitValue(
attribute,
client,
collection,
entityId,
null
);
if (error) {
toast({
title: 'Error',
description: error,
variant: 'destructive',
});
return;
}
}}
>
Set to <Code className="text-xs ml-1">null</Code>
Expand All @@ -331,8 +366,21 @@ export function DataCell({
className="text-xs h-auto py-1 px-2 justify-self-start"
disabled={value === undefined}
variant={'ghost'}
onClick={(e) => {
deleteTriplitValue(attribute, client, collection, entityId);
onClick={async (e) => {
const error = await deleteTriplitValue(
attribute,
client,
collection,
entityId
);
if (error) {
toast({
title: 'Error',
description: error,
variant: 'destructive',
});
return;
}
}}
>
Delete{' '}
Expand Down
22 changes: 19 additions & 3 deletions packages/console/src/utils/mutate-cells.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { TriplitClient } from '@triplit/client';
import { TriplitError } from '@triplit/db';

export type TriplitDataTypes =
| string
Expand Down Expand Up @@ -26,7 +27,12 @@ export async function updateTriplitValue(
entityCopy[path[0]] = value;
});
} catch (e) {
console.error(e);
if (e instanceof TriplitError) {
return e.message;
} else {
console.error(e);
return `An unknown error occurred updating entity '${entityId}'.`;
}
}
}

Expand All @@ -47,7 +53,12 @@ export async function deleteTriplitValue(
delete entityCopy[path[0]];
});
} catch (e) {
console.error(e);
if (e instanceof TriplitError) {
return e.message;
} else {
console.error(e);
return `An unknown error occurred deleting entity '${entityId}'.`;
}
}
}

Expand Down Expand Up @@ -92,6 +103,11 @@ export async function updateTriplitSet(
}
});
} catch (e) {
console.error(e);
if (e instanceof TriplitError) {
return e.message;
} else {
console.error(e);
return `An unknown error occurred updating entity '${entityId}'.`;
}
}
}
11 changes: 5 additions & 6 deletions packages/db/src/data-types/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ export function DateType<TypeOptions extends UserTypeOptions = {}>(
return new Date(dateString);
},
validateInput(val: any) {
if (
val instanceof Date ||
!Number.isNaN(Date.parse(val)) ||
(options.nullable && val === null)
)
return;
const isValidDate = val instanceof Date && !Number.isNaN(val.getTime());
const isValidDateString =
typeof val === 'string' && !Number.isNaN(Date.parse(val));
const isNullAndValid = options.nullable && val === null;
if (isValidDate || isValidDateString || isNullAndValid) return;
return valueMismatchMessage('date', options, val);
},
validateTripleValue(val: any) {
Expand Down

0 comments on commit 907c1d9

Please sign in to comment.