Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions internal/models/field_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func IsValidFieldType(fieldType string) bool {
}

type Field struct {
Name string `json:"name"`
Type FieldType `json:"type"`
Name string `json:"name"`
Type FieldType `json:"type"`
Optional bool `json:"optional,omitempty"`
}
18 changes: 17 additions & 1 deletion ui/src/components/modals/CreateCollectionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const CreateCollectionModal: React.FC<CreateCollectionModalProps> = ({
const [fields, setFields] = useState<Field[]>([]);
const [newFieldName, setNewFieldName] = useState("");
const [newFieldType, setNewFieldType] = useState<FieldType>(FieldType.STRING);
const [newFieldOptional, setNewFieldOptional] = useState<boolean>(false)
const [error, setError] = useState<string | null>(null);
const [fieldsError, setFieldsError] = useState<string | null>(null);
const { request, loading } = useApi();
Expand All @@ -30,6 +31,10 @@ const CreateCollectionModal: React.FC<CreateCollectionModalProps> = ({
setNewFieldName(e.target.value);
};

const handleNewOptionalChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setNewFieldOptional(e.target.checked)
}

const addField = () => {
if (newFieldName.trim() === "") {
setFieldsError("Field name cannot be empty.");
Expand All @@ -48,6 +53,7 @@ const CreateCollectionModal: React.FC<CreateCollectionModalProps> = ({
{
name: newFieldName.trim().toLowerCase(),
type: newFieldType,
optional: newFieldOptional
},
]);
setNewFieldName("");
Expand Down Expand Up @@ -120,7 +126,7 @@ const CreateCollectionModal: React.FC<CreateCollectionModalProps> = ({
{fields.map((field, index) => (
<div key={index} className="flex items-center mb-2">
<p className="mr-2">
{field.name} ({field.type})
{field.name} ({field.type}{field.optional && ', optional'})
</p>
<button
className="btn btn-sm btn-error btn-outline"
Expand Down Expand Up @@ -153,6 +159,16 @@ const CreateCollectionModal: React.FC<CreateCollectionModalProps> = ({
Add Field
</button>
</div>
<label className="label justify-start gap-2">
<input
id="field-optional"
type="checkbox"
className="checkbox"
checked={newFieldOptional}
onChange={handleNewOptionalChange}
/>
Optional
</label>
</div>
{fieldsError && <p className="text-red-500 mt-2">{fieldsError}</p>}
<div className="modal-action">
Expand Down
30 changes: 16 additions & 14 deletions ui/src/components/modals/CreateEntryModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ const CreateEntryModal: React.FC<CreateEntryModalProps> = ({
const handleSubmit = async () => {
console.log("fields", fields);

const hasEmptyFields = fields.some(
(field) => formState[field.name].trim() === "",
const hasRequiredFields = fields.some(
(field) => !field.optional && formState[field.name].trim() === "",
);

console.log("hasEmptyFields", hasEmptyFields);
console.log("hasEmptyFields", hasRequiredFields);

if (hasEmptyFields) {
if (hasRequiredFields) {
setError("All fields are required.");
return;
}
Expand All @@ -64,8 +64,10 @@ const CreateEntryModal: React.FC<CreateEntryModalProps> = ({
// Prepare data with types
const dataWithTypes = fields.reduce(
(acc, field) => {
let value = formState[field.name]
if (field.optional && value.trim() === "") value = null
acc[field.name] = {
value: formState[field.name],
value,
type: field.type,
};
return acc;
Expand Down Expand Up @@ -101,7 +103,7 @@ const CreateEntryModal: React.FC<CreateEntryModalProps> = ({
value={formState[field.name]}
onChange={handleInputChange}
className="input input-bordered w-full"
required
required={!field.optional}
/>
);
case FieldType.STRING:
Expand All @@ -113,7 +115,7 @@ const CreateEntryModal: React.FC<CreateEntryModalProps> = ({
value={formState[field.name]}
onChange={handleInputChange}
className="input input-bordered w-full"
required
required={!field.optional}
/>
);
case FieldType.TEXT:
Expand All @@ -124,7 +126,7 @@ const CreateEntryModal: React.FC<CreateEntryModalProps> = ({
value={formState[field.name]}
onChange={handleInputChange}
className="textarea textarea-bordered w-full"
required
required={!field.optional}
/>
);
case FieldType.NUMBER:
Expand All @@ -136,7 +138,7 @@ const CreateEntryModal: React.FC<CreateEntryModalProps> = ({
value={formState[field.name]}
onChange={handleInputChange}
className="input input-bordered w-full"
required
required={!field.optional}
/>
);
case FieldType.BOOLEAN:
Expand All @@ -147,7 +149,7 @@ const CreateEntryModal: React.FC<CreateEntryModalProps> = ({
value={formState[field.name]}
onChange={handleInputChange}
className="select select-bordered w-full"
required
required={!field.optional}
>
<option disabled selected>
Select an option
Expand All @@ -165,7 +167,7 @@ const CreateEntryModal: React.FC<CreateEntryModalProps> = ({
value={formState[field.name]}
onChange={handleInputChange}
className="input input-bordered w-full"
required
required={!field.optional}
/>
);
case FieldType.IMAGE:
Expand All @@ -177,7 +179,7 @@ const CreateEntryModal: React.FC<CreateEntryModalProps> = ({
value={formState[field.name]}
onChange={handleInputChange}
className="input input-bordered w-full"
required
required={!field.optional}
/>
);
default:
Expand All @@ -189,7 +191,7 @@ const CreateEntryModal: React.FC<CreateEntryModalProps> = ({
value={formState[field.name]}
onChange={handleInputChange}
className="input input-bordered w-full"
required
required={!field.optional}
/>
);
}
Expand All @@ -207,7 +209,7 @@ const CreateEntryModal: React.FC<CreateEntryModalProps> = ({
className="block text-sm font-medium text-gray-700 mb-2"
htmlFor={field.name}
>
{field.name} ({field.type})
{field.name} ({field.type}{field.optional && ', optional'})
</label>
{renderFieldInput(field)}
</div>
Expand Down
2 changes: 1 addition & 1 deletion ui/src/pages/collections/Detail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ const CollectionDetailsPage: React.FC = () => {
<h3 className="text-display text-base font-medium text-base-content mb-1">
{field.name}
</h3>
<p className="badge-bare">{field.type}</p>
<p className="badge-bare">{field.type}{field.optional && ', optional'}</p>
</div>
))}
</div>
Expand Down
3 changes: 2 additions & 1 deletion ui/src/types/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export const VALID_FIELD_TYPES = Object.values(FieldType);
export interface Field {
name: string;
type: FieldType;
}
optional?: boolean;
}