Skip to content

Commit

Permalink
feat embed modifications (#244)
Browse files Browse the repository at this point in the history
  • Loading branch information
chavda-bhavik authored May 18, 2023
2 parents 2324f56 + 0ac8b76 commit a45b277
Show file tree
Hide file tree
Showing 16 changed files with 225 additions and 447 deletions.
42 changes: 23 additions & 19 deletions apps/demo/components/Actions/Actions.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from 'react';
import React, { useCallback } from 'react';
import { saveAs } from 'file-saver';
import { Button } from '@impler/react';
import { useImpler } from '@impler/react';
import { IUpload } from '@impler/shared';
import { constants, variables } from '@config';
import { useAppState } from '@context/app.context';
Expand All @@ -25,16 +25,25 @@ const Actions = ({ PROJECT_ID, ACCESS_TOKEN, PRIMARY_COLOR, TEMPLATE }: ActionPr
setShowInvalidRecords,
setTotalRecords,
} = useAppState();
const onUploadComplete = useCallback(
(uploadData: IUpload) => {
setUpload(uploadData);
setPage(variables.ONE);
setShowInvalidRecords(false);
setTotalRecords(uploadData.validRecords);
setHasInvalidRecords(uploadData.invalidRecords > variables.ZERO);
},
[setHasInvalidRecords, setPage, setShowInvalidRecords, setTotalRecords, setUpload]
);
const { isImplerInitiated, showWidget } = useImpler({
projectId: PROJECT_ID,
accessToken: ACCESS_TOKEN,
templateId: TEMPLATE,
primaryColor: PRIMARY_COLOR,
onUploadComplete: onUploadComplete,
});
const { classes } = useStyles(showInvalidRecords);

const onUploadComplete = (uploadData: IUpload) => {
setUpload(uploadData);
setPage(variables.ONE);
setShowInvalidRecords(false);
setTotalRecords(uploadData.validRecords);
setHasInvalidRecords(uploadData.invalidRecords > variables.ZERO);
};

const onShowInvalidChanges = (status: boolean) => {
setShowInvalidRecords(status);
setPage(variables.ONE);
Expand All @@ -48,14 +57,9 @@ const Actions = ({ PROJECT_ID, ACCESS_TOKEN, PRIMARY_COLOR, TEMPLATE }: ActionPr
return (
<div className={classes.wrapper}>
<Flex direction="row" align="center" gap="sm">
<Button
projectId={PROJECT_ID}
accessToken={ACCESS_TOKEN}
templateId={TEMPLATE}
primaryColor={PRIMARY_COLOR}
className={classes.button}
onUploadComplete={onUploadComplete}
/>
<MantineButton onClick={showWidget} className={classes.button} disabled={!isImplerInitiated}>
Import temperature data
</MantineButton>
{hasInvalidRecords && (
<Switch
label="Show Invalid Data"
Expand All @@ -74,7 +78,7 @@ const Actions = ({ PROJECT_ID, ACCESS_TOKEN, PRIMARY_COLOR, TEMPLATE }: ActionPr
onClick={() => saveAs(constants.DATA_FILE_URL, constants.DATA_FILE_NAME)}
className={classes.button}
>
Download Temperature Data
Download sample data
</MantineButton>
</div>
);
Expand Down
48 changes: 0 additions & 48 deletions apps/web/hooks/useImpler.ts

This file was deleted.

1 change: 1 addition & 0 deletions apps/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@amplitude/analytics-browser": "^1.10.3",
"@emotion/react": "^11.10.5",
"@emotion/server": "^11.10.0",
"@impler/react": "workspace:^0.2.0",
"@impler/shared": "workspace:^0.2.0",
"@mantine/carousel": "6.0.6",
"@mantine/core": "6.0.6",
Expand Down
13 changes: 3 additions & 10 deletions apps/web/pages/imports/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import Link from 'next/link';
import { useEffect } from 'react';
import dynamic from 'next/dynamic';
import { GetServerSideProps } from 'next';
import { Flex, Group, Title } from '@mantine/core';

import { commonApi } from '@libs/api';
import { ITemplate } from '@impler/shared';
import { useImpler } from '@hooks/useImpler';
import { useImpler } from '@impler/react';
import { useImportDetails } from '@hooks/useImportDetails';
import { API_KEYS, CONSTANTS, ROUTES, colors } from '@config';

Expand All @@ -30,20 +29,14 @@ interface ImportDetailProps {

export default function ImportDetails({ template }: ImportDetailProps) {
const { onUpdateClick, onDeleteClick, templateData, profile, onSpreadsheetImported } = useImportDetails({ template });
const { init, onImportClick, isImplerInitiated } = useImpler({
const { showWidget, isImplerInitiated } = useImpler({
templateId: template._id,
projectId: template._projectId,
accessToken: profile?.accessToken,
primaryColor: colors.blue,
onUploadComplete: onSpreadsheetImported,
});

useEffect(() => {
if (profile?.accessToken && template._projectId) {
init();
}
}, [init, profile?.accessToken, template._projectId]);

return (
<Flex gap="lg" direction="column" h="100%">
<Flex justify="space-between">
Expand All @@ -54,7 +47,7 @@ export default function ImportDetails({ template }: ImportDetailProps) {
<Title order={2}>{templateData.name}</Title>
</Group>
<Group spacing="xs">
<Button disabled={!isImplerInitiated} onClick={onImportClick}>
<Button disabled={!isImplerInitiated} onClick={showWidget}>
Import
</Button>
<Button onClick={onUpdateClick}>
Expand Down
38 changes: 27 additions & 11 deletions libs/embed/src/embed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Impler {

private initPayload: any;

private isWidgetReady: boolean = false;

private isAuthenticated: boolean = false;

private authenticationError?: string;
Expand Down Expand Up @@ -95,6 +97,17 @@ class Impler {
if (!!event && !!event.data && !!event.data.type) {
// eslint-disable-next-line default-case
switch (event.data.type) {
case EventTypesEnum.WIDGET_READY:
this.isWidgetReady = true;
this.iframe?.contentWindow?.postMessage(
{
type: EventTypesEnum.INIT_IFRAME,
value: this.initPayload,
},
'*'
);
this.postMessageToContentWindow(EventTypesEnum.WIDGET_READY);
break;
case EventTypesEnum.CLOSE_WIDGET:
this.hideWidget();
this.postMessageToContentWindow(event.data.type);
Expand All @@ -120,19 +133,9 @@ class Impler {
}

initializeIframe = (projectId: string) => {
let iframe: HTMLIFrameElement;
if (!document.getElementById(IFRAME_ID)) {
const iframe = document.createElement('iframe');
window.addEventListener(
'message',
(event) => {
if (!event.target || event?.data?.type !== EventTypesEnum.WIDGET_READY) {
return;
}

iframe?.contentWindow?.postMessage({ type: EventTypesEnum.INIT_IFRAME, value: this.initPayload }, '*');
},
true
);

iframe.style.backgroundColor = 'transparent';
iframe.src = `${IFRAME_URL}/${projectId}?`;
Expand All @@ -149,6 +152,19 @@ class Impler {
iframe.style.zIndex = Number.MAX_SAFE_INTEGER.toString();
(iframe as any).crossorigin = 'anonymous';
this.iframe = iframe;
} else {
iframe = document.getElementById(IFRAME_ID) as HTMLIFrameElement;
this.iframe = iframe;
}
if (this.isWidgetReady) {
this.iframe?.contentWindow?.postMessage(
{
type: EventTypesEnum.INIT_IFRAME,
value: this.initPayload,
},
'*'
);
this.postMessageToContentWindow(EventTypesEnum.WIDGET_READY);
}
};

Expand Down
3 changes: 1 addition & 2 deletions packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
"typescript": "^4.4.4"
},
"dependencies": {
"@impler/shared": "^0.2.0",
"styled-components": "^5.3.6"
"@impler/shared": "^0.2.0"
}
}
100 changes: 0 additions & 100 deletions packages/react/src/components/button/Button.tsx

This file was deleted.

3 changes: 3 additions & 0 deletions packages/react/src/components/button/Button.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ export type EventCalls =
}
| {
type: EventTypesEnum.CLOSE_WIDGET;
}
| {
type: EventTypesEnum.WIDGET_READY;
};
2 changes: 0 additions & 2 deletions packages/react/src/components/button/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/react/src/components/index.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/react/src/config/texts.config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const TEXTS = {
PROJECTID_NOT_SPECIFIED: 'It looks like you have not specified projectId to the import button which is required!',
IMPLER_UNDEFINED_ERROR: 'It looks like you have not added embed script or your embed script path is incorrect!',
IMPLER_NOT_INITIATED: 'It looks like Impler is not initiated!',
};
1 change: 1 addition & 0 deletions packages/react/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useImpler';
Loading

0 comments on commit a45b277

Please sign in to comment.