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

Extract filter definition to constants directory #1038

Merged
merged 6 commits into from Mar 5, 2022

Conversation

Copy link
Contributor

@obulat obulat commented Mar 3, 2022

Fixes

Related to #1037 by @obulat

This is the first of 3 search store conversion PRs, and should be reviewed first.

Description

This PR extracts the filter creation from the search store file into a separate file in the constants module. I'm not sure this is the best location: we do create constants for initial filters here, but we use some functions to do that.

Other small changes in this PR:

  • mature filter is changed from a boolean value to an array with a single object [{ code: 'mature', name: 'the i18n key', checked: false }] to match the other filters.
  • set the default searchType for the search store to ALL_MEDIA.
  • convert string utils and the filters constants file to TypeScript.
  • adds a component import to silence test warnings.

Testing Instructions

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the default branch of the repository (main) or a parent feature branch.
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added or updated tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@obulat obulat added 🟨 priority: medium 🌟 goal: addition 💻 aspect: code labels Mar 3, 2022
@obulat obulat added this to the Switch from Vuex to Pinia milestone Mar 3, 2022
@obulat obulat self-assigned this Mar 3, 2022
@obulat obulat requested a review from as a code owner Mar 3, 2022
@obulat obulat requested review from krysal (assigned from WordPress/openverse-frontend) and dhruvkb (assigned from WordPress/openverse-frontend) Mar 3, 2022
@dhruvkb dhruvkb added this to Needs review in Openverse PRs Mar 3, 2022
@obulat obulat force-pushed the pinia_search_extract_filter_constants branch from d623755 to 05b4a1a Compare Mar 3, 2022
src/constants/filters.ts Outdated Show resolved Hide resolved
@@ -178,7 +87,7 @@ export const state = () => ({
size: '',
source: '',
searchBy: '',
mature: false,
mature: '',
Copy link
Member

@zackkrida zackkrida Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if the mature filter used the strings 'true' and 'false', so that we wouldn't need any custom logic for it in the search query transform functions?

Copy link
Contributor Author

@obulat obulat Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would mean that we would have to move the custom logic somewhere else. Because there would have to be the logic of removing 'false' from the API query. Or do we want to send 'mature=false' with every query, instead of only adding 'mature=true' to the queries that set mature to true?

Copy link
Member

@zackkrida zackkrida Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yes, that's a good point. But would using '' and 'true' fix that?

Copy link
Contributor Author

@obulat obulat Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export const filtersToQueryData = (

In search-query-transform, above, we use the query object from the search store, and convert it into the the object that is used to create the API request URL. There, we use the hideEmpty parameter set to true, so any query parameter that is equal to '' is discarded, and non-blank parameters are used in the query string.

Copy link
Contributor Author

@obulat obulat Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, in short, mature: 'anykindofstring' is converted to /?q=cat&mature=anykindofstring, but mature: '' is converted to ?q=cat.

Copy link
Member

@zackkrida zackkrida Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's what I thought, thanks! So we could use '' for the default, off value of the mature filter, and the string 'true' as the value for showing mature images, which would allow us to avoid any custom conditionals for the mature filter in our search query transformation logic

Copy link
Member

@zackkrida zackkrida left a comment

LGTM! I have one question left about the mature filter, but it's a minor improvement.

Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

This looks really great! I tested it locally and operationally it seems perfect. I tried a bunch of different filter scenarios and all of them worked.

Just noticed a couple things with the types, in particular with the initFilters type, that we should fix before merging. After that this is G2G from my perspective.

* A list of filters that are only used for the specific content type.
* This is used to clear filters from other content types when changing the content type.
*/
export const mediaUniqueFilterKeys = deepFreeze<Record<SearchType, string[]>>({
Copy link
Contributor

@sarayourfriend sarayourfriend Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these also filter types maybe?

Suggested change
export const mediaUniqueFilterKeys = deepFreeze<Record<SearchType, string[]>>({
export const mediaUniqueFilterKeys = deepFreeze<Record<SearchType, FilterType[]>>({

@@ -47,7 +47,7 @@ export type Query = {
categories: string
source: string
duration: string
mature: boolean
mature: string
Copy link
Contributor

@sarayourfriend sarayourfriend Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes me want to change all these string types to specific value types for each of them.

@@ -130,6 +130,7 @@ export interface Filters {
searchBy: FilterItem[]
mature: boolean
}
export type FilterType = keyof Filters
Copy link
Contributor

@sarayourfriend sarayourfriend Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Type suffix is kind of confusing here and generally best avoided when naming types due to the ambiguity of it... would FilterCategory be clearer (and also accurate)?

Copy link
Contributor Author

@obulat obulat Mar 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not realized that it's the name of the type that ends with Type :) Name updated.

src/store/types.ts Outdated Show resolved Hide resolved
tsconfig.json Outdated
@@ -43,6 +43,7 @@
"src/composables/window.js",
"src/constants/media.js",
"src/constants/screens.js",
"src/constants/filters",
Copy link
Contributor

@sarayourfriend sarayourfriend Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be left out, all .ts files are automatically included in the tsconfig scope.

Also for future reference these need to have the extension as well or the file won't get picked up.

Suggested change
"src/constants/filters",

checked: false,
})),
}),
{}
Copy link
Contributor

@sarayourfriend sarayourfriend Mar 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So... the Array.prototype.reduce function's type inference is a little weird. It'll cast the return type as the inferred type of the initial value parameter, so we need to either make sure that the initial value parameter has the right type to begin with, or (in this and most cases) cast it to the correct type.

Suggested change
{}
{} as Record<FilterType, FilterItem[]>

Currently initFilters has the type () => {} (i.e., returning an empty object literal). So filterData is Readonly<{}>. We're not seeing errors now because when we're using it elsewhere it's not being type-checked, but once we start using it in other type checked files it'll start complaining about this (probably with slightly confusing errors too unfortunately) so best to fix it now before it bites us down the line.

Copy link
Contributor Author

@obulat obulat Mar 4, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL :) Thank you!

@obulat obulat requested a review from sarayourfriend Mar 4, 2022
Copy link
Contributor

@sarayourfriend sarayourfriend left a comment

LGTM! 🎉

Openverse PRs automation moved this from Needs review to Reviewer approved Mar 4, 2022
@obulat obulat merged commit 6d82d48 into main Mar 5, 2022
10 checks passed
Openverse PRs automation moved this from Reviewer approved to Merged! Mar 5, 2022
@obulat obulat deleted the pinia_search_extract_filter_constants branch Mar 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💻 aspect: code 🌟 goal: addition 🟨 priority: medium
Projects
Openverse PRs
  
Merged!
Development

Successfully merging this pull request may close these issues.

None yet

3 participants