STEP-5 Creating Extension, Base Redux Store
In this step finally, we going to begin scandipwa extension development
​STEP-5​
  1. 1.
    Create scandipwa extension using scandipwa-cli, for that navigate to your theme folder, and execute scandipwa extension create scandipwa-socialshare
  2. 2.
    In your scandipwa theme packages folder you should locate scandipwa-socialshare extension folder NOTE. In my case ONLY for development purposes extension is located right in Magento module so my scandipwa/package.json file looks like
1
"dependencies": {
2
"@scandipwa/scandipwa": "4.4.0",
3
"scandipwa-socialshare": "file:../app/code/ScandiPWA/SocialShareGraphQl/scandipwa-socialshare"
4
},
5
"scandipwa": {
6
"type": "theme",
7
"locales": {
8
"en_US": true
9
},
10
"parentTheme": "@scandipwa/scandipwa",
11
"extensions": {
12
"scandipwa-socialshare": true
13
}
14
},
Copied!
Your file should look like so.
1
"dependencies": {
2
"@scandipwa/scandipwa": "4.4.0",
3
"scandipwa-socialshare": "file:packages/scandipwa-socialshare"
4
},
5
"scandipwa": {
6
"type": "theme",
7
"locales": {
8
"en_US": true
9
},
10
"parentTheme": "@scandipwa/scandipwa",
11
"extensions": {
12
"scandipwa-socialshare": true
13
}
14
},
Copied!
3. First, let’s create a query that going to query graph that we created in STEP-4 create query folder in scandipwa-socialshare/src <- (in future <SOURCE>) and SocialShare.query.js in it
scandipwa-socialshare/src/query/SocialShare.query.js
1
import { Field } from 'Util/Query';
2
​
3
/** @namespace ScandipwaSocialshare/Query/SocialShare/Query/SocialShareQuery */
4
export class SocialShareQuery {
5
getQuery() {
6
return new Field('socialShare')
7
.addFieldList(['enabled']);
8
}
9
}
10
​
11
export default new SocialShareQuery();
Copied!
4. Now we going to create the SocialShare redux store, create store folder in <SOURCE>/ and SocialShare folder in <SOURCE>/store/
5. In <SOURCE>/store/SocialShare create SocialShare.action.js
scandipwa-socialshare/src/store/SocialShare/SocialShare.action.js
1
export const UPDATE_SOCIAL_SHARE = 'UPDATE_SOCIAL_SHARE';
2
​
3
/** @namespace Store/SocialShare/Action/updateSocialShare */
4
export const updateSocialShare = (socialShare) => ({
5
type: UPDATE_SOCIAL_SHARE,
6
socialShare
7
});
Copied!
6. In <SOURCE>/store/SocialShare create SocialShare.reducer.js
scandipwa-socialshare/src/store/SocialShare/SocialShare.reducer.js
1
import { UPDATE_SOCIAL_SHARE } from './SocialShare.action';
2
​
3
/** @namespace ScandipwaSocialshare/Store/SocialShare/Reducer/getInitialState */
4
export const getInitialState = () => ({
5
socialShare: {
6
enabled: 'not'
7
}
8
});
9
​
10
/** @namespace ScandipwaSocialshare/Store/SocialShare/Reducer/SocialShareReducer */
11
export const SocialShareReducer = (
12
state = getInitialState(),
13
action
14
) => {
15
const {
16
type,
17
socialShare
18
} = action;
19
​
20
switch (type) {
21
case UPDATE_SOCIAL_SHARE:
22
return {
23
...state,
24
...socialShare
25
};
26
​
27
default:
28
return state;
29
}
30
};
31
​
32
export default SocialShareReducer;
Copied!
7. And finally in <SOURCE>/store/SocialShare create SocialShare.dispatcher.js
scandipwa-socialshare/src/store/SocialShare/SocialShare.dispatcher.js
1
import { showNotification } from 'Store/Notification/Notification.action';
2
import BrowserDatabase from 'Util/BrowserDatabase';
3
import { QueryDispatcher } from 'Util/Request';
4
import { ONE_MONTH_IN_SECONDS } from 'Util/Request/QueryDispatcher';
5
​
6
import SocialShareQuery from '../../query/SocialShare.query';
7
import { updateSocialShare } from './SocialShare.action';
8
​
9
/** @namespace ScandipwaSocialshare/Store/SocialShare/Dispatcher/SocialShareDispatcher */
10
export class SocialShareDispatcher extends QueryDispatcher {
11
__construct() {
12
super.__construct('SocialShare');
13
}
14
​
15
onSuccess(data, dispatch) {
16
if (data) {
17
BrowserDatabase.setItem(data, 'SocialShare', ONE_MONTH_IN_SECONDS);
18
dispatch(updateSocialShare(data));
19
}
20
}
21
​
22
onError(error, dispatch) {
23
dispatch(showNotification('error', __('Error fetching SocialShare!'), error));
24
}
25
​
26
prepareRequest() {
27
return [
28
SocialShareQuery.getQuery()
29
];
30
}
31
}
32
​
33
export default new SocialShareDispatcher();
Copied!
Unfortunately on this step, we can't see any visual outcome, the only thing that you can do, is to restart your frontend and ensure that it is still functional, believe in the next step we will have much more fun)).
Copy link