-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1945 from ThemeFuse/23522-filtersFromApi
Filters From Api
- Loading branch information
Showing
8 changed files
with
315 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { getFields } from "../../api"; | ||
import { Filters } from "../../types/Filters"; | ||
import { t } from "../../utils/i18n"; | ||
import { converter, parseFields } from "./utils"; | ||
|
||
export const handler: Filters["handler"] = async (res, rej, data) => { | ||
try { | ||
const result = await getFields(data); | ||
|
||
const convertedValue = converter(result); | ||
|
||
res(convertedValue ?? []); | ||
} catch (e) { | ||
rej(t("Failed to load sources")); | ||
} | ||
}; | ||
|
||
export const possibleValues: Filters["possibleValues"] = async ( | ||
res, | ||
rej, | ||
{ type, search, optionSource, postId, loopAttributes } | ||
) => { | ||
try { | ||
const result = await getFields({ postId, loopAttributes }); | ||
|
||
const convertPossibleValues = parseFields( | ||
result, | ||
optionSource, | ||
type, | ||
search | ||
); | ||
|
||
res(convertPossibleValues ?? []); | ||
} catch (e) { | ||
rej(t("Failed to load sources")); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { | ||
Choice, | ||
FilterFieldsData, | ||
fromStringQueryTypeSource, | ||
isChoiceArray, | ||
isPossibleValueArray, | ||
PossibleValue, | ||
QueryTypeSource | ||
} from "../../types/Filters"; | ||
import { isAllValuesValid } from "../../utils/reader/array"; | ||
|
||
export const createQueryTypeSource = ( | ||
query: string, | ||
type: string, | ||
source: string, | ||
filterBy: string | ||
): QueryTypeSource | undefined => | ||
fromStringQueryTypeSource(`${query}|||${type}|||${source}|||${filterBy}`); | ||
|
||
export const converter = (data: FilterFieldsData): Choice[] => { | ||
const arr = Object.values(data).reduce((acc: Choice[], cur) => { | ||
const field = cur.map((item) => { | ||
return { | ||
value: | ||
createQueryTypeSource( | ||
getQuery(item.filterQuery, item.optionQuery, item.optionSource), | ||
item.type, | ||
item.optionSource, | ||
item.filterBy | ||
) ?? "", | ||
title: item.label | ||
}; | ||
}); | ||
return [...acc, ...field]; | ||
}, []); | ||
|
||
return arr ?? []; | ||
}; | ||
|
||
export const getQuery = ( | ||
filterQuery: string, | ||
optionQuery: string, | ||
optionSource: string | ||
): string => filterQuery || optionQuery || `metaKey=${optionSource}`; | ||
|
||
export const parseFields = ( | ||
data: FilterFieldsData, | ||
optionSource: string, | ||
type: string, | ||
search?: string | ||
): Choice[] => { | ||
const allItem = { value: "all", title: type === "inc" ? "All" : "None" }; | ||
|
||
const selectedItem = Object.values(data).reduce((acc, cur) => { | ||
const field = cur.filter((item) => item.optionQuery === optionSource); | ||
|
||
return [...acc, ...field]; | ||
}, [])[0]; | ||
|
||
if (selectedItem.optionSource === "tax") { | ||
const parsedValues: PossibleValue[] = isPossibleValueArray( | ||
selectedItem.possibleValues | ||
) | ||
? selectedItem.possibleValues | ||
: []; | ||
const items: Choice[] = parsedValues.map((it) => ({ | ||
value: it.term_id, | ||
title: it.name | ||
})); | ||
|
||
if (search) { | ||
return [ | ||
allItem, | ||
...items.filter((it) => | ||
it.title.toLocaleLowerCase().includes(search.toLocaleLowerCase()) | ||
) | ||
]; | ||
} | ||
|
||
if (isAllValuesValid(items)) { | ||
return [allItem, ...items]; | ||
} | ||
} | ||
|
||
if (selectedItem.optionSource === "meta") { | ||
const items = isChoiceArray(selectedItem.possibleValues) | ||
? selectedItem.possibleValues | ||
: []; | ||
|
||
if (search) { | ||
return [ | ||
allItem, | ||
...items.filter((it) => | ||
it.title.toLocaleLowerCase().includes(search.toLocaleLowerCase()) | ||
) | ||
]; | ||
} | ||
|
||
if (isAllValuesValid(items)) { | ||
return [allItem, ...items]; | ||
} | ||
} | ||
|
||
return [allItem]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { pass } from "fp-utilities"; | ||
import { Literal } from "../utils/types"; | ||
import { NewType } from "./NewType"; | ||
import { Response } from "./Response"; | ||
|
||
export type Choice = { title: string; value: Literal }; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const isChoice = (values: any): values is Choice => { | ||
return typeof values === "object" && "title" in values && "value" in values; | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const isChoiceArray = (arr: any): arr is Choice[] => { | ||
if (!Array.isArray(arr)) return false; | ||
for (const item of arr) { | ||
if (!isChoice(item)) return false; | ||
} | ||
return true; | ||
}; | ||
|
||
export type QueryTypeSource = NewType<string, "tripleString">; | ||
|
||
export const isQueryTypeSource = (s: string): s is QueryTypeSource => | ||
s.split("|||").length === 3; | ||
|
||
export const fromStringQueryTypeSource = pass(isQueryTypeSource); | ||
|
||
interface FieldsCommon { | ||
filterQuery: string; | ||
label: string; | ||
optionQuery: string; | ||
optionSource: string; | ||
filterBy: string; | ||
type: string; | ||
} | ||
|
||
export interface FilterField extends FieldsCommon { | ||
possibleValues: PossibleValue[] | Choice[]; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const isPossibleValue = (values: any): values is PossibleValue => { | ||
return ( | ||
typeof values === "object" && | ||
"count" in values && | ||
"description" in values && | ||
"filter" in values && | ||
"name" in values && | ||
"parent" in values && | ||
"slug" in values && | ||
"taxonomy" in values && | ||
"term_group" in values && | ||
"term_id" in values && | ||
"term_taxonomy_id" in values | ||
); | ||
}; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
export const isPossibleValueArray = (arr: any): arr is PossibleValue[] => { | ||
if (!Array.isArray(arr)) return false; | ||
for (const item of arr) { | ||
if (!isPossibleValue(item)) return false; | ||
} | ||
return true; | ||
}; | ||
|
||
export type PossibleValue = { | ||
count: number; | ||
description: string; | ||
filter: string; | ||
name: string; | ||
parent: number; | ||
slug: string; | ||
taxonomy: string; | ||
term_group: number; | ||
term_id: number; | ||
term_taxonomy_id: number; | ||
}; | ||
|
||
export type FilterFieldsData = { | ||
fields: FilterField[]; | ||
taxonomies: FilterField[]; | ||
metaFields: FilterField[]; | ||
authors: FilterField[]; | ||
}; | ||
|
||
export interface FiltersHandlerArgs { | ||
postId: string; | ||
loopAttributes: string; | ||
} | ||
|
||
export interface FiltersValuesArgs { | ||
postId: string; | ||
loopAttributes: string; | ||
optionSource: string; | ||
type: string; | ||
search?: string; | ||
} | ||
|
||
export interface Filters { | ||
handler: ( | ||
res: Response<Choice[]>, | ||
rej: Response<string>, | ||
args: FiltersHandlerArgs | ||
) => void; | ||
possibleValues: ( | ||
res: Response<Choice[]>, | ||
rej: Response<string>, | ||
data: FiltersValuesArgs | ||
) => void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters