@@ -6,6 +6,7 @@ import { compose } from "redux";
66import GUI , { AppStateHOC } from "@scratch/scratch-gui" ;
77import ScratchIntegrationHOC from "./components/ScratchEditor/ScratchIntegrationHOC.jsx" ;
88import dedupeScratchWarnings from "./utils/dedupeScratchWarnings.js" ;
9+ import scratchProjectSave from "./utils/scratchProjectSave.js" ;
910
1011import ScratchStyles from "./assets/stylesheets/Scratch.scss" ;
1112
@@ -63,20 +64,6 @@ const handleScratchGuiAlert = (alertType) => {
6364const 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-
8067if ( ! 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 ) => {
0 commit comments