diff --git a/packages/noodl-editor/src/editor/src/contexts/PluginContext/commands/cloud-service.ts b/packages/noodl-editor/src/editor/src/contexts/PluginContext/commands/cloud-service.ts new file mode 100644 index 0000000..4e0302a --- /dev/null +++ b/packages/noodl-editor/src/editor/src/contexts/PluginContext/commands/cloud-service.ts @@ -0,0 +1,72 @@ +import { CloudService } from '@noodl-models/CloudServices'; +import { ProjectModel } from '@noodl-models/projectmodel'; +import { setCloudServices } from '@noodl-models/projectmodel.editor'; + +import { ToastLayer } from '../../../views/ToastLayer'; + +export type Command = { + kind: 'cloud-service'; + use?: boolean; + + name: string; + description?: string; + endpoint: string; + appId: string; + masterKey: string; +}; + +export async function execute(command: Command, _event: MessageEvent) { + const environment = await getOrCreate(command); + + if (command.use) { + setCloudServices(ProjectModel.instance, environment); + } +} + +async function getOrCreate(command: Command) { + const cloudServices = await CloudService.instance.backend.fetch(); + const environment = cloudServices.find((c) => c.url === command.endpoint); + if (environment) { + if ( + environment.name !== command.name || + environment.description !== command.description || + environment.appId !== command.appId || + environment.masterKey !== command.masterKey + ) { + await CloudService.instance.backend.update({ + id: environment.id, + url: environment.url, + + // Update the existing environment + name: command.name, + description: command.description, + appId: command.appId, + masterKey: command.masterKey + }); + + ToastLayer.showSuccess(`Cloud service "${command.name}" updated.`); + } + + return { + id: environment.id, + endpoint: environment.url, + appId: command.appId + }; + } else { + const create = await CloudService.instance.backend.create({ + name: command.name, + description: command.description, + appId: command.appId, + url: command.endpoint, + masterKey: command.masterKey + }); + + ToastLayer.showSuccess(`Cloud service "${command.name}" added successfully.`); + + return { + id: create.id, + endpoint: create.url, + appId: create.appId + }; + } +} diff --git a/packages/noodl-editor/src/editor/src/contexts/PluginContext/iframe.ts b/packages/noodl-editor/src/editor/src/contexts/PluginContext/iframe.ts index e1e80a6..a36d227 100644 --- a/packages/noodl-editor/src/editor/src/contexts/PluginContext/iframe.ts +++ b/packages/noodl-editor/src/editor/src/contexts/PluginContext/iframe.ts @@ -1,11 +1,13 @@ +import * as CloudService from './commands/cloud-service'; import * as Notify from './commands/notify'; import * as UploadAwsS3 from './commands/upload-aws-s3'; -type IFrameCommand = Notify.Command | UploadAwsS3.Command; +type IFrameCommand = Notify.Command | UploadAwsS3.Command | CloudService.Command; const commands: Record Promise> = { notify: Notify.execute, - 'upload-aws-s3': UploadAwsS3.execute + 'upload-aws-s3': UploadAwsS3.execute, + 'cloud-service': CloudService.execute }; export function commandEventHandler(event: MessageEvent) { diff --git a/packages/noodl-editor/src/editor/src/views/panels/CloudServicePanel/CloudServicePanel.tsx b/packages/noodl-editor/src/editor/src/views/panels/CloudServicePanel/CloudServicePanel.tsx index 3f59738..8a32282 100644 --- a/packages/noodl-editor/src/editor/src/views/panels/CloudServicePanel/CloudServicePanel.tsx +++ b/packages/noodl-editor/src/editor/src/views/panels/CloudServicePanel/CloudServicePanel.tsx @@ -6,8 +6,6 @@ import { CloudService } from '@noodl-models/CloudServices'; import { ActivityIndicator } from '@noodl-core-ui/components/common/ActivityIndicator'; import { IconName, IconSize } from '@noodl-core-ui/components/common/Icon'; import { IconButton } from '@noodl-core-ui/components/inputs/IconButton'; -import { PrimaryButton } from '@noodl-core-ui/components/inputs/PrimaryButton'; -import { Box } from '@noodl-core-ui/components/layout/Box'; import { ConditionalContainer } from '@noodl-core-ui/components/layout/ConditionalContainer'; import { Container } from '@noodl-core-ui/components/layout/Container'; import { VStack } from '@noodl-core-ui/components/layout/Stack';