STEP-5 Creating Extension, Base Redux Store

In this step finally, we going to begin scandipwa extension development

STEP-5

  1. Create scandipwa extension using scandipwa-cli, for that navigate to your theme folder, and execute scandipwa extension create scandipwa-socialshare

  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

"dependencies": {
   "@scandipwa/scandipwa": "4.4.0",
   "scandipwa-socialshare": "file:../app/code/ScandiPWA/SocialShareGraphQl/scandipwa-socialshare"
},
"scandipwa": {
   "type": "theme",
   "locales": {
       "en_US": true
   },
   "parentTheme": "@scandipwa/scandipwa",
   "extensions": {
       "scandipwa-socialshare": true
   }
},

Your file should look like so.

"dependencies": {
   "@scandipwa/scandipwa": "4.4.0",
   "scandipwa-socialshare": "file:packages/scandipwa-socialshare"
},
"scandipwa": {
   "type": "theme",
   "locales": {
       "en_US": true
   },
   "parentTheme": "@scandipwa/scandipwa",
   "extensions": {
       "scandipwa-socialshare": true
   }
},

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
import { Field } from 'Util/Query';

/** @namespace ScandipwaSocialshare/Query/SocialShare/Query/SocialShareQuery */
export class SocialShareQuery {
   getQuery() {
       return new Field('socialShare')
           .addFieldList(['enabled']);
   }
}

export default new SocialShareQuery();

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
export const UPDATE_SOCIAL_SHARE = 'UPDATE_SOCIAL_SHARE';

/** @namespace Store/SocialShare/Action/updateSocialShare */
export const updateSocialShare = (socialShare) => ({
   type: UPDATE_SOCIAL_SHARE,
   socialShare
});

6. In <SOURCE>/store/SocialShare create SocialShare.reducer.js

scandipwa-socialshare/src/store/SocialShare/SocialShare.reducer.js
import { UPDATE_SOCIAL_SHARE } from './SocialShare.action';

/** @namespace ScandipwaSocialshare/Store/SocialShare/Reducer/getInitialState */
export const getInitialState = () => ({
   socialShare: {
       enabled: 'not'
   }
});

/** @namespace ScandipwaSocialshare/Store/SocialShare/Reducer/SocialShareReducer */
export const SocialShareReducer = (
   state = getInitialState(),
   action
) => {
   const {
       type,
       socialShare
   } = action;

   switch (type) {
   case UPDATE_SOCIAL_SHARE:
       return {
           ...state,
           ...socialShare
       };

   default:
       return state;
   }
};

export default SocialShareReducer;

7. And finally in <SOURCE>/store/SocialShare create SocialShare.dispatcher.js

scandipwa-socialshare/src/store/SocialShare/SocialShare.dispatcher.js
import { showNotification } from 'Store/Notification/Notification.action';
import BrowserDatabase from 'Util/BrowserDatabase';
import { QueryDispatcher } from 'Util/Request';
import { ONE_MONTH_IN_SECONDS } from 'Util/Request/QueryDispatcher';

import SocialShareQuery from '../../query/SocialShare.query';
import { updateSocialShare } from './SocialShare.action';

/** @namespace ScandipwaSocialshare/Store/SocialShare/Dispatcher/SocialShareDispatcher */
export class SocialShareDispatcher extends QueryDispatcher {
   __construct() {
       super.__construct('SocialShare');
   }

   onSuccess(data, dispatch) {
       if (data) {
           BrowserDatabase.setItem(data, 'SocialShare', ONE_MONTH_IN_SECONDS);
           dispatch(updateSocialShare(data));
       }
   }

   onError(error, dispatch) {
       dispatch(showNotification('error', __('Error fetching SocialShare!'), error));
   }

   prepareRequest() {
       return [
           SocialShareQuery.getQuery()
       ];
   }
}

export default new SocialShareDispatcher();

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)).

Last updated