@@ -4,8 +4,10 @@ import React from 'react';
44import VM from 'scratch-vm' ;
55import { defineMessages , injectIntl , intlShape } from 'react-intl' ;
66import log from '../lib/log' ;
7+ import { connect } from 'react-redux' ; // Import connect
78// eslint-disable-next-line import/no-commonjs
8- const { EXTENSION_HOST } = require ( '../lib/brand' ) ;
9+ const { EXTENSION_HOST , TRUSTED_IFRAME_HOST , API_HOST } = require ( '../lib/brand' ) ;
10+ import storage from '../lib/storage' ;
911
1012import extensionLibraryContent , {
1113 galleryError ,
@@ -123,7 +125,7 @@ class ExtensionLibrary extends React.PureComponent {
123125 } ) ;
124126 }
125127 }
126- handleItemSelect ( item ) {
128+ handleItemSelect = async item => {
127129 if ( item . href ) {
128130 return ;
129131 }
@@ -140,13 +142,46 @@ class ExtensionLibrary extends React.PureComponent {
140142 this . props . onCategorySelected ( 'myBlocks' ) ;
141143 return ;
142144 }
145+
146+ const additionalData = { } ;
147+
148+ // SPECIAL CHECK FOR CUSTOM ACHIEVEMENTS EXTENSION
149+ if ( extensionId === 'customAchievements' ) {
150+ // Check if project ID exists and is not the default '0' (unsaved)
151+ if ( ! this . props . projectId || this . props . projectId === '0' ) {
152+ window . parent . postMessage ( {
153+ type : 'block-compiler-action' ,
154+ action : 'extension-error' ,
155+ error : 'login_required'
156+ } , '*' ) ;
157+ return ;
158+ }
159+
160+ // Check if user owns the project (canSave)
161+ if ( ! this . props . canSave ) {
162+ window . parent . postMessage ( {
163+ type : 'block-compiler-action' ,
164+ action : 'extension-error' ,
165+ error : 'not_owner'
166+ } , '*' ) ;
167+ return ;
168+ }
169+ const { accessToken, customAchievements} = await storage . loadCustomAchievementData ( ) ;
170+ additionalData . projectId = this . props . projectId ;
171+ additionalData . customAchievements = customAchievements ;
172+ additionalData . authToken = accessToken ;
173+ additionalData . API_HOST = API_HOST ;
174+ additionalData . TRUSTED_IFRAME_HOST = TRUSTED_IFRAME_HOST ;
175+ additionalData . canRecieveAchievement = false ; // you are in editor without a doubt...
176+ additionalData . canSave = this . props . canSave ;
177+ }
143178
144179 const url = item . extensionURL ? item . extensionURL : extensionId ;
145180 if ( ! item . disabled ) {
146181 if ( this . props . vm . extensionManager . isExtensionLoaded ( extensionId ) ) {
147182 this . props . onCategorySelected ( extensionId ) ;
148183 } else {
149- this . props . vm . extensionManager . loadExtensionURL ( url )
184+ this . props . vm . extensionManager . loadExtensionURL ( url , true , additionalData )
150185 . then ( ( ) => {
151186 this . props . onCategorySelected ( extensionId ) ;
152187 } )
@@ -157,7 +192,8 @@ class ExtensionLibrary extends React.PureComponent {
157192 } ) ;
158193 }
159194 }
160- }
195+ } ;
196+
161197 render ( ) {
162198 let library = null ;
163199 if ( this . state . gallery || this . state . galleryError || this . state . galleryTimedOut ) {
@@ -201,7 +237,15 @@ ExtensionLibrary.propTypes = {
201237 onOpenCustomExtensionModal : PropTypes . func ,
202238 onRequestClose : PropTypes . func ,
203239 visible : PropTypes . bool ,
204- vm : PropTypes . instanceOf ( VM ) . isRequired // eslint-disable-line react/no-unused-prop-types
240+ vm : PropTypes . instanceOf ( VM ) . isRequired , // eslint-disable-line react/no-unused-prop-types
241+ projectId : PropTypes . string ,
242+ canSave : PropTypes . bool
205243} ;
206244
207- export default injectIntl ( ExtensionLibrary ) ;
245+ const mapStateToProps = state => ( {
246+ projectId : state . scratchGui . projectState . projectId
247+ } ) ;
248+
249+ export default injectIntl ( connect (
250+ mapStateToProps
251+ ) ( ExtensionLibrary ) ) ;
0 commit comments