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

Small Refactor #6

Merged
merged 48 commits into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
fef22d8
Introduce simple Post Type Select
widoz Jun 17, 2023
bed1402
Liniting by @wordpress/scripts
widoz Jun 17, 2023
9dceaa8
Fix QA
widoz Jun 17, 2023
915b50e
Mock Select in Unit Test
widoz Jun 17, 2023
852b036
Improve code style and add small test
widoz Jun 23, 2023
48b74c2
Add todo
widoz Jun 23, 2023
a977909
Generalize the data for convertPostTypesToControlOptions
widoz Jun 24, 2023
511ad95
Introduce `useEntityRecords` Decorator
widoz Jun 25, 2023
807e6ce
Introduce Immutable JS PT2
widoz Jul 8, 2023
51269b6
Improve `useEntityRecords`
widoz Jul 8, 2023
38e444f
Introduce Reusable Package
widoz Jul 29, 2023
edf3712
Introduce Base Modules
widoz Jul 29, 2023
2aba55c
Introduce phpcs and qa fixes
widoz Jul 29, 2023
06374b1
Merge branch 'main' into feature/post-types-select
widoz Jul 29, 2023
121c10a
Add workflow
widoz Jul 29, 2023
dae8810
Add workflow for branch
widoz Jul 29, 2023
9e8549d
Incorrect github workflows dir name
widoz Jul 29, 2023
999e390
Improve workflows
widoz Jul 29, 2023
2d32589
Align js to php workflow
widoz Jul 29, 2023
be1143a
Clean up workflow
widoz Jul 29, 2023
34a005c
Update Typescript
widoz Aug 6, 2023
806845e
Fix build issues with types
widoz Aug 6, 2023
57c82c7
Various fixes and improvements
widoz Aug 6, 2023
736eedf
Fix wrong importing in test
widoz Aug 6, 2023
9b488f3
Introduce `useEntityRecords` unit tests
widoz Aug 13, 2023
c5b37e6
Add unit tests and make state works for post-type-select
widoz Aug 13, 2023
98ee82b
Rename singularly hte PostTypeSelect Component
widoz Aug 13, 2023
b665ff2
`ResolveStatus` must be private of `useEntityRecords`
widoz Aug 15, 2023
5554863
Add documentation to `useQueryViewablePostTypes`
widoz Aug 15, 2023
d2ba8fe
Fix `post-types-select` unit tests
widoz Aug 15, 2023
26ffb00
Update configuration
widoz Aug 20, 2023
1d42e0b
Making it working with Wp 6.4
widoz Aug 20, 2023
ba8b683
Lint fixes
widoz Aug 20, 2023
60f7e1c
Add Eslint Array Type Generic rule
widoz Sep 3, 2023
c6a9283
Dev env rely on required WordPress version
widoz Sep 24, 2023
8e50b24
Various Fixes
widoz Oct 12, 2023
f57e830
Hide internal implementation for `PostTypeSelect`
widoz Oct 12, 2023
279a71c
Introduce .vscode
widoz Oct 21, 2023
cdcc974
Improve infrastructure configuration
widoz Oct 21, 2023
361652a
backup
widoz Oct 21, 2023
d66dc40
Reuse WordPress code no dep
widoz Oct 23, 2023
b23410f
Introduce Controller to the Example block
widoz Oct 23, 2023
7934805
Remove wordpress installer composer plugin
widoz Oct 24, 2023
78f55b0
Add Post Select tests and make it work
widoz Oct 24, 2023
2eaa054
Fix Test
widoz Oct 27, 2023
73ff54c
Add Posts Post Types Controller Tests
widoz Oct 28, 2023
453d47a
Merge branch 'main' into feature/post-types-select
widoz Oct 28, 2023
1ba6dad
Small Refactor
widoz Oct 29, 2023
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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text eol=lf
19 changes: 13 additions & 6 deletions @types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ declare namespace EntitiesSearch {
: PostType<'edit'>[K];
}>;

