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

refactor(picker-starter): place query and filters logic directly into the picker.config.ts file #91

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions picker-starter/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './basket/BasketItem'
export * from './basket'
export * from './matchCategories'
export * from './matchItem'
export * from './service'
export * from './types'
Expand Down
1 change: 1 addition & 0 deletions picker-starter/src/core/matchCategories/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './matchCategories'
9 changes: 9 additions & 0 deletions picker-starter/src/core/matchCategories/matchCategories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { MockItem } from '@/data'

export const matchCategories =
(categoryNames: string[]) => (items: MockItem) => {
if (categoryNames.length === 0) {
return true
}
return categoryNames.some((categoryName) => items.category === categoryName)
}
6 changes: 0 additions & 6 deletions picker-starter/src/data/categories.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
export type MockCategory = {
name: string
description?: string
}

// TODO this should become a tree
export const categoryMockAssets: MockCategory[] = [
{
name: 'Kitchen',
description:
'Knives, forks, spoons... and everything else that you can think of for your home kitchen!',
},
{
name: 'Food',
description: 'Yummy things for your tummy!',
},
{
name: 'Beverages',
description: 'Beeeeeeer',
},
{
name: 'Electronics',
Expand All @@ -35,6 +30,5 @@ export const categoryMockAssets: MockCategory[] = [
},
{
name: 'Hygiene',
description: 'Take care of yourself',
},
]
71 changes: 2 additions & 69 deletions picker-starter/src/data/items.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
import {
ItemQuery,
FilterList,
FilterItem,
FilterOption,
matchItem,
BasketItem,
} from '@/core'
import {
compareName,
capitalizeWord,
getPage,
delayed,
randomDelay,
} from '@/utils'
import { categoryMockAssets } from './categories'
import { BasketItem } from '@/core'
import { compareName, capitalizeWord } from '@/utils'

export type TempItem = {
id: number
Expand Down Expand Up @@ -659,56 +645,3 @@ export const items: MockItem[] = tmpAssets
} as MockItem
})
.sort(compareName)

export const getItemFilters: FilterList = async () => {
const options: FilterOption[] = categoryMockAssets.map<FilterOption>(
(category) => {
return {
label: category.name,
value: category.name,
}
},
)

const response: FilterItem[] = [
{
type: 'multi',
options: options,
defaultValue: [],
label: 'Categories',
name: 'categoryMulti',
},
]

// It mimics an API call by adding a delay before return the data.
return delayed(randomDelay(), response)
}

export const queryItems: ItemQuery = async ({
searchTerm,
page,
perPage,
filterSelection,
}) => {
const matchCategories = (categoryNames: string[]) => (items: MockItem) => {
if (categoryNames.length === 0) {
return true
}
return categoryNames.some((categoryName) => items.category === categoryName)
}

const allSearchResults = items
.filter(matchCategories(filterSelection['categoryMulti'] as string[]))
.filter(matchItem(searchTerm))

const paginatedResults = getPage(allSearchResults, page, perPage)
const response = {
items: paginatedResults,
pageInfo: {
totalCount: allSearchResults.length,
},
}

// It mimics an API call by adding a delay before return the data.
return delayed(randomDelay(), response)
}
75 changes: 48 additions & 27 deletions picker-starter/src/picker.config.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
import { getItemFilters, queryItems } from '@/data'
import { defineConfig } from '@/core'
import { categoryMockAssets, items } from '@/data'
import { defineConfig, matchCategories, matchItem } from '@/core'
import { StoryblokIcon } from './components'
import { getPage } from './utils'

export default defineConfig((options) => {
return {
title: 'Picker Starter',
icon: StoryblokIcon,
validateOptions: () => {
const { limit } = options
export default defineConfig((options) => ({
title: 'Picker Starter',
icon: StoryblokIcon,
validateOptions: () => {
const { limit } = options

const isLimitOptionValid = limit === undefined || Number(limit) > 0

if (!isLimitOptionValid) {
return {
isValid: false,
error: `The 'limit' option must be an integer greater than 0`,
}
}
const isLimitOptionValid = limit === undefined || Number(limit) > 0

if (!isLimitOptionValid) {
return {
isValid: true,
isValid: false,
error: `The 'limit' option must be an integer greater than 0`,
}
},
tabs: [
{
name: 'items',
label: 'Items',
query: queryItems,
getFilters: getItemFilters,
}

return {
isValid: true,
}
},
tabs: [
{
name: 'items',
label: 'Items',
query: async ({ searchTerm, page, perPage, filterSelection }) => {
const filteredItems = items
.filter(matchCategories(filterSelection['categoryMulti'] as string[]))
.filter(matchItem(searchTerm))
Copy link
Contributor

Choose a reason for hiding this comment

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

matchItem sounds a bit unintuitive to me. It can be something like matchSearchTerm...?

Or we could rename them altogether like:

        const filteredItems = items
          .filter(filterByCategories(filterSelection['categoryMulti'] as string[]))
          .filter(filterBySearchTerm(searchTerm))

What do you think? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

matchItem sounds a bit unintuitive to me. It can be something like matchSearchTerm...?

Yeah, you're totally right, matchSearchTerm is much better.

About the filterByCategories and filterBySearchTerm renaming, I'm not too sure.
These names suggest to me this function is going to perform the filtering action while it doesn't.

I don't know... somehow using match... was more meaningful to me.

What do you think, @eunjae-lee?

Copy link
Contributor

Choose a reason for hiding this comment

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

okay, then it sounds good. let's merge!


return {
items: getPage(filteredItems, page, perPage),
pageInfo: {
totalCount: filteredItems.length,
},
}
},
],
}
})
getFilters: async () => [
{
type: 'multi',
label: 'Categories',
name: 'categoryMulti',
defaultValue: [],
options: categoryMockAssets.map((category) => ({
Copy link
Contributor

Choose a reason for hiding this comment

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

what if categoryMockAssets is just categories? like items above.

Or we could name them something like:

  • sampleItems
  • sampleCategories

"mock" feels like unit testing to me.

label: category.name,
value: category.name,
})),
},
],
},
],
}))
11 changes: 0 additions & 11 deletions picker-starter/src/utils/delayed/delayed.ts

This file was deleted.

1 change: 0 additions & 1 deletion picker-starter/src/utils/delayed/index.ts

This file was deleted.

1 change: 0 additions & 1 deletion picker-starter/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
export * from './capitalizeWord'
export * from './compareName'
export * from './delayed'
export * from './getPage'
export * from './initials'
export * from './hasKey'
Expand Down