Skip to content

Commit

Permalink
feat(index): add child indices to UiState correctly
Browse files Browse the repository at this point in the history
Before this change, UiState was merging the indexIds directly with the other state, meaning it's very hard to do changes like IFW-933, which filter out the current UiState, as well as making routing later more easy to write (IFW-941 specifically)

Merging this will prompt an update to #4070
  • Loading branch information
Haroenv committed Aug 26, 2019
1 parent f69ea26 commit 5dc2dcb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 56 deletions.
3 changes: 3 additions & 0 deletions src/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ export type UiState = {
sortBy?: string;
page?: number;
hitsPerPage?: number;
indices?: {
[indexId: string]: UiState;
};
};

/**
Expand Down
124 changes: 69 additions & 55 deletions src/widgets/index/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,21 +1007,23 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
.setQueryParameter('page', 4);

expect(level0.getWidgetState({})).toEqual({
level0IndexName: {
query: 'Apple',
page: 1,
},
level1IndexName: {
query: 'Apple iPhone',
page: 2,
},
level2IndexName: {
query: 'Apple iPhone XS',
page: 3,
},
level3IndexName: {
query: 'Apple iPhone XS Red',
page: 4,
indices: {
level0IndexName: {
query: 'Apple',
page: 1,
},
level1IndexName: {
query: 'Apple iPhone',
page: 2,
},
level2IndexName: {
query: 'Apple iPhone XS',
page: 3,
},
level3IndexName: {
query: 'Apple iPhone XS Red',
page: 4,
},
},
});

Expand All @@ -1032,17 +1034,19 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
.search();

expect(level0.getWidgetState({})).toEqual({
level0IndexName: {
query: 'Hey',
},
level1IndexName: {
query: 'Apple iPhone',
},
level2IndexName: {
query: 'Apple iPhone XS',
},
level3IndexName: {
query: 'Apple iPhone XS Red',
indices: {
level0IndexName: {
query: 'Hey',
},
level1IndexName: {
query: 'Apple iPhone',
},
level2IndexName: {
query: 'Apple iPhone XS',
},
level3IndexName: {
query: 'Apple iPhone XS Red',
},
},
});
});
Expand Down Expand Up @@ -1328,9 +1332,11 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
.setQueryParameter('page', 5);

expect(instance.getWidgetState({})).toEqual({
indexName: {
query: 'Apple',
page: 5,
indices: {
indexName: {
query: 'Apple',
page: 5,
},
},
});
});
Expand Down Expand Up @@ -1362,7 +1368,9 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
);

expect(instance.getWidgetState({})).toEqual({
indexName: {},
indices: {
indexName: {},
},
});
});

Expand Down Expand Up @@ -1427,43 +1435,49 @@ See documentation: https://www.algolia.com/doc/api-reference/widgets/index/js/"
.setQueryParameter('page', 9);

expect(level0.getWidgetState({})).toEqual({
level0IndexName: {
query: 'Apple',
page: 5,
},
level1IndexName: {
query: 'Apple iPhone',
page: 7,
},
level2IndexName: {
query: 'Apple iPhone 5S',
page: 9,
indices: {
level0IndexName: {
query: 'Apple',
page: 5,
},
level1IndexName: {
query: 'Apple iPhone',
page: 7,
},
level2IndexName: {
query: 'Apple iPhone 5S',
page: 9,
},
level3IndexName: {},
},
level3IndexName: {},
});

expect(level1.getWidgetState({})).toEqual({
level1IndexName: {
query: 'Apple iPhone',
page: 7,
},
level2IndexName: {
query: 'Apple iPhone 5S',
page: 9,
indices: {
level1IndexName: {
query: 'Apple iPhone',
page: 7,
},
level2IndexName: {
query: 'Apple iPhone 5S',
page: 9,
},
level3IndexName: {},
},
level3IndexName: {},
});

expect(level2.getWidgetState({})).toEqual({
level2IndexName: {
query: 'Apple iPhone 5S',
page: 9,
indices: {
level2IndexName: {
query: 'Apple iPhone 5S',
page: 9,
},
level3IndexName: {},
},
level3IndexName: {},
});

expect(level3.getWidgetState({})).toEqual({
level3IndexName: {},
indices: { level3IndexName: {} },
});
});
});
Expand Down
5 changes: 4 additions & 1 deletion src/widgets/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,10 @@ const index = (props: IndexProps): Index => {
innerIndex.getWidgetState(previousUiState),
{
...uiState,
[this.getIndexId()]: localUiState,
indices: {
...uiState.indices,
[this.getIndexId()]: localUiState,
},
}
);
},
Expand Down

0 comments on commit 5dc2dcb

Please sign in to comment.