Skip to content

Commit

Permalink
refactor: remove leftover code (#285)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Mar 31, 2021
1 parent 6b2807c commit ed84be2
Show file tree
Hide file tree
Showing 39 changed files with 624 additions and 3,379 deletions.
5 changes: 0 additions & 5 deletions library/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion library/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
"constate": "^1.2.0",
"dompurify": "^2.1.1",
"markdown-it": "^11.0.1",
"merge": "^2.1.0",
"openapi-sampler": "^1.0.0-beta.15",
"react-use": "^12.2.0"
},
Expand Down
11 changes: 0 additions & 11 deletions library/src/components/Tree.tsx

This file was deleted.

1 change: 0 additions & 1 deletion library/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ export * from './Href';
export * from './Markdown';
export * from './Tag';
export * from './Tags';
export * from './Tree';
export * from './Toggle';
export * from './CollapseButton';
16 changes: 0 additions & 16 deletions library/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,6 @@ export const SERVER_SECURITY_COLUMN_NAMES: TableColumnName[] = [
'Description',
];

export const SCHEMA_COLUMN_NAMES: TableColumnName[] = [
'Name',
'Title',
'Type',
'Format',
'Default',
'Description',
];

export const SERVER_VARIABLES_COLUMN_NAMES: TableColumnName[] = [
'Name',
'Default value',
'Possible values',
'Description',
];

export enum CONTAINER_LABELS {
INFO = 'info',
CHANNELS = 'channels',
Expand Down
90 changes: 30 additions & 60 deletions library/src/containers/AsyncApi/AsyncApi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,57 @@ import React, { Component } from 'react';
import { AsyncAPIDocument } from '@asyncapi/parser';

import {
AsyncAPI,
isFetchingSchemaInterface,
NullableAsyncApi,
ErrorObject,
AsyncApiProps,
PropsSchema,
} from '../../types';
import { ConfigInterface, defaultConfig } from '../../config';
import { bemClasses, stateHelpers, Parser } from '../../helpers';
import { parser, bemClasses, stateHelpers } from '../../helpers';
import { CSS_PREFIX } from '../../constants';
import { useSpec, useExpandedContext, useChangeHashContext } from '../../store';

import { ErrorComponent } from '../Error/Error';
import { InfoComponent } from '../Info/NewInfo';
import { ServersComponent } from '../Servers/Servers';
import { Operations } from '../Channels/NewOperations';
import { Messages } from '../Messages/NewMessages';
import { SchemasComponent } from '../Schemas/Schemas';
import { Operations } from '../Channels/Operations';
import { Messages } from '../Messages/Messages';

export interface AsyncApiProps {
schema: PropsSchema;
config?: Partial<ConfigInterface>;
}

interface AsyncAPIState {
validatedSchema: NullableAsyncApi;
asyncapi: AsyncAPIDocument | null;
asyncapi?: AsyncAPIDocument;
error?: ErrorObject;
}

const defaultAsyncApi: AsyncAPI = {
asyncapi: '2.0.0-rc2',
info: {
title: 'AsyncApi example title',
version: '1.0.0',
},
channels: {},
};

class AsyncApiComponent extends Component<AsyncApiProps, AsyncAPIState> {
state: AsyncAPIState = {
validatedSchema: defaultAsyncApi,
asyncapi: null,
asyncapi: undefined,
error: undefined,
};
private readonly parser: Parser;

constructor(props: AsyncApiProps) {
super(props);
this.parser = new Parser();
}

async componentDidMount() {
this.parseSchema(
this.props.schema,
this.props.config && this.props.config.parserOptions,
);
this.updateState(this.props.schema, this.props.config);
}

async componentDidUpdate(prevProps: AsyncApiProps) {
const { schema } = prevProps;
const oldSchema = prevProps.schema;
const newSchema = this.props.schema;

if (schema !== this.props.schema) {
this.parseSchema(
this.props.schema,
this.props.config && this.props.config.parserOptions,
);
if (oldSchema !== newSchema) {
this.updateState(newSchema, this.props.config);
}
}

render() {
const { config } = this.props;
const { validatedSchema, asyncapi, error } = this.state;
const { asyncapi, error } = this.state;
const concatenatedConfig: ConfigInterface = {
...defaultConfig,
...config,
Expand All @@ -83,11 +66,7 @@ class AsyncApiComponent extends Component<AsyncApiProps, AsyncAPIState> {
},
};

if (
!validatedSchema ||
!Object.keys(validatedSchema).length ||
asyncapi === null
) {
if (asyncapi === undefined) {
if (!error) {
return null;
}
Expand All @@ -100,12 +79,12 @@ class AsyncApiComponent extends Component<AsyncApiProps, AsyncAPIState> {

bemClasses.setSchemaID(concatenatedConfig.schemaID);
const numberOfElement = stateHelpers.calculateNumberOfElements({
spec: validatedSchema,
spec: asyncapi.json(),
showConfig: concatenatedConfig.show,
});
const initialExpandedElements = stateHelpers.calculateInitialExpandedElements(
{
spec: validatedSchema,
spec: asyncapi.json(),
showConfig: concatenatedConfig.show,
expandConfig: concatenatedConfig.expand || {},
},
Expand All @@ -132,43 +111,34 @@ class AsyncApiComponent extends Component<AsyncApiProps, AsyncAPIState> {
/>
)}
{concatenatedConfig.show.channels && <Operations />}
{validatedSchema.components && (
<section className={bemClasses.element(`components`)}>
{concatenatedConfig.show.messages && <Messages />}
{concatenatedConfig.show.schemas && (
<SchemasComponent
expand={
concatenatedConfig.expand &&
concatenatedConfig.expand.schemas
}
/>
)}
</section>
)}
{concatenatedConfig.show.messages && <Messages />}
</main>
</useChangeHashContext.Provider>
</useExpandedContext.Provider>
</useSpec.Provider>
);
}

private updateState(schema: PropsSchema, config?: Partial<ConfigInterface>) {
if (typeof schema === 'function' && schema.name === 'AsyncAPIDocument') {
this.setState({ asyncapi: schema });
return;
}
this.parseSchema(schema, config && config.parserOptions);
}

private async parseSchema(schema: PropsSchema, parserOptions?: any) {
if (isFetchingSchemaInterface(schema)) {
const parsedFromUrl = await this.parser.parseFromUrl(
schema,
parserOptions,
);
const parsedFromUrl = await parser.parseFromUrl(schema, parserOptions);
this.setState({
validatedSchema: parsedFromUrl.data,
asyncapi: parsedFromUrl.asyncapi,
error: parsedFromUrl.error,
});
return;
}

const parsed = await this.parser.parse(schema, parserOptions);
const parsed = await parser.parse(schema, parserOptions);
this.setState({
validatedSchema: parsed.data,
asyncapi: parsed.asyncapi,
error: parsed.error,
});
Expand Down
6 changes: 3 additions & 3 deletions library/src/containers/Bindings/Bindings.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';

import { SchemaComponent } from '../Schemas/NewSchema';
import { Schema } from '../Schemas/Schema';

import { SchemaHelpers } from '../../helpers';

Expand All @@ -16,7 +16,7 @@ export const Bindings: React.FunctionComponent<Props> = ({
if (!bindings || !Object.keys(bindings).length) {
return null;
}
const schema = SchemaHelpers.jsonToSchema(bindings);

return <SchemaComponent schemaName={name} schema={schema} />;
const schema = SchemaHelpers.jsonToSchema(bindings);
return <Schema schemaName={name} schema={schema} />;
};
Loading

0 comments on commit ed84be2

Please sign in to comment.