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

Introduce Enriched Control Option #40

Merged
merged 2 commits into from
Feb 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 14 additions & 5 deletions @types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ declare namespace EntitiesSearch {
queryArguments?: EntitiesSearch.QueryArguments<E>
) => Promise<Options<E>>;

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

type SingularControl<V> = {
[K in keyof BaseControl<V>]: K extends 'value'
? V
Expand All @@ -55,6 +50,20 @@ declare namespace EntitiesSearch {
: BaseControl<V>[K];
};

interface Record<T> {
get<F>(key: string, fallback?: F): T | F | undefined;
set(key: string, value: T): Record<T>;
}

interface ControlOption<V extends any> extends Readonly<{
value: V;
label: string;
}> {}

interface EnrichedControlOption<V extends any> extends ControlOption<V>, Readonly<{
readonly extra: Record<string, unknown>;
}> {}

interface BaseControl<V>
extends Readonly<{
value: Set<V>;
Expand Down
2 changes: 1 addition & 1 deletion sources/client/src/api/search-entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { doAction } from '@wordpress/hooks';
*/
import { abortControllers } from '../services/abort-controllers';
import { ContextualAbortController } from '../services/contextual-abort-controller';
import { Set } from '../vo/set';
import { Set } from '../models/set';
import { fetch } from './fetch';

export async function searchEntities< E >(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { useEntitiesOptionsStorage } from '../hooks/use-entities-options-storage
import { useSearch } from '../hooks/use-search';
import { orderSelectedOptionsAtTheTop } from '../utils/order-selected-options-at-the-top';
import { uniqueControlOptions } from '../utils/unique-control-options';
import { Set } from '../vo/set';
import { Set } from '../models/set';

/**
* A composite component that provides a way to search for entities by kind.
Expand Down
2 changes: 1 addition & 1 deletion sources/client/src/components/plural-select-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import React, { JSX } from 'react';
/**
* Internal dependencies
*/
import { Set } from '../vo/set';
import { Set } from '../models/set';
import { NoOptionsMessage } from './no-options-message';

export function PluralSelectControl(
Expand Down
2 changes: 1 addition & 1 deletion sources/client/src/hooks/use-entities-options-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { doAction } from '@wordpress/hooks';
*/
import { makeInitialState } from '../storage/entities/initial-state';
import { reducer } from '../storage/entities/reducer';
import { Set } from '../vo/set';
import { Set } from '../models/set';

type _Reducer< E, K > = Reducer<
EntitiesSearch.EntitiesState< E, K >,
Expand Down
2 changes: 1 addition & 1 deletion sources/client/src/hooks/use-entity-records.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useEntityRecords as useCoreEntityRecords } from '@wordpress/core-data';
/**
* Internal dependencies
*/
import { Set } from '../vo/set';
import { Set } from '../models/set';

enum ResolveStatus {
ERROR = 'ERROR',
Expand Down
2 changes: 1 addition & 1 deletion sources/client/src/hooks/use-query-viewable-post-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import EntitiesSearch from '@types';
/**
* Internal dependencies
*/
import { Set } from '../vo/set';
import { Set } from '../models/set';
import { useEntityRecords } from './use-entity-records';

/**
Expand Down
2 changes: 1 addition & 1 deletion sources/client/src/hooks/use-query-viewable-taxonomies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import EntitiesSearch from '@types';
/**
* Internal dependencies
*/
import { Set } from '../vo/set';
import { Set } from '../models/set';
import { useEntityRecords } from './use-entity-records';

/**
Expand Down
2 changes: 1 addition & 1 deletion sources/client/src/hooks/use-search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { doAction } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import { Set } from '../vo/set';
import { Set } from '../models/set';

type SearchPhrase = Parameters<
EntitiesSearch.SearchControl[ 'onChange' ]
Expand Down
8 changes: 5 additions & 3 deletions sources/client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ export * from './hooks/use-entity-records';
export * from './hooks/use-query-viewable-post-types';
export * from './hooks/use-query-viewable-taxonomies';

export * from './models/set';
export * from './models/immutable-record';

export * from './utils/convert-entities-to-control-options';
export * from './utils/is-control-option';
export * from './utils/order-selected-options-at-the-top';
export * from './utils/unique-control-options';
export * from './utils/order-selected-options-at-the-top';

export * from './vo/control-option';
export * from './vo/set';
export * from './value-objects/control-option';
20 changes: 20 additions & 0 deletions sources/client/src/models/immutable-record.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* External dependencies
*/
import EntitiesSearch from '@types';

export class ImmutableRecord< T > implements EntitiesSearch.Record< T > {
readonly #map: Record< string, T > = {};

public constructor( map: Record< string, T > = {} ) {
this.#map = map;
}

public get< F >( key: string, fallback?: F ): T | F | undefined {
return this.#map[ key ] ?? fallback;
}

public set( key: string, value: T ): ImmutableRecord< T > {
return new ImmutableRecord( { ...this.#map, [ key ]: value } );
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion sources/client/src/storage/entities/initial-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import EntitiesSearch from '@types';
/**
* Internal dependencies
*/
import { Set } from '../../vo/set';
import { Set } from '../../models/set';

type Options< V > = EntitiesSearch.ControlOption< V >;

Expand Down
2 changes: 1 addition & 1 deletion sources/client/src/storage/entities/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import EntitiesSearch from '@types';
/**
* Internal dependencies
*/
import { Set } from '../../vo/set';
import { Set } from '../../models/set';

/**
* @internal
Expand Down
18 changes: 13 additions & 5 deletions sources/client/src/utils/convert-entities-to-control-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,30 @@ import EntitiesSearch from '@types';
/**
* Internal dependencies
*/
import { ControlOption } from '../vo/control-option';
import { Set } from '../vo/set';
import { ControlOption } from '../value-objects/control-option';
import { Set } from '../models/set';
import { assert } from './assert';
import { ImmutableRecord } from '../models/immutable-record';

export function convertEntitiesToControlOptions<
V,
EntitiesFields extends { [ p: string ]: any },
>(
entities: Set< EntitiesFields >,
labelKey: string,
valueKey: string
valueKey: string,
...extraKeys: Array< string >
): Set< EntitiesSearch.ControlOption< V > > {
return entities.map( ( entity ) => {
const label = entity[ labelKey ];
const value = entity[ valueKey ];
assert( typeof label === 'string', 'Label Key must be a string' );
return new ControlOption( label, value );
assert( typeof label === 'string', 'Label key must be a string' );

const extra = extraKeys.reduce(
( record, key ) => ( { ...record, [ key ]: entity[ key ] } ),
{}
);

return new ControlOption( label, value, new ImmutableRecord( extra ) );
} );
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import EntitiesSearch from '@types';
/**
* Internal dependencies
*/
import { Set } from '../vo/set';
import { Set } from '../models/set';

export function orderSelectedOptionsAtTheTop< V >(
options: Set< EntitiesSearch.ControlOption< V > >,
Expand Down
2 changes: 1 addition & 1 deletion sources/client/src/utils/unique-control-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import EntitiesSearch from '@types';
/**
* Internal dependencies
*/
import { Set } from '../vo/set';
import { Set } from '../models/set';

// TODO Is this necessary due the new Set implementation?
export function uniqueControlOptions< V >(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import EntitiesSearch from '@types';
* Internal dependencies
*/
import { assert } from '../utils/assert';
import { ImmutableRecord } from '../models/immutable-record';

export class ControlOption< V > implements EntitiesSearch.ControlOption< V > {
export class ControlOption< V >
implements EntitiesSearch.EnrichedControlOption< V >
{
public readonly label: string;
public readonly value: V;
public readonly extra: EntitiesSearch.Record< unknown >;

public constructor( label: string, value: V ) {
public constructor(
label: string,
value: V,
extra: EntitiesSearch.Record< unknown > = new ImmutableRecord()
) {
assert(
label !== '',
'ControlOption: Label must be a non empty string.'
Expand All @@ -24,5 +32,6 @@ export class ControlOption< V > implements EntitiesSearch.ControlOption< V > {

this.label = label;
this.value = value;
this.extra = extra;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { CompositeEntitiesByKind } from '../../../../sources/client/src/componen
import { PluralSelectControl } from '../../../../sources/client/src/components/plural-select-control';
import { SearchControl } from '../../../../sources/client/src/components/search-control';
import { SingularSelectControl } from '../../../../sources/client/src/components/singular-select-control';
import { Set } from '../../../../sources/client/src/vo/set';
import { Set } from '../../../../sources/client/src/models/set';

jest.mock( '@wordpress/hooks', () => ( {
doAction: jest.fn(),
Expand All @@ -36,7 +36,11 @@ describe( 'CompositeEntitiesByKind', () => {

const rendered = render(
<CompositeEntitiesByKind
searchEntities={ () => Promise.resolve( new Set() ) }
searchEntities={ () =>
Promise.resolve(
new Set< EntitiesSearch.ControlOption< any > >()
)
}
entities={ {
value: new Set(),
onChange: () => {},
Expand Down Expand Up @@ -92,7 +96,9 @@ describe( 'CompositeEntitiesByKind', () => {
} }
kind={ {
value: new Set( [ 'post' ] ),
options: new Set(),
options: new Set<
EntitiesSearch.ControlOption< any >
>(),
onChange: () => {},
} }
>
Expand Down Expand Up @@ -335,7 +341,9 @@ describe( 'CompositeEntitiesByKind', () => {
} }
kind={ {
value: new Set( [ 'post' ] ),
options: new Set(),
options: new Set<
EntitiesSearch.ControlOption< any >
>(),
onChange: () => {},
} }
>
Expand Down Expand Up @@ -381,7 +389,9 @@ describe( 'CompositeEntitiesByKind', () => {
} }
kind={ {
value: new Set( [ 'post' ] ),
options: new Set(),
options: new Set<
EntitiesSearch.ControlOption< any >
>(),
onChange: () => {},
} }
>
Expand Down Expand Up @@ -414,7 +424,9 @@ describe( 'CompositeEntitiesByKind', () => {
} }
kind={ {
value: new Set( [ 'post' ] ),
options: new Set(),
options: new Set<
EntitiesSearch.ControlOption< any >
>(),
onChange: () => {},
} }
>
Expand Down Expand Up @@ -485,7 +497,11 @@ describe( 'CompositeEntitiesByKind', () => {

const rendered = render(
<CompositeEntitiesByKind
searchEntities={ () => Promise.resolve( new Set() ) }
searchEntities={ () =>
Promise.resolve(
new Set< EntitiesSearch.ControlOption< any > >()
)
}
entities={ {
value: new Set(),
onChange: () => {},
Expand Down
2 changes: 1 addition & 1 deletion tests/client/unit/api/search-entities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { doAction } from '@wordpress/hooks';
*/
import { fetch } from '../../../../sources/client/src/api/fetch';
import { searchEntities } from '../../../../sources/client/src/api/search-entities';
import { Set } from '../../../../sources/client/src/vo/set';
import { Set } from '../../../../sources/client/src/models/set';

jest.mock( '@wordpress/hooks', () => ( {
doAction: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { faker } from '@faker-js/faker';
* Internal dependencies
*/
import { PluralSelectControl } from '../../../../sources/client/src/components/plural-select-control';
import { Set } from '../../../../sources/client/src/vo/set';
import { Set } from '../../../../sources/client/src/models/set';
import { buildOptions } from '../utils';

describe( 'Posts Select', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/client/unit/components/radio-control.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { render, screen, fireEvent } from '@testing-library/react';
* Internal dependencies
*/
import { RadioControl } from '../../../../sources/client/src/components/radio-control';
import { Set } from '../../../../sources/client/src/vo/set';
import { Set } from '../../../../sources/client/src/models/set';

describe( 'KindRadioControl', () => {
it( 'renders the component', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { faker } from '@faker-js/faker';
* Internal dependencies
*/
import { SingularSelectControl } from '../../../../sources/client/src/components/singular-select-control';
import { Set } from '../../../../sources/client/src/vo/set';
import { Set } from '../../../../sources/client/src/models/set';
import { buildOptions } from '../utils';

describe( 'Post Types Select', () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/client/unit/components/toggle-control.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import userEvent from '@testing-library/user-event';
* Internal dependencies
*/
import { ToggleControl } from '../../../../sources/client/src/components/toggle-control';
import { Set } from '../../../../sources/client/src/vo/set';
import { Set } from '../../../../sources/client/src/models/set';

const options = new Set( [
{ label: 'Option 1', value: '1' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { doAction } from '@wordpress/hooks';
* Internal dependencies
*/
import { useEntitiesOptionsStorage } from '../../../../sources/client/src/hooks/use-entities-options-storage';
import { Set } from '../../../../sources/client/src/vo/set';
import { Set } from '../../../../sources/client/src/models/set';

jest.mock( '@wordpress/hooks', () => ( {
doAction: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { describe, it, jest, expect } from '@jest/globals';
*/
import { useEntityRecords } from '../../../../sources/client/src/hooks/use-entity-records';
import { useQueryViewablePostTypes } from '../../../../sources/client/src/hooks/use-query-viewable-post-types';
import { Set } from '../../../../sources/client/src/vo/set';
import { Set } from '../../../../sources/client/src/models/set';

jest.mock( '@wordpress/data', () => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { describe, expect, it, jest } from '@jest/globals';
*/
import { useEntityRecords } from '../../../../sources/client/src/hooks/use-entity-records';
import { useQueryViewableTaxonomies } from '../../../../sources/client/src/hooks/use-query-viewable-taxonomies';
import { Set } from '../../../../sources/client/src/vo/set';
import { Set } from '../../../../sources/client/src/models/set';

jest.mock( '../../../../sources/client/src/hooks/use-entity-records', () => {
return {
Expand Down
Loading