From 005e16d6183c9a8967ff84640b72037f68825ee8 Mon Sep 17 00:00:00 2001 From: Matatjahu Date: Thu, 7 Oct 2021 20:18:45 +0200 Subject: [PATCH] feat: add integration with AsyncAPI Server API --- src/components/Editor/EditorDropdown.tsx | 6 ++ src/components/Modals/GeneratorModal.tsx | 73 ++++++++++++++++++++++++ src/components/Modals/index.tsx | 1 + src/services/generator.service.ts | 8 +++ src/services/index.ts | 2 + src/services/server-api.service.ts | 30 ++++++++++ 6 files changed, 120 insertions(+) create mode 100644 src/components/Modals/GeneratorModal.tsx create mode 100644 src/services/generator.service.ts create mode 100644 src/services/server-api.service.ts diff --git a/src/components/Editor/EditorDropdown.tsx b/src/components/Editor/EditorDropdown.tsx index 81fbd6ab77..01e8e41ded 100644 --- a/src/components/Editor/EditorDropdown.tsx +++ b/src/components/Editor/EditorDropdown.tsx @@ -4,6 +4,7 @@ import { FaEllipsisH } from 'react-icons/fa'; import { ConvertModal, + GeneratorModal, ImportBase64Modal, ImportURLModal, } from '../Modals'; @@ -177,6 +178,11 @@ export const EditorDropdown: React.FunctionComponent = () = +
+
  • + +
  • +
  • {saveFileButton} diff --git a/src/components/Modals/GeneratorModal.tsx b/src/components/Modals/GeneratorModal.tsx new file mode 100644 index 0000000000..b77b4e56a8 --- /dev/null +++ b/src/components/Modals/GeneratorModal.tsx @@ -0,0 +1,73 @@ +import React, { useState } from 'react'; +import toast from 'react-hot-toast'; +import { ConfirmModal } from './index'; + +import { GeneratorService, ServerAPIService } from '../../services'; + +export const GeneratorModal: React.FunctionComponent = () => { + const [template, setTemplate] = useState(''); + + const onSubmit = () => { + toast.promise(ServerAPIService.generate({ template }), { + loading: 'Generating...', + success: ( +
    + + Succesfully generated! + +
    + ), + error: ( +
    + + Failed to generate. + +
    + ), + }); + }; + + return ( + + Generate template + + } + onSubmit={onSubmit} + > +
    +
    + + +
    +
    +
    + ); +}; diff --git a/src/components/Modals/index.tsx b/src/components/Modals/index.tsx index faf682a02f..e802d6e3eb 100644 --- a/src/components/Modals/index.tsx +++ b/src/components/Modals/index.tsx @@ -1,5 +1,6 @@ export * from './ConfirmModal'; export * from './ConvertModal'; export * from './ConvertToLatestModal'; +export * from './GeneratorModal'; export * from './ImportBase64Modal'; export * from './ImportURLModal'; diff --git a/src/services/generator.service.ts b/src/services/generator.service.ts new file mode 100644 index 0000000000..ac5fd58fc1 --- /dev/null +++ b/src/services/generator.service.ts @@ -0,0 +1,8 @@ +export class GeneratorService { + static getTemplates() { + return { + 'html-template': '@asyncapi/html-template', + 'markdown-template': '@asyncapi/markdown-template', + }; + } +} diff --git a/src/services/index.ts b/src/services/index.ts index 32ff6bef4b..d9783f58bc 100644 --- a/src/services/index.ts +++ b/src/services/index.ts @@ -1,5 +1,7 @@ export * from './editor.service'; export * from './format.service'; +export * from './generator.service'; export * from './monaco.service'; export * from './navigation.service'; +export * from './server-api.service'; export * from './specification.service'; diff --git a/src/services/server-api.service.ts b/src/services/server-api.service.ts new file mode 100644 index 0000000000..d700b05adb --- /dev/null +++ b/src/services/server-api.service.ts @@ -0,0 +1,30 @@ +import fileDownload from 'js-file-download'; + +import state from '../state'; + +export class ServerAPIService { + static serverPath = 'http://localhost:5000'; + + static async generate(data: { + template: string, + }) { + const editorState = state.editor; + + return fetch(`${this.serverPath}/generate`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + ...data, + templateParams: {}, + asyncapi: editorState.editorValue.get(), + }), + }) + .then(response => response.blob()) + .then(zipFile => { + console.log(zipFile); + fileDownload(zipFile, 'asyncapi.zip'); + }); + } +}