committed by
József Farkas
parent
ee1d8ec59e
commit
4e13f910dd
@@ -1,12 +1,84 @@
|
||||
import { Action } from '@ngrx/store';
|
||||
|
||||
import { Macro } from '../../config-serializer/config-items/Macro';
|
||||
import { MacroAction } from '../../config-serializer/config-items/macro-action';
|
||||
|
||||
export namespace MacroActions {
|
||||
export const PREFIX = '[Macro] ';
|
||||
export const GET_ALL = MacroActions.PREFIX + 'Get all macros';
|
||||
|
||||
export function getAll(): Action {
|
||||
export const DUPLICATE = MacroActions.PREFIX + 'Duplicate macro';
|
||||
export const EDIT_NAME = MacroActions.PREFIX + 'Edit macro title';
|
||||
export const REMOVE = MacroActions.PREFIX + 'Remove macro';
|
||||
|
||||
export const ADD_ACTION = MacroActions.PREFIX + 'Add macro action';
|
||||
export const SAVE_ACTION = MacroActions.PREFIX + 'Save macro action';
|
||||
export const DELETE_ACTION = MacroActions.PREFIX + 'Delete macro action';
|
||||
export const REORDER_ACTION = MacroActions.PREFIX + 'Reorder macro action';
|
||||
|
||||
export function removeMacro(id: number): Action {
|
||||
return {
|
||||
type: MacroActions.GET_ALL
|
||||
type: MacroActions.REMOVE,
|
||||
payload: id
|
||||
};
|
||||
}
|
||||
|
||||
export function duplicateMacro(macro: Macro): Action {
|
||||
return {
|
||||
type: MacroActions.DUPLICATE,
|
||||
payload: macro
|
||||
};
|
||||
}
|
||||
|
||||
export function editMacroName(id: number, name: string): Action {
|
||||
return {
|
||||
type: MacroActions.EDIT_NAME,
|
||||
payload: {
|
||||
id: id,
|
||||
name: name
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function addMacroAction(id: number, action: MacroAction): Action {
|
||||
return {
|
||||
type: MacroActions.ADD_ACTION,
|
||||
payload: {
|
||||
id: id,
|
||||
action: action
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function saveMacroAction(id: number, index: number, action: MacroAction): Action {
|
||||
return {
|
||||
type: MacroActions.SAVE_ACTION,
|
||||
payload: {
|
||||
id: id,
|
||||
index: index,
|
||||
action: action
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function deleteMacroAction(id: number, index: number, action: MacroAction): Action {
|
||||
return {
|
||||
type: MacroActions.DELETE_ACTION,
|
||||
payload: {
|
||||
id: id,
|
||||
index: index,
|
||||
action: action
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export function reorderMacroAction(id: number, oldIndex: number, newIndex: number): Action {
|
||||
return {
|
||||
type: MacroActions.REORDER_ACTION,
|
||||
payload: {
|
||||
id: id,
|
||||
oldIndex: oldIndex,
|
||||
newIndex: newIndex
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
21
src/store/effects/macro.ts
Normal file
21
src/store/effects/macro.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { Injectable } from '@angular/core';
|
||||
|
||||
import { Actions, Effect } from '@ngrx/effects';
|
||||
|
||||
import 'rxjs/add/operator/map';
|
||||
|
||||
import { MacroActions } from '../actions';
|
||||
|
||||
@Injectable()
|
||||
export class MacroEffects {
|
||||
|
||||
@Effect()remove$: any = this.actions$
|
||||
.ofType(MacroActions.REMOVE)
|
||||
.map(() => {
|
||||
// TODO: Waiting for the fix: https://github.com/angular/angular/issues/10770
|
||||
// If state is empty router.navigate(['/macro']);
|
||||
// Else router.navigate(['/macro']);
|
||||
});
|
||||
|
||||
constructor(private actions$: Actions) {}
|
||||
}
|
||||
@@ -1,16 +1,146 @@
|
||||
import '@ngrx/core/add/operator/select';
|
||||
import { Action } from '@ngrx/store';
|
||||
|
||||
import 'rxjs/add/operator/map';
|
||||
import { Observable } from 'rxjs/Observable';
|
||||
|
||||
import { Macro } from '../../config-serializer/config-items/Macro';
|
||||
|
||||
import { MacroActions } from '../actions';
|
||||
import { AppState } from '../index';
|
||||
|
||||
const initialState: Macro[] = [];
|
||||
|
||||
export default function(state = initialState, action: Action): Macro[] {
|
||||
let newMacro: Macro;
|
||||
|
||||
switch (action.type) {
|
||||
case MacroActions.GET_ALL:
|
||||
break;
|
||||
case MacroActions.DUPLICATE:
|
||||
|
||||
newMacro = new Macro(action.payload);
|
||||
newMacro.name = generateName(state, newMacro.name);
|
||||
newMacro.id = generateId(state);
|
||||
|
||||
return [...state, newMacro];
|
||||
|
||||
case MacroActions.EDIT_NAME:
|
||||
let name: string = generateName(state, action.payload.name);
|
||||
|
||||
return state.map((macro: Macro) => {
|
||||
if (macro.id === action.payload.id) {
|
||||
macro.name = name;
|
||||
}
|
||||
|
||||
return macro;
|
||||
});
|
||||
|
||||
case MacroActions.REMOVE:
|
||||
return state.filter((macro: Macro) => macro.id !== action.payload);
|
||||
|
||||
case MacroActions.ADD_ACTION:
|
||||
return state.map((macro: Macro) => {
|
||||
if (macro.id === action.payload.id) {
|
||||
newMacro = new Macro(macro);
|
||||
newMacro.macroActions.push(action.payload.action);
|
||||
|
||||
return newMacro;
|
||||
}
|
||||
|
||||
return macro;
|
||||
});
|
||||
|
||||
case MacroActions.SAVE_ACTION:
|
||||
return state.map((macro: Macro) => {
|
||||
if (macro.id === action.payload.id) {
|
||||
newMacro = new Macro(macro);
|
||||
newMacro.macroActions[action.payload.index] = action.payload.action;
|
||||
|
||||
return newMacro;
|
||||
}
|
||||
|
||||
return macro;
|
||||
});
|
||||
|
||||
case MacroActions.DELETE_ACTION:
|
||||
return state.map((macro: Macro) => {
|
||||
if (macro.id === action.payload.id) {
|
||||
newMacro = new Macro(macro);
|
||||
newMacro.macroActions.splice(action.payload.index, 1);
|
||||
|
||||
return newMacro;
|
||||
}
|
||||
|
||||
return macro;
|
||||
});
|
||||
|
||||
case MacroActions.REORDER_ACTION:
|
||||
return state.map((macro: Macro) => {
|
||||
if (macro.id === action.payload.id) {
|
||||
let newIndex: number = action.payload.newIndex;
|
||||
|
||||
// We need to reduce the new index for one when we are moving action down
|
||||
if (newIndex > action.payload.oldIndex) {
|
||||
--newIndex;
|
||||
}
|
||||
|
||||
newMacro = new Macro(macro);
|
||||
newMacro.macroActions.splice(
|
||||
newIndex,
|
||||
0,
|
||||
newMacro.macroActions.splice(action.payload.oldIndex, 1)[0]
|
||||
);
|
||||
|
||||
return newMacro;
|
||||
}
|
||||
|
||||
return macro;
|
||||
});
|
||||
|
||||
default: {
|
||||
return state;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function getMacro(id: number) {
|
||||
if (isNaN(id)) {
|
||||
return (state$: Observable<AppState>) => state$
|
||||
.select(appState => appState.macros)
|
||||
.map((macros: Macro[]) => {
|
||||
if (macros.length > 0) {
|
||||
return macros[0];
|
||||
} else {
|
||||
return undefined;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
return (state$: Observable<AppState>) => state$
|
||||
.select(appState => appState.macros)
|
||||
.map((macros: Macro[]) => macros.find((macro: Macro) => macro.id === id));
|
||||
}
|
||||
}
|
||||
|
||||
function generateName(macros: Macro[], name: string) {
|
||||
let suffix = 2;
|
||||
const oldName: string = name;
|
||||
|
||||
while (macros.some((macro: Macro) => macro.name === name)) {
|
||||
name = oldName + ` (${suffix})`;
|
||||
++suffix;
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
function generateId(macros: Macro[]) {
|
||||
let newId = 0;
|
||||
|
||||
macros.forEach((macro: Macro) => {
|
||||
if (macro.id > newId) {
|
||||
newId = macro.id;
|
||||
}
|
||||
});
|
||||
|
||||
return ++newId;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user