Skip to content

Commit

Permalink
remove case when catalog is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
eschutho committed Oct 14, 2022
1 parent da02b71 commit 9cf9ea1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1703,22 +1703,6 @@ describe('dbReducer', () => {
},
});
});
test('it will set state to payload from parametersChange when catalog is undefined', () => {
const action: DBReducerActionType = {
type: ActionType.parametersChange,
payload: { name: 'foo', type: 'catalog-foo', value: 'bar' },
};
const currentState = dbReducer(databaseFixture, action);

expect(currentState).toEqual({
...databaseFixture,
parameters: {
catalog: {
'': 'bar',
},
},
});
});

test('it will add a new catalog array when empty', () => {
const action: DBReducerActionType = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,41 +276,33 @@ export function dbReducer(
[action.payload.name]: action.payload.value,
};
case ActionType.parametersChange:
if (action.payload.type?.startsWith('catalog')) {
if (trimmedState.catalog !== undefined) {
// Formatting wrapping google sheets table catalog
const catalogCopy: CatalogObject[] = [...trimmedState.catalog];
const idx = action.payload.type?.split('-')[1];
const catalogToUpdate: CatalogObject = catalogCopy[idx] || {};
catalogToUpdate[action.payload.name] = action.payload.value;

// insert updated catalog to existing state
catalogCopy.splice(parseInt(idx, 10), 1, catalogToUpdate);

// format catalog for state
// eslint-disable-next-line array-callback-return
parametersCatalog = catalogCopy.reduce((obj, item: any) => {
const catalog = { ...obj };
catalog[item.name] = item.value;
return catalog;
}, {});

return {
...trimmedState,
catalog: catalogCopy,
parameters: {
...trimmedState.parameters,
catalog: parametersCatalog,
},
};
}
if (action.payload.name === 'name') {
parametersCatalog = { [action.payload.value as string]: undefined };
} else {
parametersCatalog = { '': action.payload.value };
}
// catalog params will always have a catalog state for
// dbs that use a catalog, i.e., gsheets, even if the
// fields are empty strings
if (
action.payload.type?.startsWith('catalog') &&
trimmedState.catalog !== undefined
) {
// Formatting wrapping google sheets table catalog
const catalogCopy: CatalogObject[] = [...trimmedState.catalog];
const idx = action.payload.type?.split('-')[1];
const catalogToUpdate: CatalogObject = catalogCopy[idx] || {};
catalogToUpdate[action.payload.name] = action.payload.value;

// insert updated catalog to existing state
catalogCopy.splice(parseInt(idx, 10), 1, catalogToUpdate);

// format catalog for state
// eslint-disable-next-line array-callback-return
parametersCatalog = catalogCopy.reduce((obj, item: any) => {
const catalog = { ...obj };
catalog[item.name] = item.value;
return catalog;
}, {});

return {
...trimmedState,
catalog: catalogCopy,
parameters: {
...trimmedState.parameters,
catalog: parametersCatalog,
Expand Down

0 comments on commit 9cf9ea1

Please sign in to comment.