Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dashboard: Incorporate the rest of the filters into FiltersProvider #11851

Merged
merged 36 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
382d52f
Added StoryStatusToggle component, added status filter to filters pro…
mwritter Jun 28, 2022
67626a9
Clean up
mwritter Jun 29, 2022
e4c4517
Clean up
mwritter Jun 29, 2022
df776dc
Code clean up, added status and search to filters provider
mwritter Jun 30, 2022
22ad677
Added sortObject to filters provider
mwritter Jun 30, 2022
01df549
Removed file
mwritter Jun 30, 2022
44a0f11
File rename
mwritter Jun 30, 2022
69da443
Added unit test for updateSort
mwritter Jun 30, 2022
460c88f
Updated test
mwritter Jul 1, 2022
d7e2346
removed import
mwritter Jul 1, 2022
b6ea60f
oops
mwritter Jul 1, 2022
2e58711
oops
mwritter Jul 1, 2022
017afa6
removed imports
mwritter Jul 1, 2022
d80124c
Refactor
mwritter Jul 1, 2022
0450dfc
Fixed imports
mwritter Jul 1, 2022
dc9ec5f
Fixed test
mwritter Jul 1, 2022
c9aa70e
Merge branch 'main' into feature/11789_Incorporate_All_Filters
spacedmonkey Jul 4, 2022
8ebdc10
Fixed tests, laid ground work for using a template filters provider, …
mwritter Jul 6, 2022
8612a9f
Merge branch 'feature/11789_Incorporate_All_Filters' of github.com:Go…
mwritter Jul 6, 2022
6cbf436
Fixing tests
mwritter Jul 7, 2022
dd64e89
Fixing tests
mwritter Jul 7, 2022
5840bb6
Merge branch 'main' into feature/11789_Incorporate_All_Filters
mwritter Jul 7, 2022
8b4b3d3
Fixing tests
mwritter Jul 7, 2022
6eef6dc
Fixing tests
mwritter Jul 7, 2022
be352cd
Fixing tests
mwritter Jul 7, 2022
7df6b52
Removed change to e2e test
mwritter Jul 8, 2022
da23874
Updated tests, add more test coverage, code clean up
mwritter Jul 8, 2022
9eba870
Updated registering status to use default filter
mwritter Jul 8, 2022
e3995c6
oof
mwritter Jul 8, 2022
73dde14
Fixed dashboard text not diplaying correctly
mwritter Jul 11, 2022
7b8c37e
Removed import
mwritter Jul 11, 2022
a11303e
Merge branch 'main' into feature/11789_Incorporate_All_Filters
mwritter Jul 11, 2022
e4811d0
Merge branch 'main' into feature/11789_Incorporate_All_Filters
swissspidy Jul 11, 2022
f44e412
Merge branch 'feature/11789_Incorporate_All_Filters' of github.com:Go…
mwritter Jul 12, 2022
acedfe1
Fixed tests
mwritter Jul 12, 2022
03cc6ce
Fixed tests
mwritter Jul 14, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import {
import {
ViewPropTypes,
PagePropTypes,
SearchPropTypes,
} from '../../../../utils/useTemplateView';
import { TemplatesPropType, TemplateActionsPropType } from '../../../../types';
import { EmptyContentMessage } from '../../shared';
Expand Down Expand Up @@ -90,18 +89,18 @@ function Content({
) : (
<EmptyContentMessage>
<Headline size={THEME_CONSTANTS.TYPOGRAPHY.PRESET_SIZES.SMALL} as="h3">
{search?.keyword
{search
? sprintf(
/* translators: %s: search term. */
__(
'Sorry, we couldn\'t find any results matching "%s"',
'web-stories'
),
search.keyword
search
)
: __('No templates currently available.', 'web-stories')}
</Headline>
{!search?.keyword && (
{!search && (
<Button
type={BUTTON_TYPES.PRIMARY}
size={BUTTON_SIZES.MEDIUM}
Expand All @@ -118,8 +117,8 @@ function Content({
isLoading,
newStoryURL,
page.requestNextPage,
search.keyword,
templateActions,
search,
templates,
totalTemplates,
view.pageSize,
Expand All @@ -138,7 +137,7 @@ Content.propTypes = {
page: PagePropTypes,
templates: TemplatesPropType,
totalTemplates: PropTypes.number,
search: SearchPropTypes,
search: PropTypes.string,
templateActions: TemplateActionsPropType,
view: ViewPropTypes,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ import { screen } from '@testing-library/react';
/**
* Internal dependencies
*/
import { TEMPLATES_GALLERY_STATUS, VIEW_STYLE } from '../../../../../constants';
import {
TEMPLATES_GALLERY_SORT_OPTIONS,
TEMPLATES_GALLERY_STATUS,
VIEW_STYLE,
} from '../../../../../constants';
import { renderWithProviders } from '../../../../../testUtils';
import LayoutProvider from '../../../../../components/layout/provider';
import Content from '..';
import { ConfigProvider } from '../../../../config';
import useTemplateFilters from '../../filters/useTemplateFilters';

const fakeTemplates = [
{
Expand Down Expand Up @@ -61,14 +66,49 @@ const fakeTemplates = [
},
];

jest.mock('../../filters/useTemplateFilters', () => ({
...jest.requireActual('../../filters/useTemplateFilters'),
__esModule: true,
default: jest.fn(),
}));

const mockUseTemplateFilters = useTemplateFilters;

const updateSort = jest.fn();
const updateFilter = jest.fn();

const mockFilterState = {
filters: [
{
key: 'search',
filterId: null,
},
{
key: 'status',
filterId: TEMPLATES_GALLERY_STATUS.ALL,
},
],
filtersObject: {
status: TEMPLATES_GALLERY_STATUS.ALL,
},
sortObject: {
orderby: TEMPLATES_GALLERY_SORT_OPTIONS.POPULAR,
},
registerFilters: () => {},
updateFilter,
updateSort,
};

describe('Explore Templates <Content />', function () {
beforeEach(() => {
mockUseTemplateFilters.mockImplementation(() => mockFilterState);
});

it('should render the content grid with the correct template count.', function () {
renderWithProviders(
<ConfigProvider config={{ cdnURL: 'cdn.example.com' }}>
<LayoutProvider>
<Content
filter={{ view: TEMPLATES_GALLERY_STATUS.ALL }}
search={{ keyword: '' }}
templates={fakeTemplates}
totalTemplates={3}
page={{
Expand All @@ -92,8 +132,6 @@ describe('Explore Templates <Content />', function () {
renderWithProviders(
<LayoutProvider>
<Content
filter={{ view: TEMPLATES_GALLERY_STATUS.ALL }}
search={{ keyword: '' }}
templates={[]}
totalTemplates={0}
page={{
Expand All @@ -115,8 +153,7 @@ describe('Explore Templates <Content />', function () {
renderWithProviders(
<LayoutProvider>
<Content
filter={{ view: TEMPLATES_GALLERY_STATUS.ALL }}
search={{ keyword: 'scooby dooby doo' }}
search={'scooby dooby doo'}
templates={[]}
totalTemplates={0}
page={{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* External dependencies
*/
import {
createContext,
useReducer,
useCallback,
useMemo,
} from '@googleforcreators/react';
import PropTypes from 'prop-types';

/**
* Internal dependencies
*/
import { DEFAULT_TEMPLATE_FILTERS } from '../../../../../constants/templates';
import reducer from '../../../filters/reducer';
import * as types from '../../../filters/types';

export const filterContext = createContext({
state: {},
actions: {},
});

const { filters: defaultTemplateFilters, sort: defaultTemplateSort } =
DEFAULT_TEMPLATE_FILTERS;

export default function TemplateFiltersProvider({ children }) {
const [state, dispatch] = useReducer(reducer, {
filters: [],
filtersObject: defaultTemplateFilters,
sortObject: defaultTemplateSort,
});

/**
* Dispatch UPDATE_FILTER with new data for a given filter
*
* @param {string} key key property on one of the filter objects
* @param {Object} value the properties with updated values
* @return {void}
*/
const updateFilter = useCallback((key, value) => {
dispatch({ type: types.UPDATE_FILTER, payload: { key, value } });
}, []);

/**
* Dispatch UPDATE_SORT with new data to update sort
*
* @param {string} key key property on one of the sort objects
* @param {Object} value the properties with updated values
* @return {void}
*/
const updateSort = useCallback((values) => {
dispatch({
type: types.UPDATE_SORT,
payload: { type: 'template', values },
});
}, []);

/**
* Dispatch REGISTER_FILTERS with all the filters data
*
* @param {Array} payload array of filters data
* @return {void}
*/
const registerFilters = useCallback((value) => {
dispatch({ type: types.REGISTER_FILTERS, payload: { value } });
}, []);

const contextValue = useMemo(() => {
return { state, actions: { updateSort, updateFilter, registerFilters } };
}, [state, updateSort, updateFilter, registerFilters]);

return (
<filterContext.Provider value={contextValue}>
{children}
</filterContext.Provider>
);
}

TemplateFiltersProvider.propTypes = {
children: PropTypes.node,
};
Loading