Skip to content

Commit 293dff4

Browse files
committed
add export button
1 parent dd9a08b commit 293dff4

File tree

4 files changed

+110
-32
lines changed

4 files changed

+110
-32
lines changed

app/pages/projects/[projectId].tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
useMutation,
1010
Routes,
1111
usePaginatedQuery,
12+
invoke,
1213
} from "blitz"
1314
import Layout from "app/core/layouts/Layout"
1415
import getProject from "app/projects/queries/getProject"
@@ -32,8 +33,15 @@ import {
3233
Text,
3334
} from "@chakra-ui/react"
3435
import { HiViewGridAdd, HiOutlineClipboardCopy } from "react-icons/hi"
35-
import { ChevronLeftIcon, ChevronRightIcon, DeleteIcon, EditIcon } from "@chakra-ui/icons"
36+
import {
37+
ChevronLeftIcon,
38+
ChevronRightIcon,
39+
DeleteIcon,
40+
EditIcon,
41+
DownloadIcon,
42+
} from "@chakra-ui/icons"
3643
import { CopyToClipboard } from "react-copy-to-clipboard"
44+
import { saveAs } from "file-saver"
3745

3846
const BreadCrumb = ({ project }) => {
3947
return (
@@ -148,6 +156,27 @@ export const Project = () => {
148156
<a>EDIT</a>
149157
</Button>
150158
</Link>
159+
<Button
160+
mr="2"
161+
colorScheme="teal"
162+
variant="solid"
163+
_hover={{ bg: "teal.400", borderColor: "teal.400" }}
164+
leftIcon={<DownloadIcon />}
165+
onClick={async () => {
166+
const { stubs } = await invoke(getStubs, { where: { projectId: projectId } })
167+
const stubsForExport = stubs.map(
168+
({ path, method, contentType, statusCode, response, sleep }) => {
169+
return { path, method, contentType, statusCode, response, sleep }
170+
}
171+
)
172+
const blob = new Blob([JSON.stringify(stubsForExport)], {
173+
type: "application/json; charset=utf-8",
174+
})
175+
saveAs(blob, `co-metub_p${project.id}.json`)
176+
}}
177+
>
178+
EXPORT
179+
</Button>
151180
<Button
152181
colorScheme="red"
153182
variant="solid"

app/pages/stubs/[stubId].tsx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ import {
1515
Text,
1616
} from "@chakra-ui/react"
1717
import getProject from "app/projects/queries/getProject"
18-
import { DeleteIcon, EditIcon } from "@chakra-ui/icons"
18+
import { DeleteIcon, EditIcon, DownloadIcon } from "@chakra-ui/icons"
1919
import { HiOutlineClipboardCopy } from "react-icons/hi"
2020
import { CopyToClipboard } from "react-copy-to-clipboard"
2121
import formatXml from "xml-formatter"
22+
import { saveAs } from "file-saver"
2223

2324
const BreadCrumb = ({ stub }) => {
2425
const [project] = useQuery(getProject, { id: stub.projectId })
@@ -93,6 +94,25 @@ export const Stub = () => {
9394
<a>EDIT</a>
9495
</Button>
9596
</Link>
97+
<Button
98+
mr="2"
99+
colorScheme="teal"
100+
variant="solid"
101+
_hover={{ bg: "teal.400", borderColor: "teal.400" }}
102+
leftIcon={<DownloadIcon />}
103+
onClick={async () => {
104+
const { path, method, contentType, statusCode, response, sleep } = stub
105+
const blob = new Blob(
106+
[JSON.stringify({ path, method, contentType, statusCode, response, sleep })],
107+
{
108+
type: "application/json; charset=utf-8",
109+
}
110+
)
111+
saveAs(blob, `co-metub_s${stub.id}.json`)
112+
}}
113+
>
114+
EXPORT
115+
</Button>
96116
<Button
97117
colorScheme="red"
98118
variant="solid"

package-lock.json

Lines changed: 57 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
"@prisma/client": "4.0.0",
3535
"better-sqlite3": "7.6.2",
3636
"blitz": "0.45.5",
37+
"file-saver": "2.0.5",
3738
"final-form": "4.20.7",
3839
"framer-motion": "6.5.1",
3940
"react": "18.2.0",
@@ -46,6 +47,7 @@
4647
},
4748
"devDependencies": {
4849
"@types/react": "18.0.1",
50+
"@types/file-saver": "2.0.5",
4951
"eslint": "8.20.0",
5052
"husky": "8.0.1",
5153
"lint-staged": "13.0.3",

0 commit comments

Comments
 (0)