Skip to content

Commit

Permalink
Add the core/site store.
Browse files Browse the repository at this point in the history
  • Loading branch information
tofumatt committed Feb 21, 2020
1 parent 1ea1971 commit f77a771
Show file tree
Hide file tree
Showing 15 changed files with 568 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ Desktop.ini
# Backstop
tests/backstop/html_report/
tests/backstop/tests/

# Jest coverage
build/
coverage/
6 changes: 5 additions & 1 deletion assets/js/googlesitekit-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ if ( typeof global.googlesitekit === 'undefined' ) {
global.googlesitekit = {};
}

global.googlesitekit.api = API;
if ( typeof global.googlesitekit.api === 'undefined' ) {
global.googlesitekit.api = API;
}

export * from 'assets/js/googlesitekit/api';
9 changes: 7 additions & 2 deletions assets/js/googlesitekit-data.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
/**
* External dependencies
*/
import Data from 'assets/js/googlesitekit/data';
import SiteKitRegistry, * as DataFunctions from 'assets/js/googlesitekit/data';

if ( typeof global.googlesitekit === 'undefined' ) {
global.googlesitekit = {};
}

global.googlesitekit.data = Data;
if ( typeof global.googlesitekit.data === 'undefined' ) {
global.googlesitekit.data = { ...DataFunctions, SiteKitRegistry };
}

