Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix][WRS-1190] Don't execute SDK load event if document is not loaded #237

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
2 changes: 1 addition & 1 deletion src/DemoDocumentLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class DemoDocumentLoader {

onProjectInfoRequested: (projectId: string) => Promise<Project>;

onProjectDocumentRequested: (projectId: string) => Promise<string>;
onProjectDocumentRequested: (projectId: string) => Promise<string | null>;

onProjectLoaded: (project: Project) => void;

Expand Down
41 changes: 21 additions & 20 deletions src/MainContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ import {
useConnectorAuthenticationResult,
} from './components/connector-authentication';
import LeftPanel from './components/layout-panels/leftPanel/LeftPanel';
import Navbar from './components/navbar/Navbar';
import StudioNavbar from './components/navbar/studioNavbar/StudioNavbar';
import MobileVariablesTray from './components/variables/MobileVariablesTray';
import AppProvider from './contexts/AppProvider';
import { useAuthToken } from './contexts/AuthTokenProvider';
import ShortcutProvider from './contexts/ShortcutManager/ShortcutProvider';
import { useSubscriberContext } from './contexts/Subscriber';
import { UiConfigContextProvider } from './contexts/UiConfigContext';
import { VariablePanelContextProvider } from './contexts/VariablePanelContext';
import { SuiCanvas } from './MainContent.styles';
import { Project, ProjectConfig } from './types/types';
import { getDataIdForSUI, getDataTestIdForSUI } from './utils/dataIds';
import MobileVariablesTray from './components/variables/MobileVariablesTray';
import StudioNavbar from './components/navbar/studioNavbar/StudioNavbar';
import Navbar from './components/navbar/Navbar';
import { APP_WRAPPER_ID } from './utils/constants';
import ShortcutProvider from './contexts/ShortcutManager/ShortcutProvider';
import { SuiCanvas } from './MainContent.styles';
import { useAuthToken } from './contexts/AuthTokenProvider';
import AppProvider from './contexts/AppProvider';
import { getDataIdForSUI, getDataTestIdForSUI } from './utils/dataIds';

declare global {
interface Window {
Expand All @@ -48,7 +48,7 @@ interface MainContentProps {
}

function MainContent({ projectConfig, updateToken: setAuthToken }: MainContentProps) {
const [fetchedDocument, setFetchedDocument] = useState('');
const [fetchedDocument, setFetchedDocument] = useState<string | null>(null);
const [variables, setVariables] = useState<Variable[]>([]);
const [canUndo, setCanUndo] = useState(false);
const [canRedo, setCanRedo] = useState(false);
Expand Down Expand Up @@ -266,18 +266,19 @@ function MainContent({ projectConfig, updateToken: setAuthToken }: MainContentPr
}, [currentProject?.template?.id]);

useEffect(() => {
const setHandTool = async () => {
(async () => {
await window.StudioUISDK.configuration.setValue(WellKnownConfigurationKeys.GraFxStudioAuthToken, authToken);
})();
}, [authToken]);

useEffect(() => {
(async () => {
await window.StudioUISDK.tool.setHand();
};
setHandTool();
const loadDocument = async () => {
if (authToken) {
await window.StudioUISDK.configuration.setValue(
WellKnownConfigurationKeys.GraFxStudioAuthToken,
authToken,
);
}
})();
}, []);

useEffect(() => {
const loadDocument = async () => {
if (!fetchedDocument) return;

await window.StudioUISDK.document.load(fetchedDocument).then((res) => {
Expand All @@ -301,7 +302,7 @@ function MainContent({ projectConfig, updateToken: setAuthToken }: MainContentPr
};

loadDocument();
}, [authToken, fetchedDocument]);
}, [fetchedDocument]);

return (
<AppProvider isDocumentLoaded={isDocumentLoaded} isAnimationPlaying={animationStatus}>
Expand Down
13 changes: 5 additions & 8 deletions src/StudioProjectLoader.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { AxiosError, AxiosResponse } from 'axios';
import { DownloadFormats, WellKnownConfigurationKeys } from '@chili-publish/studio-sdk';
import axios, { AxiosError, AxiosResponse } from 'axios';
import {
DownloadLinkResult,
HttpHeaders,
Expand Down Expand Up @@ -74,7 +74,7 @@ export class StudioProjectLoader {
return this.cachedProject;
};

public onProjectDocumentRequested = async (): Promise<string> => {
public onProjectDocumentRequested = async (): Promise<string | null> => {
const fallbackDownloadUrl = `${this.graFxStudioEnvironmentApiBaseUrl}/projects/${this.projectId}/document`;
return StudioProjectLoader.fetchDocument(this.projectDownloadUrl ?? fallbackDownloadUrl, this.authToken);
};
Expand Down Expand Up @@ -132,22 +132,19 @@ export class StudioProjectLoader {
);
};

private static fetchDocument = async (templateUrl: string, token: string): Promise<string> => {
private static fetchDocument = async (templateUrl: string, token: string): Promise<string | null> => {
const url = templateUrl;
if (url) {
const fetchPromise = axios.get(url, { headers: { Authorization: `Bearer ${token}` } });
return fetchPromise
.then((response) => {
return response;
})
.then((res) => {
return JSON.stringify(res.data);
})
.catch(() => {
return '{}';
return null;
});
}
return '{}';
return null;
};

private saveDocument = async (
Expand Down
2 changes: 1 addition & 1 deletion src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

constructor(selector: string, projectConfig: ProjectConfig) {
const container = document.getElementById(selector || 'sui-root');
this.root = createRoot(container!);

Check warning on line 32 in src/main.tsx

View workflow job for this annotation

GitHub Actions / build (20)

Forbidden non-null assertion
this.root.render(
<React.StrictMode>
<App projectConfig={projectConfig} />
Expand Down Expand Up @@ -110,7 +110,7 @@
featureFlags: Record<string, boolean> | undefined,
onSandboxModeToggle: (() => void) | undefined,
onProjectInfoRequested: (projectId: string) => Promise<Project>,
onProjectDocumentRequested: (projectId: string) => Promise<string>,
onProjectDocumentRequested: (projectId: string) => Promise<string | null>,
onProjectSave: (generateJson: () => Promise<string>) => Promise<Project>,
onProjectLoaded: (project: Project) => void,
onAuthenticationRequested: () => string,
Expand Down
24 changes: 22 additions & 2 deletions src/tests/StudioProjectLoader.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import axios, { AxiosError } from 'axios';
import { WellKnownConfigurationKeys } from '@chili-publish/studio-sdk';
import { Project } from '../types/types';
import axios, { AxiosError } from 'axios';
import { StudioProjectLoader } from '../StudioProjectLoader';
import { Project } from '../types/types';

jest.mock('axios');

Expand Down Expand Up @@ -111,6 +111,26 @@ describe('StudioProjectLoader', () => {
headers: { Authorization: 'Bearer mockAuthToken' },
});
});

it('should return "null" in case of error', async () => {
(axios.get as jest.Mock).mockRejectedValueOnce({});
const loader = new StudioProjectLoader(
mockProjectId,
mockGraFxStudioEnvironmentApiBaseUrl,
mockAuthToken,
false,
mockRefreshTokenAction,
mockProjectDownloadUrl,
mockProjectUploadUrl,
);

const result = await loader.onProjectDocumentRequested();

expect(result).toBeNull();
expect(axios.get).toHaveBeenCalledWith(mockProjectDownloadUrl, {
headers: { Authorization: 'Bearer mockAuthToken' },
});
});
});

describe('onProjectLoaded', () => {
Expand Down
8 changes: 4 additions & 4 deletions src/types/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ITheme } from '@chili-publish/grafx-shared-components';
import { DownloadFormats } from '@chili-publish/studio-sdk';
import { AxiosError, AxiosResponse } from 'axios';
import { ITheme } from '@chili-publish/grafx-shared-components';
import { ConnectorAuthenticationResult } from './ConnectorAuthenticationResult';

export type FeatureFlagsType = Record<string, boolean>;
Expand All @@ -17,7 +17,7 @@
featureFlags?: FeatureFlagsType;
onSandboxModeToggle?: () => void;
onProjectInfoRequested: (projectId: string) => Promise<Project>;
onProjectDocumentRequested: (projectId: string) => Promise<string>;
onProjectDocumentRequested: (projectId: string) => Promise<string | null>;
onProjectLoaded: (project: Project) => void;
onProjectSave: (generateJson: () => Promise<string>) => Promise<Project>;
onAuthenticationRequested: () => string;
Expand All @@ -31,7 +31,7 @@
) => Promise<DownloadLinkResult>;
overrideEngineUrl?: string;
onFetchOutputSettings?: (_?: string) => Promise<UserInterfaceWithOutputSettings | null>;
onFetchUserInterfaces?: () => Promise<AxiosResponse<PaginatedResponse<UserInterface>, any>>;

Check warning on line 34 in src/types/types.ts

View workflow job for this annotation

GitHub Actions / build (20)

Unexpected any. Specify a different type
onConnectorAuthenticationRequested?: (connectorId: string) => Promise<ConnectorAuthenticationResult>;
}

Expand All @@ -57,7 +57,7 @@

export interface StudioConfig extends DefaultStudioConfig {
onProjectInfoRequested: () => Promise<Project>;
onProjectDocumentRequested: () => Promise<string>;
onProjectDocumentRequested: () => Promise<string | null>;
onProjectSave: (generateJson: () => Promise<string>) => Promise<Project>;
}

Expand Down Expand Up @@ -177,7 +177,7 @@
featureFlags?: Record<string, boolean>;
onSandboxModeToggle?: () => void;
onProjectInfoRequested?: (projectId: string) => Promise<Project>;
onProjectDocumentRequested?: (projectId: string) => Promise<string>;
onProjectDocumentRequested?: (projectId: string) => Promise<string | null>;
onProjectSave?: (generateJson: () => Promise<string>) => Promise<Project>;
onProjectLoaded?: (project: Project) => void;
onAuthenticationRequested?: () => string;
Expand Down
Loading