Skip to content

Commit 4236724

Browse files
committed
UI passes down username now...
1 parent 479d0de commit 4236724

File tree

3 files changed

+22
-19
lines changed

3 files changed

+22
-19
lines changed

src/lib/storage.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Storage extends ScratchStorage {
6666

6767
window.parent.postMessage({type: 'block-compiler-action', action: 'JWT_AUTH_REQUEST'}, trustedOrigin);
6868

69-
const jwt = await new Promise(resolve => {
69+
const creds = await new Promise(resolve => {
7070
// eslint-disable-next-line require-jsdoc, func-style
7171
function handleMessage (event) {
7272
if (event.origin !== trustedOrigin) {
@@ -76,16 +76,19 @@ class Storage extends ScratchStorage {
7676

7777
if (event.data?.type === 'JWT_AUTH CREDS' && event.data?.token) {
7878
window.removeEventListener('message', handleMessage);
79-
resolve(event.data.token);
79+
resolve({
80+
token: event.data.token,
81+
username: event.data.username || ''
82+
});
8083
}
8184
}
8285

8386
window.addEventListener('message', handleMessage);
8487
});
8588

86-
console.log('Received JWT:', jwt);
87-
this.projectToken = jwt;
88-
return jwt;
89+
this.projectToken = creds.token;
90+
this.username = creds.username;
91+
return creds;
8992
}
9093
async getProjectToken () {
9194
if (!this.projectToken) {

src/lib/tw-project-meta-fetcher-hoc.jsx

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import log from './log';
88
const {API_HOST} = require('./brand');
99

1010
import {setProjectTitle} from '../reducers/project-title';
11-
import {setAuthor, setDescription, setUsername} from '../reducers/tw';
11+
import {setAuthor, setDescription} from '../reducers/tw';
1212

1313
import storage from './storage';
1414

@@ -88,11 +88,7 @@ const TWProjectMetaFetcherHOC = function (WrappedComponent) {
8888
if (title) {
8989
this.props.onSetProjectTitle(title);
9090
}
91-
const username = data.username;
92-
if (username) {
93-
this.props.onSetUsername(username);
94-
this.setState({canUseCloud: true});
95-
}
91+
9692
const authorName = data.author.username;
9793
const authorThumbnail = data.author.PFP;
9894
this.props.onSetAuthor(authorName, authorThumbnail);
@@ -103,7 +99,8 @@ const TWProjectMetaFetcherHOC = function (WrappedComponent) {
10399
}
104100

105101
this.setState({canSave: data.canSave === 'true'});
106-
// this.setState({canEditTitle: true}); // if you can save, you can edit title (it doesn't work the prop isn't passed down)
102+
// if you can save, you can edit title (it doesn't work the prop isn't passed down)
103+
// this.setState({canEditTitle: true});
107104
this.setState({canRemix: data.canRemix === 'true'});
108105
storage.setCloudOTT(data?.cloudDataOTT);
109106
storage.setCustomAchievements(data?.customAchievements);
@@ -129,7 +126,6 @@ const TWProjectMetaFetcherHOC = function (WrappedComponent) {
129126
onSetAuthor,
130127
onSetDescription,
131128
onSetProjectTitle,
132-
onSetUsername,
133129
/* eslint-enable no-unused-vars */
134130
...props
135131
} = this.props;
@@ -147,8 +143,7 @@ const TWProjectMetaFetcherHOC = function (WrappedComponent) {
147143
reduxProjectId: PropTypes.string,
148144
onSetAuthor: PropTypes.func,
149145
onSetDescription: PropTypes.func,
150-
onSetProjectTitle: PropTypes.func,
151-
onSetUsername: PropTypes.func
146+
onSetProjectTitle: PropTypes.func
152147
};
153148
const mapStateToProps = state => ({
154149
reduxProjectId: state.scratchGui.projectState.projectId
@@ -162,8 +157,7 @@ const TWProjectMetaFetcherHOC = function (WrappedComponent) {
162157
instructions,
163158
credits
164159
})),
165-
onSetProjectTitle: title => dispatch(setProjectTitle(title)),
166-
onSetUsername: username => dispatch(setUsername(username))
160+
onSetProjectTitle: title => dispatch(setProjectTitle(title))
167161
});
168162
return connect(
169163
mapStateToProps,

src/lib/vm-manager-hoc.jsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import VM from 'scratch-vm';
77
import AudioEngine from 'scratch-audio';
88

99
import {setProjectUnchanged} from '../reducers/project-changed';
10+
import {setUsername} from '../reducers/tw';
1011
import {
1112
LoadingStates,
1213
getIsLoadingWithId,
@@ -88,6 +89,9 @@ const vmManagerHOC = function (WrappedComponent) {
8889
canRecieveAchievement: !this.props.hasEverEnteredEditor,
8990
canSave: this.props.canSave
9091
};
92+
const username = storage.username || '';
93+
this.props.onSetUsername(username);
94+
9195
this.props.vm.loadProject(this.props.projectData, additionalData)
9296
.then(() => {
9397
this.props.onLoadedProject(this.props.loadingState, this.props.canSave);
@@ -158,7 +162,8 @@ const vmManagerHOC = function (WrappedComponent) {
158162
projectId: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
159163
username: PropTypes.string,
160164
vm: PropTypes.instanceOf(VM).isRequired,
161-
hasEverEnteredEditor: PropTypes.bool
165+
hasEverEnteredEditor: PropTypes.bool,
166+
onSetUsername: PropTypes.func
162167
};
163168

164169
const mapStateToProps = state => {
@@ -181,7 +186,8 @@ const vmManagerHOC = function (WrappedComponent) {
181186
onError: error => dispatch(projectError(error)),
182187
onLoadedProject: (loadingState, canSave) =>
183188
dispatch(onLoadedProject(loadingState, canSave, true)),
184-
onSetProjectUnchanged: () => dispatch(setProjectUnchanged())
189+
onSetProjectUnchanged: () => dispatch(setProjectUnchanged()),
190+
onSetUsername: username => dispatch(setUsername(username))
185191
});
186192

187193
// Allow incoming props to override redux-provided props. Used to mock in tests.

0 commit comments

Comments
 (0)