Skip to content

Commit

Permalink
Merge pull request #212 from TouK/add-json-topic
Browse files Browse the repository at this point in the history
feat: allow creating topic without schema
  • Loading branch information
ForrestFairy authored Oct 3, 2024
2 parents a3f9445 + ca608cd commit ae2327a
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/components/addClonedTopicDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const AddClonedTopicDialog = observer(() => {
schema: topic.schemaPrettified,
group: groups.getGroupOfTopic(topic.name),
description: topic.description,
contentType: topic.contentType,
...getTopicData(topic),
}
: {
Expand Down
1 change: 1 addition & 0 deletions src/components/addTopicDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const DEFAULT_TOPIC_VALUES: TopicFormikValues = {
schema: "",
group: "",
description: "",
contentType: "AVRO",
};

export const AddTopicDialog = observer(() => {
Expand Down
5 changes: 4 additions & 1 deletion src/components/dialogFormFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ export const DialogFormFields = observer(
(props: {
basicFields: JSX.Element[];
advancedFields: JSX.Element[];
schemaInputField: JSX.Element;
showSchemaInput: boolean;
showAdvanced: boolean;
toggleAdvanced: () => void;
validateForm: () => void;
}) => {
const { basicFields, advancedFields, showAdvanced, toggleAdvanced, validateForm } = props;
const { basicFields, schemaInputField, showSchemaInput, advancedFields, showAdvanced, toggleAdvanced, validateForm } = props;
const { options } = useStore();

const toggleButton = <ToggleButton toggleAdvanced={toggleAdvanced} validateForm={validateForm} showAdvanced={showAdvanced} />;
Expand All @@ -42,6 +44,7 @@ export const DialogFormFields = observer(
{basicFields.map((field, i) => (
<Box key={i}>{field}</Box>
))}
{showSchemaInput && <Box key={1}>{schemaInputField}</Box>}
{options.allowAdvancedFields && advancedFields.length > 0 && (
<>
<Divider>{toggleButton}</Divider>
Expand Down
4 changes: 4 additions & 0 deletions src/components/dialogTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ interface DialogTemplateComponentProps<T extends FormValues, R = void> extends O
initialValues: T;
basicFields: (FormikErrors) => JSX.Element[];
advancedFields: (FormikErrors) => JSX.Element[];
schemaInputField?: (FormikErrors) => JSX.Element;
}

function DialogTemplateComponent<T extends FormValues, R = void>(props: DialogTemplateComponentProps<T, R>) {
Expand All @@ -35,6 +36,7 @@ function DialogTemplateComponent<T extends FormValues, R = void>(props: DialogTe
initialValues,
basicFields,
advancedFields,
schemaInputField,
...passProps
} = props;
const [backendValidationError, setBackendValidationError] = useState<string>(null);
Expand Down Expand Up @@ -87,6 +89,8 @@ function DialogTemplateComponent<T extends FormValues, R = void>(props: DialogTe
<BackendValidation text={backendValidationError} />
<DialogFormFields
basicFields={basicFields(errors).filter(Boolean)}
schemaInputField={schemaInputField ? schemaInputField(errors) : null}
showSchemaInput={values.contentType !== "JSON"}
advancedFields={advancedFields(errors).filter(Boolean)}
showAdvanced={showAdvanced}
toggleAdvanced={() => setShowAdvanced(!showAdvanced)}
Expand Down
16 changes: 14 additions & 2 deletions src/components/editTopicDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const EditTopicDialog = observer(() => {
schema: topic.schemaPrettified,
group: groups.getGroupOfTopic(topic.name),
description: topic.description,
contentType: topic.contentType,
}
: {
...DEFAULT_TOPIC_VALUES,
Expand All @@ -49,6 +50,16 @@ export const EditTopicDialog = observer(() => {
!groups.areGroupsHidden && <GroupsFormControl key="group" errors={errors} disabled />,
<Field required component={TextField} label="Topic name" name="topic" key="topic" fullWidth disabled />,
<Field required component={TextField} autoFocus label="Topic description" name="description" key="description" fullWidth />,
<FormControl key="contentType">
<FormLabel>ContentType</FormLabel>
<Field component={RadioGroup} row name={"contentType"}>
<FormControlLabel value="AVRO" control={<Radio />} label="AVRO" />
<FormControlLabel value="JSON" control={<Radio />} label="JSON" />
</Field>
</FormControl>,
];

const schemaInputField = (): JSX.Element => (
<Field
component={JsonTextField}
label="Avro schema"
Expand All @@ -59,8 +70,8 @@ export const EditTopicDialog = observer(() => {
variant="outlined"
multiline
rows={15}
/>,
];
/>
);

const advancedFields = (): JSX.Element[] => [
<FormControl key="advancedValues.acknowledgement">
Expand Down Expand Up @@ -97,6 +108,7 @@ export const EditTopicDialog = observer(() => {
<DialogTemplate<TopicFormikValues>
advancedFields={advancedFields}
basicFields={basicFields}
schemaInputField={schemaInputField}
dialog={dialog}
dialogTitle={"Edit topic"}
initialValues={initialValues()}
Expand Down
15 changes: 13 additions & 2 deletions src/components/topicDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ export const TopicDialog = observer(
),
<Field autoFocus required component={TextField} label="Topic name" name="topic" key="topic" fullWidth />,
<Field required component={TextField} label="Topic description" name="description" key="description" fullWidth />,
<FormControl key="contentType">
<FormLabel>ContentType</FormLabel>
<Field component={RadioGroup} row name={"contentType"}>
<FormControlLabel value="AVRO" control={<Radio />} label="AVRO" />
<FormControlLabel value="JSON" control={<Radio />} label="JSON" />
</Field>
</FormControl>,
];

const schemaInputField = (): JSX.Element => (
<Field
component={JsonTextField}
label="Avro schema"
Expand All @@ -62,8 +72,8 @@ export const TopicDialog = observer(
variant="outlined"
multiline
rows={15}
/>,
];
/>
);

const advancedFields = (): JSX.Element[] => [
<FormControl key="advancedValues.acknowledgement">
Expand Down Expand Up @@ -100,6 +110,7 @@ export const TopicDialog = observer(
<DialogTemplate<TopicFormikValues>
advancedFields={advancedFields}
basicFields={basicFields}
schemaInputField={schemaInputField}
dialog={dialog}
dialogTitle={"Add new topic"}
initialValues={initialValues()}
Expand Down
5 changes: 4 additions & 1 deletion src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface TopicModel {
trackingEnabled: boolean;
migratedFromJsonType: boolean;
schemaIdAwareSerializationSchemaEnabled: boolean;
contentType: string;
contentType: ContentType;
maxMessageSize: number;
auth: AuthModel;
subscribingRestricted: boolean;
Expand Down Expand Up @@ -119,6 +119,7 @@ export interface UndeliveredMessage {

export interface FormValues<T extends FormikValues = FormikValues> {
advancedValues?: T;
contentType?: ContentType;
}

export interface TopicFormikValues extends FormValues<AdvancedTopicFormikValues> {
Expand All @@ -130,6 +131,8 @@ export interface TopicFormikValues extends FormValues<AdvancedTopicFormikValues>

export type Acknowledgement = "LEADER" | "ALL";

export type ContentType = "AVRO" | "JSON";

export interface AdvancedTopicFormikValues extends FormikValues {
acknowledgement: Acknowledgement;
retentionTime: number;
Expand Down
16 changes: 13 additions & 3 deletions src/store/topic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { debouncedTask } from "../helpers/debouncedTask";
import {
Acknowledgement,
AuthModel,
ContentType,
MessagePreviewModel,
OwnerModel,
RetentionTimeModel,
Expand Down Expand Up @@ -41,7 +42,7 @@ export class Topic implements TopicModel {
@observable trackingEnabled: boolean;
@observable migratedFromJsonType: boolean;
@observable schemaIdAwareSerializationEnabled: boolean;
@observable contentType = "AVRO";
@observable contentType: ContentType;
@observable maxMessageSize = 10240;
@observable auth: AuthModel;
@observable createdAt: number;
Expand Down Expand Up @@ -155,8 +156,17 @@ export class Topic implements TopicModel {

private getModelFromForm(object: TopicFormikValues): TopicModel {
const value = this.model;
if (object.schema) {
value.schema = this.metadataRequired ? addMetadata(object.schema) : object.schema;
if (object.contentType) {
value.contentType = object.contentType;

switch (object.contentType) {
case "AVRO":
if (object.schema) {
value.schema = this.metadataRequired ? addMetadata(object.schema) : object.schema;
}
break;
default:
}
}
if (object.description) {
value.description = object.description;
Expand Down

0 comments on commit ae2327a

Please sign in to comment.