-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd0008f
commit 3e51066
Showing
9 changed files
with
434 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Table } from "ka-table"; | ||
import { DataType, EditingMode, SortingMode } from "ka-table/enums"; | ||
import "ka-table/style.css"; | ||
|
||
function CsvDataTable({ data, table }) { | ||
if (data === null || data.length === 0) { | ||
return null; | ||
} | ||
|
||
const columns = Object.keys(data[0]).map((key) => ({ | ||
key, | ||
title: key, | ||
dataType: DataType.String, | ||
})); | ||
|
||
const rows = data.map((row, index) => ({ ...row, id: index })); | ||
|
||
return ( | ||
<Table | ||
columns={columns} | ||
data={rows} | ||
table={table} | ||
editingMode={EditingMode.Cell} | ||
rowKeyField={"id"} | ||
sortingMode={SortingMode.Single} | ||
/> | ||
); | ||
} | ||
|
||
export default CsvDataTable; |
39 changes: 39 additions & 0 deletions
39
example-embedded-app/pages/csv-example/PrismaticAvatar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { CableTwoTone } from "@mui/icons-material"; | ||
import { Avatar } from "@mui/material"; | ||
import config from "prismatic/config"; | ||
import React from "react"; | ||
|
||
function PrismaticAvatar({ avatarUrl, token }) { | ||
const [src, setSrc] = React.useState(""); | ||
|
||
React.useEffect(() => { | ||
let mounted = true; | ||
if (avatarUrl) { | ||
fetch(`${config.prismaticUrl}${avatarUrl}`, { | ||
headers: { Authorization: `Bearer ${token}` }, | ||
}).then((response) => { | ||
response.json().then((data) => { | ||
if (mounted) { | ||
setSrc(data.url); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
return () => { | ||
mounted = false; | ||
}; | ||
}, []); | ||
|
||
if (!avatarUrl) { | ||
return ( | ||
<Avatar> | ||
<CableTwoTone /> | ||
</Avatar> | ||
); | ||
} | ||
|
||
return src ? <Avatar variant="rounded" src={src} /> : null; | ||
} | ||
|
||
export default PrismaticAvatar; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { | ||
Alert, | ||
Box, | ||
Button, | ||
Container, | ||
LinearProgress, | ||
TextField, | ||
} from "@mui/material"; | ||
import Papa from "papaparse"; | ||
import React, { Dispatch, SetStateAction } from "react"; | ||
import { Instance } from "./getInstances"; | ||
|
||
interface UploadCsvParams { | ||
fileName: string; | ||
uploadUrl: string; | ||
data: unknown[]; | ||
setUploadState: Dispatch<SetStateAction<UploadState>>; | ||
} | ||
|
||
function uploadCsv({ | ||
fileName, | ||
uploadUrl, | ||
data, | ||
setUploadState, | ||
}: UploadCsvParams) { | ||
setUploadState("uploading"); | ||
const csvData = Papa.unparse(data.map(({ id, ...rest }) => rest)); | ||
const formData = new FormData(); | ||
formData.append("file", csvData); | ||
formData.append("fileName", fileName); | ||
fetch(uploadUrl, { method: "post", body: formData }) | ||
.then(() => { | ||
setUploadState("success"); | ||
setTimeout(() => setUploadState("idle"), 4000); | ||
}) | ||
.catch(() => { | ||
setUploadState("failed"); | ||
}); | ||
} | ||
|
||
type UploadState = "idle" | "uploading" | "success" | "failed"; | ||
|
||
function ProgressIndicator({ state }: { state: UploadState }) { | ||
switch (state) { | ||
case "idle": | ||
return null; | ||
case "uploading": | ||
return ( | ||
<Box> | ||
<LinearProgress /> | ||
</Box> | ||
); | ||
case "success": | ||
return <Alert severity="success">Upload successful</Alert>; | ||
case "failed": | ||
return <Alert severity="error">Upload failed</Alert>; | ||
} | ||
} | ||
|
||
function UploadButtons({ | ||
data, | ||
instances, | ||
}: { | ||
data: any; | ||
instances: Instance[]; | ||
}) { | ||
const [fileName, setFileName] = React.useState(""); | ||
const [uploadState, setUploadState] = React.useState<UploadState>("idle"); | ||
|
||
return ( | ||
<Container> | ||
<hr /> | ||
<TextField | ||
onChange={({ target }) => { | ||
setFileName(target.value); | ||
}} | ||
label="File Name" | ||
/> | ||
{instances.map((instance) => ( | ||
<Button | ||
key={instance.integration.name} | ||
onClick={() => { | ||
uploadCsv({ | ||
fileName, | ||
uploadUrl: instance.webhookUrls["upload"], | ||
data, | ||
setUploadState, | ||
}); | ||
}} | ||
> | ||
Upload to {instance.integration.name} | ||
</Button> | ||
))} | ||
<ProgressIndicator state={uploadState} /> | ||
</Container> | ||
); | ||
} | ||
|
||
export default UploadButtons; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## CSV Load and Upload Example | ||
|
||
This example demonstrates interactivity between an application and a set of Prismatic instances that a customer has deployed. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import prismatic from "@prismatic-io/embedded"; | ||
|
||
export interface Instance { | ||
id: string; | ||
enabled: boolean; | ||
flowConfigs: FlowConfigs; | ||
integration: Integration; | ||
webhookUrls: Record<string, string>; | ||
} | ||
|
||
export interface FlowConfigs { | ||
nodes: FlowConfigsNode[]; | ||
} | ||
|
||
export interface FlowConfigsNode { | ||
flow: Flow; | ||
webhookUrl: string; | ||
} | ||
|
||
export interface Flow { | ||
name: string; | ||
} | ||
|
||
export interface Integration { | ||
id: string; | ||
name: string; | ||
avatarUrl: null | string; | ||
category: Category; | ||
} | ||
|
||
export enum Category { | ||
CSVStores = "CSV Stores", | ||
Communication = "Communication", | ||
Empty = "", | ||
} | ||
const query = `query getInstances { | ||
instances { | ||
nodes { | ||
id | ||
enabled | ||
flowConfigs { | ||
nodes { | ||
flow { | ||
name | ||
} | ||
webhookUrl | ||
} | ||
} | ||
integration { | ||
id | ||
name | ||
avatarUrl | ||
category | ||
} | ||
} | ||
} | ||
} | ||
`; | ||
|
||
const getInstances = (): Promise<Instance[]> => { | ||
return prismatic.graphqlRequest({ query }).then((response) => { | ||
const csvInstances = (response.data.instances.nodes as Instance[]).filter( | ||
(instance) => instance.integration.category === "CSV Stores", | ||
); | ||
// Make webhook URLs more accessible | ||
for (const instance of csvInstances) { | ||
instance.webhookUrls = Object.fromEntries( | ||
instance.flowConfigs.nodes.map((flowConfig) => [ | ||
flowConfig.flow.name, | ||
flowConfig.webhookUrl, | ||
]), | ||
); | ||
} | ||
return csvInstances; | ||
}); | ||
}; | ||
|
||
export default getInstances; |
Oops, something went wrong.