Skip to content

Commit

Permalink
Created plugin actions and reducers (WIP). See #59
Browse files Browse the repository at this point in the history
  • Loading branch information
MKHenson committed Oct 28, 2016
1 parent a466555 commit e759eaa
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 4 deletions.
34 changes: 34 additions & 0 deletions lib/actions/plugin-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { IStorePlugins } from 'hatchery-editor';
import { get } from '../core/utils';
import { DB } from '../setup/db';

/**
* Describes the different types of plugin action types
*/
export type PluginActionType =
'PLUGINS_REQUEST_PENDING' |
'PLUGINS_DOWNLOADED' |
'PLUGINS_REQUEST_REJECTED';

/**
* An interface for describing plugin actions
*/
export interface IPluginAction extends Redux.Action {
type: PluginActionType;
data?: IStorePlugins;
};

/**
* Attempts to download the available plugins for use in the editor
*/
export function downloadPluginList() {
return ( dispatch: Redux.Dispatch<IPluginAction> ) => {
dispatch<IPluginAction>( { type: 'PLUGINS_REQUEST_PENDING' });

get<Modepress.IGetArrayResponse<HatcheryServer.IPlugin>>( `${DB.API}/plugins` ).then(( response: ModepressAddons.IGetProjects ) => {
dispatch<IPluginAction>( { type: 'PLUGINS_DOWNLOADED', data: { plugins: response.data } });
}).catch(( err: Error ) => {
dispatch<IPluginAction>( { type: 'PLUGINS_REQUEST_REJECTED', data: { error: err } });
});
}
}
4 changes: 2 additions & 2 deletions lib/components/new-project/new-project.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { VForm } from '../v-form/v-form';
import { VTextarea } from '../v-textarea/v-textarea';
import { Attention } from '../attention/attention';
import { ButtonPrimary } from '../buttons/buttons';
import { PluginsWidget, IPluginPlus } from '../plugins-widget/plugins-widget';
import { PluginsWidget } from '../plugins-widget/plugins-widget';
import { ValidationType, AttentionType } from '../../setup/enums';
import { capitalize } from '../../core/utils';
import { ISplashScreen } from 'hatchery-editor';
Expand All @@ -15,7 +15,7 @@ export interface INewProjectProps {
}

export interface INewProjectState {
plugins?: IPluginPlus[];
plugins?: HatcheryServer.IPlugin[];
message?: string | null;
error?: boolean;
}
Expand Down
2 changes: 0 additions & 2 deletions lib/containers/splash/splash.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ export interface ISplashState {
project?: HatcheryServer.IProject | null;
}



/**
* The splash screen when starting the app
*/
Expand Down
9 changes: 9 additions & 0 deletions lib/definitions/custom/hatchery-editor.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ declare namespace Animate {
screen?: 'welcome' | 'new-project' | 'opening-project';
}

/**
* Describes the plugins available to the editor
*/
export interface IStorePlugins extends IBaseStoreState {
plugins?: HatcheryServer.IPlugin[];
map?: { [ name: string ]: HatcheryServer.IPlugin[] };
}

/**
* The root interface for the application store
*/
Expand All @@ -189,6 +197,7 @@ declare namespace Animate {
project: IProject;
user: IUser;
logs: ILogMessage[];
storePlugins: IStorePlugins;
}
}

Expand Down
88 changes: 88 additions & 0 deletions lib/reducers/plugins-reducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { IPluginAction } from '../actions/plugin-actions';
import { IStorePlugins } from 'hatchery-editor';

// The defaults of the plugin store
const defaults: IStorePlugins = {
plugins: [],
error: null,
loading: false,
map: {},
serverResponse: null
}

function createPluginMap( plugins: HatcheryServer.IPlugin[] ) {
const toRet = {};

for ( let i = 0, l = plugins.length; i < l; i++ ) {
if ( !toRet[ plugins[ i ].name! ] )
toRet[ plugins[ i ].name! ] = [];
else
continue;

let pluginArray = toRet[ plugins[ i ].name! ];

for ( let ii = 0; ii < l; ii++ )
if ( plugins[ ii ].name === plugins[ i ].name )
pluginArray.push( plugins[ ii ] );

// Sort the plugins based on their versions
pluginArray = pluginArray.sort( function compare( a, b ) {
if ( a === b )
return 0;

const a_components = a.version!.split( '.' );
const b_components = b.version!.split( '.' );

const len = Math.min( a_components.length, b_components.length );

// loop while the components are equal
for ( let i = 0; i < len; i++ ) {
// A bigger than B
if ( parseInt( a_components[ i ] ) > parseInt( b_components[ i ] ) )
return 1;

// B bigger than A
if ( parseInt( a_components[ i ] ) < parseInt( b_components[ i ] ) )
return -1;
}

// If one's a prefix of the other, the longer one is greater.
if ( a_components.length > b_components.length )
return 1;

if ( a_components.length < b_components.length )
return -1;

// Otherwise they are the same.
return 0;
});

pluginArray.reverse();
}

return toRet;
}

/**
* A reducer that processes state changes of the plugins
*/
export function editorReducer( state: IStorePlugins = defaults, action: IPluginAction ): IStorePlugins {
let toRet = state;

switch ( action.type ) {
case 'PLUGINS_REQUEST_PENDING':
toRet = Object.assign<IStorePlugins>( {}, state, { loading: true, error: null });
break;
case 'PLUGINS_REQUEST_REJECTED':
toRet = Object.assign<IStorePlugins>( {}, state, action.data! );
break;
case 'PLUGINS_DOWNLOADED':
toRet = Object.assign<IStorePlugins>( {}, state, action.data! );
toRet.map = createPluginMap( toRet.plugins! );
break;
default:
break;
}

return toRet;
}

0 comments on commit e759eaa

Please sign in to comment.