The following code is a ngrx 4 reducer could you make it compatible for ngrx 16 so it…
- User
The following code is a ngrx 4 reducer could you make it compatible for ngrx 16 so it works with createReducer()
export function reducer(state = initialState, action: any): State { switch (action.type) { case exportStream.ActionTypes.REQUEST_TABLE_TREE: return Object.assign({}, state, { isLoading: true, editingTable: null, }); case exportStream.ActionTypes.REQUEST_TABLE_TREE_FAIL: return Object.assign({}, state, { isLoading: false, messages: [{severity: 'error', summary: 'Failed to fetch', detail: 'Failed to perform action'}] }); case exportStream.ActionTypes.REQUEST_TABLE_TREE_SUCCESS: let selectedExportStream = state.selectedExportStream; let selectedItems = []; if (selectedExportStream && action.payload.selectedItemsPerExportStream) {
selectedItems = action.payload.selectedItemsPerExportStream.get(selectedExportStream.id); } return Object.assign({}, state, { isLoading: false, tableTree: action.payload.tableTree, selectedItems: selectedItems, selectedItemsPerExportStream: action.payload.selectedItemsPerExportStream, tableTreeChanges: emptytableTreeChanges, }); case exportStream.ActionTypes.SELECT_EXPORT_STREAM: return Object.assign({}, state, { selectedExportStream: action.payload, selectedItems: state.selectedItemsPerExportStream.has(action.payload.id) ? state.selectedItemsPerExportStream.get(action.payload.id) : [], tableTreeChanges: emptytableTreeChanges }); case exportStream.ActionTypes.CREATE_EXPORT_STREAM: return { ...state, isSaving: true, }; case exportStream.ActionTypes.CREATE_EXPORT_STREAM_SUCCESS: let newExportStream: ExportStream = {id: action.payload.tag}; return { ...state, isSaving: false, exportStreams: [newExportStream, ...state.exportStreams], tableTreeChanges: emptytableTreeChanges, selectedItems: [], defaultRepetitionRates: [...state.defaultRepetitionRates, ...action.payload.repetitionRates], selectedExportStream: newExportStream, messages: [{ severity: 'success', summary: 'Successfully created ' + action.payload.tag + ' export stream', detail: 'Successfully created ' + action.payload.tag + ' export stream', }] }; case exportStream.ActionTypes.CREATE_EXPORT_STREAM_FAIL: return { ...state, isSaving: false, messages: [{severity: 'error', summary: 'Failed to create exportStream', detail: 'Failed to perform action'}] }; case exportStream.ActionTypes.CREATE_PRIVATE_TABLE: return { ...state, isSaving: true, }; case exportStream.ActionTypes.CREATE_PRIVATE_TABLE_SUCCESS: const newTreeAddPrivateTable: TableTree = {...state.tableTree}; newTreeAddPrivateTable.privateTables.push(action.payload as PrivateTable); return { ...state, isSaving: false, tableTreeChanges: emptytableTreeChanges, tableTree: newTreeAddPrivateTable, messages: [{ severity: 'success', summary: 'Successfully created ' + action.payload.name + ' private table', detail: 'Successfully created ' + action.payload.name + ' private table', }] }; case exportStream.ActionTypes.CREATE_PRIVATE_TABLE_FAIL: return { ...state, isSaving: false, messages: [{severity: 'error', summary: 'Failed to create private table', detail: 'Failed to perform action'}] }; case exportStream.ActionTypes.UPDATE_PRIVATE_TABLE: return { ...state, isSaving: true, }; case exportStream.ActionTypes.UPDATE_PRIVATE_TABLE_SUCCESS: const newTree: TableTree = { ...state.tableTree, privateTables: state.tableTree.privateTables.map(privateTable => { if(action.payload.id == privateTable.id) { return action.payload as PrivateTable; } return privateTable as PrivateTable; }), }; return { ...state, isSaving: false, tableTreeChanges: emptytableTreeChanges, tableTree: newTree, messages: [{ severity: 'success', summary: 'Successfully updated ' + action.payload.name + ' private table', detail: 'Successfully updated ' + action.payload.name + ' private table', }] }; case exportStream.ActionTypes.UPDATE_PRIVATE_TABLE_FAIL: return { ...state, isSaving: false, messages: [{severity: 'error', summary: 'Failed to create private table', detail: 'Failed to perform action'}] }; case exportStream.ActionTypes.CREATE_PRIVATE_TABLE_FAIL: return { ...state, isSaving: false, messages: [{severity: 'error', summary: 'Failed to create private table', detail: 'Failed to perform action'}] }; case exportStream.ActionTypes.DELETE_PRIVATE_TABLE: return { ...state, isSaving: true, }; case exportStream.ActionTypes.DELETE_PRIVATE_TABLE_SUCCESS: const newTreeDeletePrivateTable: TableTree = { ...state.tableTree, privateTables: state.tableTree.privateTables.filter(privateTable => { if(action.payload.id == privateTable.id) { return false; } return true; }), }; return { ...state, isSaving: false, tableTreeChanges: emptytableTreeChanges, tableTree: newTreeDeletePrivateTable, messages: [{ severity: 'success', summary: 'Successfully deleted ' + action.payload.name + ' private table', detail: 'Successfully deleted ' + action.payload.name + ' private table', }] }; case exportStream.ActionTypes.DELETE_PRIVATE_TABLE_FAIL: return { ...state, isSaving: false, messages: [{severity: 'error', summary: 'Failed to create private table', detail: 'Failed to perform action'}] }; case exportStream.ActionTypes.NEW_UPDATE_ITEMS_EXPORTSTREAM: return Object.assign({}, state, { tableTreeChanges: action.payload }); case exportStream.ActionTypes.CREATE_REPETITION_RATE: { return { ...state, isSaving: true }; } case exportStream.ActionTypes.CREATE_REPETITION_RATE_FAIL: { return { ...state, isSaving: false, messages: [{ severity: 'error', summary: 'Failed to update repetetion rate for tableId 0x' + (action.payload.tableId.toString().length < 2 ? "0" : "") + Number(action.payload.tableId).toString(16), detail: 'Failed to perform action' }] }; } case exportStream.ActionTypes.CREATE_REPETITION_RATE_SUCCESS: { const tempTableTreeCreateRepettionRate: TableTree = { ...state.tableTree, privateTables: state.tableTree.privateTables.map(privateTable => { if(action.payload.tableId == privateTable.tableId) { privateTable.repetitionRateInMilliseconds = action.payload.repetitionRateInMilliseconds; return privateTable as PrivateTable; } return privateTable as PrivateTable; }), }; return { ...state, isSaving: false, tableTree: tempTableTreeCreateRepettionRate, messages: [{ severity: 'success', summary: 'Updated repetetion rate for tableId 0x' + (action.payload.tableId.toString().length < 2 ? "0" : "") + Number(action.payload.tableId).toString(16), detail: 'Updated repetetion rate for tableId 0x' + (action.payload.tableId.toString().length < 2 ? "0" : "") + Number(action.payload.tableId).toString(16) + ' successfully', }] }; } default: return state;} }
- Assistant
Here's the updated code for the ngrx 16 reducer using createReducer():
import { createReducer, on } from '@ngrx/store'; import { ActionTypes } from './export-stream.actions';
export const initialState = { isLoading: false, editingTable: null, messages: [], selectedExportStream: null, selectedItems: [], selectedItemsPerExportStream: null, tableTreeChanges: null, exportStreams: [], defaultRepetitionRates: [], tableTree: null, };
export const reducer = createReducer( initialState,
on(ActionTypes.REQUEST_TABLE_TREE, state => ({ ...state, isLoading: true, editingTable: null, })),
on(ActionTypes.REQUEST_TABLE_TREE_FAIL, state => ({ ...state, isLoading: false, messages: [{severity: 'error', summary: 'Failed to fetch', detail: 'Failed to perform action'}] })),
on(ActionTypes.REQUEST_TABLE_TREE_SUCCESS, (state, action) => { const selectedExportStream = state.selectedExportStream; let selectedItems = [];
if (selectedExportStream && action.payload.selectedItemsPerExportStream) { selectedItems = action.payload.selectedItemsPerExportStream.get(selectedExportStream.id); } return { ...state, isLoading: false, tableTree: action.payload.tableTree, selectedItems: selectedItems, selectedItemsPerExportStream: action.payload.selectedItemsPerExportStream, tableTreeChanges: emptytableTreeChanges, };}),
on(ActionTypes.SELECT_EXPORT_STREAM, (state, action) => ({ ...state, selectedExportStream: action.payload, selectedItems: state.selectedItemsPerExportStream.has(action.payload.id) ? state.selectedItemsPerExportStream.get(action.payload.id) : [], tableTreeChanges: emptytableTreeChanges, })),
on(ActionTypes.CREATE_EXPORT_STREAM, state => ({ ...state, isSaving: true, })),
on(ActionTypes.CREATE_EXPORT_STREAM_SUCCESS, (state, action) => { const newExportStream = { id: action.payload.tag };
return { ...state, isSaving: false, exportStreams: [newExportStream, ...state.exportStreams], tableTreeChanges: emptytableTreeChanges, selectedItems: [], defaultRepetitionRates: [...state.defaultRepetitionRates, ...action.payload.repetitionRates], selectedExportStream: newExportStream, messages: [ { severity: 'success', summary: 'Successfully created ' + action.payload.tag + ' export stream', detail: 'Successfully created ' + action.payload.tag + ' export stream', }, ], };}),
on(ActionTypes.CREATE_EXPORT_STREAM_FAIL, state => ({ ...state, isSaving: false, messages: [{ severity: 'error', summary: 'Failed to create exportStream', detail: 'Failed to perform action' }], })),
on(ActionTypes.CREATE_PRIVATE_TABLE, state => ({ ...state, isSaving: true, })),
on(ActionTypes.CREATE_PRIVATE_TABLE_SUCCESS, (state, action) => { const newTreeAddPrivateTable = { ...state.tableTree }; newTreeAddPrivateTable.privateTables.push(action.payload as PrivateTable);
return { ...state, isSaving: false, tableTreeChanges: emptytableTreeChanges, tableTree: newTreeAddPrivateTable, messages: [ { severity: 'success', summary: 'Successfully created ' + action.payload.name + ' private table', detail: 'Successfully created ' + action.payload.name + ' private table', }, ], };}),
on(ActionTypes.CREATE_PRIVATE_TABLE_FAIL, state => ({ ...state, isSaving: false, messages: [{ severity: 'error', summary: 'Failed to create private table', detail: 'Failed to perform action' }], })),
on(ActionTypes.UPDATE_PRIVATE_TABLE, state => ({ ...state, isSaving: true, })),
on(ActionTypes.UPDATE_PRIVATE_TABLE_SUCCESS, (state, action) => { const newTree = { ...state.tableTree, privateTables: state.tableTree.privateTables.map(privateTable => { if (action.payload.id == privateTable.id) { return action.payload as PrivateTable; } return privateTable as PrivateTable; }), };
return { ...state, isSaving: false, tableTreeChanges: emptytableTreeChanges, tableTree: newTree, messages: [ { severity: 'success', summary: 'Successfully updated ' + action.payload.name + ' private table', detail: 'Successfully updated ' + action.payload.name + ' private table', }, ], };}),
on(ActionTypes.UPDATE_PRIVATE_TABLE_FAIL, state => ({ ...state, isSaving: false, messages: [{ severity: 'error', summary: 'Failed to create private table', detail: 'Failed to perform action' }], })),
on(ActionTypes.DELETE_PRIVATE_TABLE, state => ({ ...state, isSaving: true, })),
on(ActionTypes.DELETE_PRIVATE_TABLE_SUCCESS, (state, action) => { const newTreeDeletePrivateTable = { ...state.tableTree, privateTables: state.tableTree.privateTables.filter(privateTable => { if (action.payload.id == privateTable.id) { return false; } return true; }), };
return { ...state, isSaving: false, tableTreeChanges: emptytableTreeChanges, tableTree: newTreeDeletePrivateTable, messages: [ { severity: 'success', summary: 'Successfully deleted ' + action.payload.name + ' private table', detail: 'Successfully deleted ' + action.payload.name + ' private table', }, ], };}),
on(ActionTypes.DELETE_PRIVATE_TABLE_FAIL, state => ({ ...state, isSaving: false, messages: [{ severity: 'error', summary: 'Failed to create private table', detail: 'Failed to perform action' }], })),
on(ActionTypes.NEW_UPDATE_ITEMS_EXPORTSTREAM, (state, action) => ({ ...state, tableTreeChanges: action.payload, })),
on(ActionTypes.CREATE_REPETITION_RATE, state => ({ ...state, isSaving: true, })),
on(ActionTypes.CREATE_REPETITION_RATE_FAIL, (state, action) => ({ ...state, isSaving: false, messages: [ { severity: 'error', summary: 'Failed to update repetetion rate for tableId 0x' + (action.payload.tableId.toString().length < 2 ? '0' : '') + Number(action.payload.tableId).toString(16), detail: 'Failed to perform action', }, ], })),
on(ActionTypes.CREATE_REPETITION_RATE_SUCCESS, (state, action) => { const tempTableTreeCreateRepettionRate = { ...state.tableTree, privateTables: state.tableTree.privateTables.map(privateTable => { if (action.payload.tableId == privateTable.tableId) { privateTable.repetitionRateInMilliseconds = action.payload.repetitionRateInMilliseconds; return privateTable as PrivateTable; } return privateTable as PrivateTable; }), };
return { ...state, isSaving: false, tableTree: tempTableTreeCreateRepettionRate, messages: [ { severity: 'success', summary: 'Updated repetetion rate for tableId 0x' + (action.payload.tableId.toString().length < 2 ? '0' : '') + Number(action.payload.tableId).toString(16), detail: 'Updated repetetion rate for tableId 0x' + (action.payload.tableId.toString().length < 2 ? '0' : '') + Number(action.payload.tableId).toString(16) + ' successfully', }, ], };}) );