type Record<V extends any> = Readonly<{
type ControlOption<V extends any> = Readonly<{
value: V;
label: string;
}>;

type ComponentStateAware<V> = {
value: V;
setValue(value: ComponentStateAware['value']): void;
};

type EntitiesRecords<Entity> = Readonly<{
records(): Set<Entity>;
isResolving(): boolean;
Expand All @@ -35,19 +40,21 @@ declare namespace EntitiesSearch {
* Components
*/
interface PostTypeSelect<V> {
readonly value: Record<V> | null;
readonly options: Set<NonNullable<PostTypeSelect<V>['value']>>;
readonly value: V | null;
readonly options: Set<ControlOption<V>>;
readonly onChange: (value: PostTypeSelect<V>['value']) => void;
}

interface PostsSelect<V> {
readonly value: Set<Record<V>> | null;
readonly options: NonNullable<PostsSelect<V>['value']>;
readonly value: Set<V> | null;
readonly options: Set<ControlOption<V>>;
readonly onChange: (values: PostsSelect<V>['value']) => void;
}

interface PostsController<P, T> {
readonly postsComponent: React.ComponentType<ComponentStateAware<P>>;
readonly postsComponent: React.ComponentType<
ComponentStateAware<Set<P>>
>;
readonly typesComponent: React.ComponentType<ComponentStateAware<T>>;
}
}
31 changes: 28 additions & 3 deletions sources/js/src/components/post-type-select.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,41 @@
import EntitiesSearch from '@types';
import { Set } from 'immutable';
import React, { JSX } from 'react';
import Select from 'react-select';
import Select, { SingleValue } from 'react-select';

import { matchOptionValues } from '../utils/match-option-values';
import { onChangeControlOptionsHandle } from '../utils/on-change-control-options-handle';

export function PostTypeSelect<V>(
props: EntitiesSearch.PostTypeSelect<V>
): JSX.Element | null {
const matchedValues = matchOptionValues(Set([props.value]), props.options);

const onChange = (values: Set<V> | null) =>
props.onChange(values?.first() ?? null);

return (
<Select
isMulti={false}
value={props.value}
value={matchedValues?.first() ?? null}
options={props.options.toArray()}
onChange={props.onChange}
onChange={(options) => {
if (isNonNullableValue(options)) {
onChange(null);
return;
}

onChangeControlOptionsHandle(
onChange,
Set(options ? [options] : [])
);
}}
/>
);
}

function isNonNullableValue<V>(
value: SingleValue<EntitiesSearch.ControlOption<V>>
): value is NonNullable<EntitiesSearch.ControlOption<V>> {
return value !== null && value.value !== null;
}
10 changes: 5 additions & 5 deletions sources/js/src/components/posts-post-types-controller.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import React, { JSX } from 'react';

import { useState } from '@wordpress/element';

export function PostsPostTypesController<P, T>(
export const PostsPostTypesController = <P, T>(
props: EntitiesSearch.PostsController<P, T>
): JSX.Element {
): JSX.Element => {
const [state, setState] = useState({
posts: Set<P>(),
types: Set<T>(),
Expand All @@ -21,8 +21,8 @@ export function PostsPostTypesController<P, T>(

const Types = () => (
<props.typesComponent
value={state.types}
setValue={(types) => setState({ ...state, types })}
value={state.types.first()}
setValue={(type) => setState({ ...state, types: Set([type]) })}
/>
);

Expand All @@ -32,4 +32,4 @@ export function PostsPostTypesController<P, T>(
<Posts />
</>
);
}
};
13 changes: 8 additions & 5 deletions sources/js/src/components/posts-select.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@ import { Set } from 'immutable';
import React, { JSX } from 'react';
import Select from 'react-select';

import { isControlOption } from '../utils/is-control-option';
import { matchOptionValues } from '../utils/match-option-values';
import { onChangeControlOptionsHandle } from '../utils/on-change-control-options-handle';

export function PostsSelect<V>(
props: EntitiesSearch.PostsSelect<V>
): JSX.Element | null {
const values = matchOptionValues(props.value, props.options);

return (
<Select
isMulti={true}
value={props.value?.toArray()}
value={values?.toArray() ?? []}
options={props.options.toArray()}
onChange={(options) => {
props.onChange(Set(options.filter(isControlOption)));
}}
onChange={(options) =>
onChangeControlOptionsHandle(props.onChange, Set(options))
}
/>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Set } from 'immutable';

export function convertPostTypesToControlOptions(
postTypes: Set<EntitiesSearch.PostType>
): Set<EntitiesSearch.Record<EntitiesSearch.PostType['slug']>> {
): Set<EntitiesSearch.ControlOption<EntitiesSearch.PostType['slug']>> {
return postTypes.map((postType) => ({
label: postType.name,
value: postType.slug,
Expand Down
6 changes: 3 additions & 3 deletions sources/js/src/utils/is-control-option.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import EntitiesSearch from '@types';

export function isControlOption(
export const isControlOption = (
value: unknown
): value is EntitiesSearch.Record<any> {
): value is EntitiesSearch.ControlOption<any> => {
if (!value) {
return false;
}
Expand All @@ -11,4 +11,4 @@ export function isControlOption(
}

return value.hasOwnProperty('label') && value.hasOwnProperty('value');
}
};
10 changes: 10 additions & 0 deletions sources/js/src/utils/match-option-values.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import EntitiesSearch from '@types';
import { Set } from 'immutable';

export const matchOptionValues = <V>(
value: Set<V> | null,
options: Set<EntitiesSearch.ControlOption<V>>
): Set<EntitiesSearch.ControlOption<V>> | null => {
if (!value) return null;
return options.filter((option) => value?.has(option.value));
};
19 changes: 19 additions & 0 deletions sources/js/src/utils/on-change-control-options-handle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import EntitiesSearch from '@types';
import { Set } from 'immutable';

import { isControlOption } from './is-control-option';

/**
* @internal
* @param handler
* @param options
*/
export const onChangeControlOptionsHandle = <V>(
handler: (values: Set<V> | null) => void,
options: Set<EntitiesSearch.ControlOption<V>> | null
): void => {
if (options === null) return;
const controlOptions = Set(options).filter(isControlOption);
const values = controlOptions.map((option) => option.value);
handler(values.size > 0 ? values : null);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Posts Post Types Controller render the given components 1`] = `
<DocumentFragment>
<div>
Post Type Component
</div>
<div>
Posts Component
</div>
</DocumentFragment>
`;
13 changes: 4 additions & 9 deletions tests/js/unit/components/post-types-select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ jest.mock('react-select', () => (props: ReactSelect) => (
<select
id="post-type-select"
data-testid="post-type-select"
onChange={() =>
props.onChange({
label: faker.random.word(),
value: faker.word.noun(),
})
}
onChange={() => props.onChange(faker.word.noun())}
className="react-select"
>
{props.options.map((option: any) => (
Expand All @@ -41,18 +36,18 @@ describe('Post Types Select', () => {
*/
it('call the given onChange handler', (done) => {
let expectedCalled: boolean = false;
const option: EntitiesSearch.Record<string> = {
const option: EntitiesSearch.ControlOption<string> = {
label: faker.random.word(),
value: faker.word.noun(),
};
const options = Set<EntitiesSearch.Record<string>>([])
const options = Set<EntitiesSearch.ControlOption<string>>([])
.add(option)
.merge(buildOptions());

render(
<PostTypeSelect
options={options}
value={option}
value={option.value}
onChange={() => (expectedCalled = true)}
/>
);
Expand Down
120 changes: 120 additions & 0 deletions tests/js/unit/components/posts-post-types-controller.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import EntitiesSearch from '@types';
import { Set } from 'immutable';
import React from 'react';

import { describe, expect, it, jest } from '@jest/globals';

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import { PostsPostTypesController } from '../../../../sources/js/src/components/posts-post-types-controller';

type State = {
posts: Set<string>;
types: Set<string>;
} | null;

let STATE: State = null;
const dispatch: React.Dispatch<State> = (state: State) => (STATE = state);

jest.mock('@wordpress/element', () => ({
useState: jest.fn((initialState: State): [State, React.Dispatch<State>] => {
return [STATE ?? initialState, dispatch];
}),
}));

describe('Posts Post Types Controller', () => {
it('render the given components', () => {
const PostsComponent = () => <div>Posts Component</div>;
const PostTypeComponent = () => <div>Post Type Component</div>;

const { asFragment } = render(
<PostsPostTypesController
postsComponent={PostsComponent}
typesComponent={PostTypeComponent}
/>
);

expect(asFragment()).toMatchSnapshot();
});

it('update the state when the posts component is updated', (done) => {
const PostTypeComponent = () => null;
const PostsSelect = (props: EntitiesSearch.PostsSelect<string>) => (
<button
data-testid="posts-component"
onClick={() => props.onChange(Set(['post-one', 'post-two']))}
>
Update Component State
</button>
);

const PostsComponent = (
props: EntitiesSearch.ComponentStateAware<Set<string>>
) => {
return (
<PostsSelect
value={props.value}
onChange={props.setValue}
options={Set([])}
/>
);
};

render(
<PostsPostTypesController
postsComponent={PostsComponent}
typesComponent={PostTypeComponent}
/>
);

const postComponentElement = screen.getByTestId('posts-component');
userEvent.click(postComponentElement).then(() => {
// @ts-ignore
expect(STATE.posts.toArray()).toEqual(['post-one', 'post-two']);
done();
});
});

it('update the state when the types component is updated', (done) => {
const PostsComponent = () => null;
const PostTypeSelect = (
props: EntitiesSearch.PostTypeSelect<string>
) => (
<button
data-testid="post-type-component"
onClick={() => props.onChange('post-type')}
>
Update Component State
</button>
);

const PostTypeComponent = (
props: EntitiesSearch.ComponentStateAware<string>
) => {
return (
<PostTypeSelect
value={props.value}
onChange={props.setValue}
options={Set([])}
/>
);
};

render(
<PostsPostTypesController
postsComponent={PostsComponent}
typesComponent={PostTypeComponent}
/>
);

const postTypeComponentElement = screen.getByTestId(
'post-type-component'
);
userEvent.click(postTypeComponentElement).then(() => {
// @ts-ignore
expect(STATE.types.toArray()).toEqual(['post-type']);
done();
});
});
});
Loading