Skip to content

Commit

Permalink
Merge pull request #76 from netceteragroup/feat/add-row
Browse files Browse the repository at this point in the history
feat(grid): add action for adding new row in the grid
  • Loading branch information
hdimitrieski committed Jan 5, 2021
2 parents 038f015 + f58454a commit 03f9122
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 8 deletions.
2 changes: 2 additions & 0 deletions projects/data-grid-demo/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@
<button (click)="initializeWithColumnResizing()">Column Resizing</button>
<br/>
<button (click)="onDeleteRow(rowIndex.value)">Delete Row</button>
<br/>
<button (click)="onAddRow(rowIndex.value)">Add Row</button>
18 changes: 17 additions & 1 deletion projects/data-grid-demo/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { getGridState } from './reducers';
import { BadgesColumnComponent } from './components/badge/badges-column.component';
import { DateFilterComponent } from './components/date-filter.component';
import { ExperienceFilterComponent } from './components/experience-filter.component';
import { resetGridState } from '../../../data-grid/src/lib/actions/data-grid-actions';
import { addRow, resetGridState } from '../../../data-grid/src/lib/actions/data-grid-actions';
import { take } from 'rxjs/operators';

const dateFormat = 'MM-dd-yyyy';
Expand Down Expand Up @@ -311,4 +311,20 @@ export class AppComponent implements OnInit {
}));
}
}

onAddRow(rowIndex: string) {
const index = Number(rowIndex);
if (!Number.isNaN(index)) {
this.store.dispatch(addRow({
name: this.gridName,
index: index,
row: {
userId: `New-Row-Id-${index}`,
skills: [],
experience: [],
fromDate: Date.now()
}
}));
}
}
}
9 changes: 8 additions & 1 deletion projects/data-grid/src/lib/actions/data-grid-actions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createAction, props } from '@ngrx/store';
import {
AddRowPayload,
BaseGridPayload,
ChangePageNumberPayload,
ChangePageSizePayload,
Expand Down Expand Up @@ -34,7 +35,8 @@ export enum GridActionTypes {
ToggleDetailGrid = 'ngrx-data-grid/ToggleDetailGrid',
ReorderColumn = 'ngrx-data-grid/ReorderColumn',
ResizeColumn = 'ngrx-data-grid/ResizeColumn',
DeleteRow = 'ngrx-data-grid/DeleteRow'
DeleteRow = 'ngrx-data-grid/DeleteRow',
AddRow = 'ngrx-data-grid/AddRow'
}

export const initGrid = createAction(
Expand Down Expand Up @@ -120,3 +122,8 @@ export const deleteRow = createAction(
GridActionTypes.DeleteRow,
props<DeleteRowByIndexPayload | DeleteRowWherePayload<any>>()
);

export const addRow = createAction(
GridActionTypes.AddRow,
props<AddRowPayload<any>>()
);
5 changes: 5 additions & 0 deletions projects/data-grid/src/lib/actions/data-grid-payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,8 @@ export interface DeleteRowByIndexPayload extends BaseGridPayload {
export interface DeleteRowWherePayload<T> extends BaseGridPayload {
where: (dataItem: T) => boolean;
}

export interface AddRowPayload<T> extends BaseGridPayload {
row: T;
index?: number;
}
41 changes: 41 additions & 0 deletions projects/data-grid/src/lib/store/data-grid.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as R from 'ramda';
import {
addRow,
changePageNumber,
changePageSize,
deleteRow,
Expand Down Expand Up @@ -564,4 +565,44 @@ describe('Data Grid reducer', () => {
expect(result.length).toEqual(7);
});

it('should add new row at the given position', () => {
// given
const action = addRow({name: 'grid-1', index: 1, row: {id: 50, name: 'new', value: 100}});

// when
const resultState = gridReducer(state, action);

// then
const result = R.path(['grid-1'])(resultState);
expect(result.data.length).toEqual(8);
expect(result.data[1].id).toEqual(50);
});

it('should add new row at the end', () => {
// given
const action = addRow({name: 'grid-1', row: {id: 45}});

// when
const resultState = gridReducer(state, action);

// then
const result = R.path(['grid-1'])(resultState);
expect(result.data.length).toEqual(8);
expect(result.data[7].id).toEqual(45);
expect(result.rowDataIndexes[7]).toEqual(7);
});

it('should add the new row at the end if the new row index is bigger than the length of the data in the grid', () => {
// given
const action = addRow({name: 'grid-1', index: 9, row: {id: 45}});

// when
const resultState = gridReducer(state, action);

// then
const result = R.path(['grid-1'])(resultState);
expect(result.data.length).toEqual(8);
expect(result.data[7].id).toEqual(45);
});

});
32 changes: 26 additions & 6 deletions projects/data-grid/src/lib/store/data-grid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { calculateNumberOfPages, getPagedData } from './pagination-util';
import { applySorting } from './sorting-util';
import { hasNoValue, hasValue, isNotEqual, isTrue } from '../util/type';
import {
AddRowPayload,
DeleteRowByIndexPayload,
DeleteRowWherePayload,
FilterGridPayload,
Expand Down Expand Up @@ -257,17 +258,34 @@ const resolveDataItemIndex = (state: GridState, action: DeleteRowByIndexPayload
return -1;
};

const validIndex = (state: GridState, index: number): boolean =>
hasValue(index) && index > -1 && index < state.rowDataIndexes.length;

const deleteRow = (state: GridState, action: DeleteRowByIndexPayload | DeleteRowWherePayload<any>): GridState => {
const dataItemIndex = resolveDataItemIndex(state, action);
if (dataItemIndex > -1 && dataItemIndex < state.data.length) {
return R.evolve({
rowDataIndexes: R.remove(dataItemIndex, 1),
data: R.remove(state.rowDataIndexes[dataItemIndex], 1)
}, state);
if (validIndex(state, dataItemIndex)) {
return R.evolve({
rowDataIndexes: R.remove(dataItemIndex, 1),
data: R.remove(state.rowDataIndexes[dataItemIndex], 1)
}, state);
}
return state;
};

const addRow = (state: GridState, action: AddRowPayload<any>): GridState => {
if (!hasValue(action.row)) {
return state;
}

return validIndex(state, action.index)
? R.evolve({
data: R.insert(state.rowDataIndexes[action.index], action.row)
}, state)
: R.evolve({
data: R.append(action.row)
}, state);
};

const recalculateRowIndexesAndPagination = (state: GridState): any => {
const newRowDataIndexes = calculateRowDataIndexes(state);
const prevPagination = state.pagination;
Expand Down Expand Up @@ -349,7 +367,8 @@ const reducer = createReducer(
on(GridActions.toggleDetailGrid, toggleDetailGrid),
on(GridActions.reorderColumn, reorderColumn),
on(GridActions.resizeColumn, resizeColumn),
on(GridActions.deleteRow, deleteRow)
on(GridActions.deleteRow, deleteRow),
on(GridActions.addRow, addRow)
);

const rowIndexesAndPaginationReducer = createReducer(initialGridState, on(
Expand All @@ -359,6 +378,7 @@ const rowIndexesAndPaginationReducer = createReducer(initialGridState, on(
GridActions.changePageSize,
GridActions.changePageNumber,
GridActions.deleteRow,
GridActions.addRow,
recalculateRowIndexesAndPagination
));

Expand Down

0 comments on commit 03f9122

Please sign in to comment.