Skip to content

Commit

Permalink
fix: added support for models' attribute descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
qwerdenkerXD committed Aug 19, 2023
1 parent 25adb0b commit afbc27a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
11 changes: 9 additions & 2 deletions src/types/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ export type StorageType =
| 'neo4j-adapter';

export interface Attributes {
[key: string]: AttributeScalarType | AttributeArrayType;
[key: string]: AttributeType | AttributeWithDescription;
}

export type AttributeType = AttributeScalarType | AttributeArrayType;

export interface AttributeWithDescription {
description: string;
type: AttributeType;
}

export type AttributeValue = AttributeScalarValue | AttributeArrayValue;
Expand All @@ -61,7 +68,7 @@ export type spaSearchOperator = 'like' | 'iLike';

export interface ParsedAttribute {
name: string;
type: AttributeScalarType | AttributeArrayType;
type: AttributeType | AttributeWithDescription;
primaryKey?: boolean;
foreignKey?: boolean;
automaticId?: boolean;
Expand Down
4 changes: 3 additions & 1 deletion src/utils/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ParsedAssociation,
ParsedAttribute,
StorageType,
AttributeWithDescription,
} from '@/types/models';

/**
Expand Down Expand Up @@ -143,9 +144,10 @@ export function parseAttributes(
const foreignKeys = getForeignKeys(model);
let attributes: Array<ParsedAttribute> = Object.keys(model.attributes).map(
(attribute) => {
const type = model.attributes[attribute];
return {
name: attribute,
type: model.attributes[attribute],
type: (type as AttributeWithDescription).type ?? type,
primaryKey: model.internalId === attribute,
foreignKey: foreignKeys.has(attribute),
};
Expand Down
13 changes: 8 additions & 5 deletions src/zendro/model-table/table-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Typography,
} from '@mui/material';
import { VpnKey as KeyIcon } from '@mui/icons-material';
import { ParsedAttribute } from '@/types/models';
import { ParsedAttribute, AttributeWithDescription } from '@/types/models';
import { OrderDirection } from '@/types/queries';
import TableColumn from './table-column';

Expand Down Expand Up @@ -59,11 +59,14 @@ export default function TableHeader({
? t('model-table.header-id-tooltip')
: undefined
}
align={
attribute.type.includes('Int') || attribute.type.includes('Float')
align={(() => {
const type =
(attribute.type as AttributeWithDescription).type ??
attribute.type;
return type.includes('Int') || type.includes('Float')
? 'right'
: 'left'
}
: 'left';
})()}
disableSort={disableSort}
activeOrder={activeOrder === attribute.name}
orderDirection={
Expand Down
17 changes: 12 additions & 5 deletions src/zendro/model-table/table-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import {
TableRow as MuiTableRow,
TableRowProps as MuiTableRowProps,
} from '@mui/material';
import { DataRecord, ParsedAttribute } from '@/types/models';
import {
DataRecord,
ParsedAttribute,
AttributeWithDescription,
} from '@/types/models';

export type TableRowActionHandler = (primaryKey: string | number) => void;

Expand All @@ -30,11 +34,14 @@ export default function TableRow({
{attributes.map((attribute, index) => (
<MuiTableCell
key={`${attribute.name}-${index}`}
align={
attribute.type.includes('Int') || attribute.type.includes('Float')
align={(() => {
const type =
(attribute.type as AttributeWithDescription).type ??
attribute.type;
return type.includes('Int') || type.includes('Float')
? 'right'
: 'left'
}
: 'left';
})()}
>
{String(
record[attribute.name] !== null ? record[attribute.name] : ''
Expand Down
9 changes: 7 additions & 2 deletions src/zendro/record-form/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@ import {

import ActionButton from '@/components/float-button';

import { AttributeValue, DataRecord, ParsedAttribute } from '@/types/models';
import {
AttributeValue,
DataRecord,
ParsedAttribute,
AttributeWithDescription,
} from '@/types/models';
import { isNullorEmpty } from '@/utils/validation';

import FormErrors from './form-errors';
Expand Down Expand Up @@ -286,7 +291,7 @@ export default function AttributesForm({
return (
<FormField
key={name}
type={type}
type={(type as AttributeWithDescription).type ?? type}
error={clientError || serverErrors ? true : false}
helperText={
clientError || serverErrors
Expand Down

0 comments on commit afbc27a

Please sign in to comment.