export * from 'assets/js/googlesitekit/data';
export default SiteKitRegistry;
2 changes: 1 addition & 1 deletion assets/js/googlesitekit/api/index.private.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const siteKitRequest = async ( type, identifier, datapoint, {

return response;
} catch ( error ) {
// global.console.error( 'Google Site Kit API Error', error );
global.console.error( 'Google Site Kit API Error', error );

throw error;
}
Expand Down
4 changes: 3 additions & 1 deletion assets/js/googlesitekit/api/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import apiFetch from '@wordpress/api-fetch';
* Internal dependencies
*/
// eslint-disable-next-line @wordpress/dependency-group
import { unexpectedSuccess } from 'tests/js/utils';
import { muteConsole, unexpectedSuccess } from 'tests/js/utils';
import * as CacheModule from './cache';
import { setSelectedStorageBackend } from './cache.private';
import { invalidateCache, usingCache, get, set, setUsingCache } from './index';
Expand Down Expand Up @@ -133,6 +133,7 @@ describe( 'googlesitekit.api', () => {
.mockResponseOnce( JSON.stringify( errorResponse ), { status: 404 } );

try {
muteConsole( 'error' );
await get( 'core', 'search-console', 'other' );
} catch ( err ) {
expect( err ).toEqual( errorResponse );
Expand All @@ -153,6 +154,7 @@ describe( 'googlesitekit.api', () => {
.mockResponseOnce( JSON.stringify( errorResponse ), { status: 500 } );

try {
muteConsole( 'error' );
await get( 'core', 'search-console', 'users' );
} catch ( err ) {
expect( err ).toEqual( errorResponse );
Expand Down
8 changes: 6 additions & 2 deletions assets/js/googlesitekit/data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
*/
import { createRegistry } from '@wordpress/data';

const DataStoreRegistry = createRegistry();
const siteKitRegistry = createRegistry();

export default DataStoreRegistry;
export * from '@wordpress/data';

export const { registerStore } = siteKitRegistry;

export default siteKitRegistry;
220 changes: 220 additions & 0 deletions assets/js/googlesitekit/site/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
/**
* External dependencies
*/
import invariant from 'invariant';

/**
* Internal dependencies
*/
import { get, set } from 'googlesitekit-api';
import {
FETCH_CONNECTION_INFO,
FETCH_RESET,
INITIALIZE,
RESET,
RESET_FAILURE,
RESET_SUCCESS,
RECEIVE_CONNECTION_INFO,
RECEIVE_CONNECTION_INFO_FAILED,
} from './index.private';

export const INITIAL_STATE = {
connectionInfo: null,
isFetchingConnectionInfo: false,
isResetting: false,
};

export const STORE_NAME = 'core/site';

export const actions = {
fetchConnectionInfo() {
return {
payload: {},
type: FETCH_CONNECTION_INFO,
};
},

fetchReset() {
return {
payload: {},
type: FETCH_RESET,
};
},

initialize() {
return {
payload: {},
type: INITIALIZE,
};
},

receiveConnectionInfo( connectionInfo ) {
invariant( connectionInfo, 'connectionInfo is required.' );

return {
payload: { connectionInfo },
type: RECEIVE_CONNECTION_INFO,
};
},

receiveConnectionInfoFailed( error ) {
invariant( error, 'error is required.' );

return {
payload: { error },
type: RECEIVE_CONNECTION_INFO_FAILED,
};
},

*reset() {
try {
yield actions.fetchReset();
yield actions.resetSuccess();
yield actions.initialize();

return {
payload: {},
type: RESET,
};
} catch ( err ) {
return actions.resetFailure( err );
}
},

resetFailure( error ) {
invariant( error, 'error is required.' );
return {
payload: { error },
type: RESET_FAILURE,
};
},

resetSuccess() {
return {
payload: {},
type: RESET_SUCCESS,
};
},
};

export const reducer = ( state = INITIAL_STATE, action = {} ) => {
switch ( action.type ) {
case FETCH_CONNECTION_INFO: {
return {
...state,
isFetchingConnectionInfo: true,
};
}

case FETCH_RESET: {
return {
...state,
isResetting: true,
};
}

case INITIALIZE: {
return { ...INITIAL_STATE };
}

case RECEIVE_CONNECTION_INFO: {
const { connectionInfo } = action.payload;

return {
...state,
isFetchingConnectionInfo: false,
connectionInfo,
};
}

case RECEIVE_CONNECTION_INFO_FAILED: {
const { error } = action.payload;

return {
...state,
isFetchingConnectionInfo: false,
connectionInfo: { error, hasError: true },
};
}

case RESET_FAILURE: {
const { error } = action.payload;
return {
...state,
resetError: error,
isResetting: false,
};
}

case RESET_SUCCESS: {
return {
...state,
isResetting: false,
};
}

default: {
return { ...state };
}
}
};

export const selectors = {
getConnection: ( state ) => {
const { connectionInfo } = state;

return connectionInfo;
},

isFetchingConnectionInfo: ( state ) => {
const { isFetchingConnectionInfo } = state;

return isFetchingConnectionInfo;
},

isResetting: ( state ) => {
const { isResetting } = state;

return isResetting;
},
};

export const controls = {
[ FETCH_RESET ]: () => {
return set( 'core', 'site', 'reset' );
},
[ FETCH_CONNECTION_INFO ]: async () => {
return get( 'core', 'site', 'connection' );
},
};

export const resolvers = {
*getConnection() {
try {
const connectionInfo = yield actions.fetchConnectionInfo();
return actions.receiveConnectionInfo( connectionInfo );
} catch ( err ) {
// TODO: Implement an error handler store...
return actions.receiveConnectionInfoFailed( err );
}
},

// *reset() {
// try {
// const resetResult = yield actions.fetchReset();
// yield actions.resetSuccess();
// return actions.initialize();
// } catch ( err ) {
// // TODO: Implement an error handler store...
// return actions.resetFailure( err );
// }
// },
};

export default {
actions,
controls,
reducer,
resolvers,
selectors,
};
8 changes: 8 additions & 0 deletions assets/js/googlesitekit/site/index.private.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const FETCH_CONNECTION_INFO = 'FETCH_CONNECTION_INFO';
export const FETCH_RESET = 'FETCH_RESET';
export const INITIALIZE = 'INITIALIZE';
export const RESET = 'RESET';
export const RESET_FAILURE = 'RESET_FAILURE';
export const RESET_SUCCESS = 'RESET_SUCCESS';
export const RECEIVE_CONNECTION_INFO = 'RECEIVE_CONNECTION_INFO';
export const RECEIVE_CONNECTION_INFO_FAILED = 'RECEIVE_CONNECTION_INFO_FAILED';
Loading

0 comments on commit f77a771

Please sign in to comment.