Skip to content

Commit 3d02b13

Browse files
committed
feat: support hash paramter to load scratch project
1 parent 50dd881 commit 3d02b13

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ npm-*
2323

2424
# Downloaded during "npm install"
2525
/static/microbit
26+
27+
/tmp

src/lib/project-fetcher-hoc.jsx

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
33
import {intlShape, injectIntl} from 'react-intl';
44
import bindAll from 'lodash.bindall';
55
import {connect} from 'react-redux';
6+
import xhr from 'xhr';
67

78
import {setProjectUnchanged} from '../reducers/project-changed';
89
import {
@@ -72,6 +73,41 @@ const ProjectFetcherHOC = function (WrappedComponent) {
7273
}
7374
}
7475
fetchProject (projectId, loadingState) {
76+
if (!this.props.projectToken) {
77+
const errorHandler = err => {
78+
this.props.onError(err);
79+
log.error(err);
80+
};
81+
return new Promise((resolve, reject) => {
82+
const options = {
83+
method: 'GET',
84+
uri: `https://api.smalruby.app/scratch-api-proxy/projects/${projectId}`,
85+
json: true
86+
}
87+
xhr(options, (error, response) => {
88+
if (error || response.statusCode !== 200) {
89+
return reject(new Error(response.status));
90+
}
91+
resolve(response.body.project_token);
92+
});
93+
})
94+
.then(projectToken => {
95+
storage.setProjectToken(projectToken);
96+
storage
97+
.load(storage.AssetType.Project, projectId, storage.DataFormat.JSON)
98+
.then(projectAsset => {
99+
if (projectAsset) {
100+
this.props.onFetchedProjectData(projectAsset.data, loadingState);
101+
} else {
102+
// Treat failure to load as an error
103+
// Throw to be caught by catch later on
104+
throw new Error('Could not find project');
105+
}
106+
})
107+
.catch(errorHandler);
108+
}, errorHandler)
109+
.catch(errorHandler);
110+
}
75111
return storage
76112
.load(storage.AssetType.Project, projectId, storage.DataFormat.JSON)
77113
.then(projectAsset => {

0 commit comments

Comments
 (0)