Skip to content

Commit 01de928

Browse files
committed
move function to a different file
1 parent 5daf7df commit 01de928

File tree

2 files changed

+54
-36
lines changed

2 files changed

+54
-36
lines changed

src/scratch.jsx

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { compose } from "redux";
66
import GUI, { AppStateHOC } from "@scratch/scratch-gui";
77
import ScratchIntegrationHOC from "./components/ScratchEditor/ScratchIntegrationHOC.jsx";
88
import dedupeScratchWarnings from "./utils/dedupeScratchWarnings.js";
9+
import scratchProjectSave from "./utils/scratchProjectSave.js";
910

1011
import ScratchStyles from "./assets/stylesheets/Scratch.scss";
1112

@@ -63,20 +64,6 @@ const handleScratchGuiAlert = (alertType) => {
6364
const generateNonce = () =>
6465
`${Math.random().toString(36).slice(2)}-${Date.now().toString(36)}`;
6566

66-
const buildQueryString = (params = {}) => {
67-
const searchParams = new URLSearchParams(
68-
Object.entries({
69-
original_id: params.originalId,
70-
is_copy: params.isCopy,
71-
is_remix: params.isRemix,
72-
title: params.title,
73-
}).filter(([, value]) => value !== undefined),
74-
);
75-
76-
const queryString = searchParams.toString();
77-
return queryString ? `?${queryString}` : "";
78-
};
79-
8067
if (!projectId) {
8168
console.error("project_id is required but not set");
8269
} else if (!apiUrl) {
@@ -108,29 +95,13 @@ if (!projectId) {
10895
event.data?.nonce === nonce;
10996

11097
const handleUpdateProjectData = async (currentProjectId, vmState, params) => {
111-
const creatingProject =
112-
currentProjectId === null || typeof currentProjectId === "undefined";
113-
const queryString = buildQueryString(params);
114-
const url = creatingProject
115-
? `${apiUrl}/api/scratch/projects/${queryString}`
116-
: `${apiUrl}/api/scratch/projects/${currentProjectId}${queryString}`;
117-
118-
const response = await scratchFetchApi.scratchFetch(url, {
119-
method: creatingProject ? "post" : "put",
120-
body: vmState,
121-
headers: {
122-
"Content-Type": "application/json",
123-
},
124-
credentials: "include",
98+
return scratchProjectSave({
99+
scratchFetchApi,
100+
apiUrl,
101+
currentProjectId,
102+
vmState,
103+
params,
125104
});
126-
127-
if (response.status !== 200) {
128-
throw response.status;
129-
}
130-
131-
const body = await response.json();
132-
body.id = creatingProject ? body["content-name"] : currentProjectId;
133-
return body;
134105
};
135106

136107
const mountGui = (accessToken) => {

src/utils/scratchProjectSave.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const buildQueryString = (params = {}) => {
2+
const searchParams = new URLSearchParams(
3+
Object.entries({
4+
original_id: params.originalId,
5+
is_copy: params.isCopy,
6+
is_remix: params.isRemix,
7+
title: params.title,
8+
}).filter(([, value]) => value !== undefined),
9+
);
10+
11+
const queryString = searchParams.toString();
12+
return queryString ? `?${queryString}` : "";
13+
};
14+
15+
const scratchProjectSave = async ({
16+
scratchFetchApi,
17+
apiUrl,
18+
currentProjectId,
19+
vmState,
20+
params,
21+
}) => {
22+
const creatingProject =
23+
currentProjectId === null || typeof currentProjectId === "undefined";
24+
const queryString = buildQueryString(params);
25+
const url = creatingProject
26+
? `${apiUrl}/api/scratch/projects/${queryString}`
27+
: `${apiUrl}/api/scratch/projects/${currentProjectId}${queryString}`;
28+
29+
const response = await scratchFetchApi.scratchFetch(url, {
30+
method: creatingProject ? "post" : "put",
31+
body: vmState,
32+
headers: {
33+
"Content-Type": "application/json",
34+
},
35+
credentials: "include",
36+
});
37+
38+
if (response.status !== 200) {
39+
throw response.status;
40+
}
41+
42+
const body = await response.json();
43+
body.id = creatingProject ? body["content-name"] : currentProjectId;
44+
return body;
45+
};
46+
47+
export default scratchProjectSave;

0 commit comments

Comments
 (0)