-
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.
- Loading branch information
1 parent
a69fd4a
commit 264b8fd
Showing
6 changed files
with
263 additions
and
1 deletion.
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,134 @@ | ||
import { getFields } from "../../api"; | ||
import { | ||
Choice, | ||
FilterFieldsData, | ||
Filters, | ||
PossibleValue, | ||
QueryTypeSource | ||
} from "../../types/Filters"; | ||
import { t } from "../../utils/i18n"; | ||
|
||
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")); | ||
} | ||
}; | ||
|
||
const createQueryTypeSource = ( | ||
query: string, | ||
type: string, | ||
source: string, | ||
filterBy: string | ||
): QueryTypeSource => { | ||
return `${query}|||${type}|||${source}|||${filterBy}` as QueryTypeSource; | ||
}; | ||
|
||
const converter = (data: FilterFieldsData): Choice[] => { | ||
const arr = Object.values(data).reduce((acc: Choice[], cur) => { | ||
const field = cur.map((item) => { | ||
return { | ||
value: createQueryTypeSource( | ||
item.filterQuery | ||
? item.filterQuery | ||
: item.optionQuery | ||
? item.optionQuery | ||
: `metaKey=${item.optionSource}`, | ||
item.type, | ||
item.optionSource, | ||
item.filterBy | ||
), | ||
title: item.label | ||
}; | ||
}); | ||
return [...acc, ...field]; | ||
}, []); | ||
|
||
return arr ?? []; | ||
}; | ||
|
||
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 items: Choice[] = ( | ||
selectedItem.possibleValues as PossibleValue[] | ||
).map((it: PossibleValue) => ({ | ||
value: it.term_id, | ||
title: it.name | ||
})); | ||
|
||
if (search) { | ||
return [ | ||
allItem, | ||
...items.filter((it) => | ||
it.title.toLocaleLowerCase().includes(search.toLocaleLowerCase()) | ||
) | ||
]; | ||
} | ||
|
||
if (checkNotNullOrUndefinedValues(items)) { | ||
return [allItem, ...items]; | ||
} | ||
} | ||
|
||
if (selectedItem.optionSource === "meta") { | ||
const items = selectedItem.possibleValues as Choice[]; | ||
|
||
if (search) { | ||
return [ | ||
allItem, | ||
...items.filter((it) => | ||
it.title.toLocaleLowerCase().includes(search.toLocaleLowerCase()) | ||
) | ||
]; | ||
} | ||
|
||
if (checkNotNullOrUndefinedValues(items)) { | ||
return [allItem, ...items]; | ||
} | ||
} | ||
|
||
return [allItem]; | ||
}; | ||
|
||
function checkNotNullOrUndefinedValues(arr: unknown[]) { | ||
return arr.every((value) => value !== null && value !== undefined); | ||
} |
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,61 @@ | ||
import { NewType } from "./NewType"; | ||
import { Response } from "./Response"; | ||
|
||
export type Choice = { title: string; value: string | number }; | ||
|
||
export type QueryTypeSource = NewType<string, "tripleString">; | ||
|
||
interface FieldsCommon { | ||
filterQuery: string; | ||
label: string; | ||
optionQuery: string; | ||
optionSource: string; | ||
filterBy: string; | ||
type: string; | ||
} | ||
|
||
export interface FilterField extends FieldsCommon { | ||
possibleValues: PossibleValue[] | Choice[]; | ||
} | ||
|
||
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 Filters { | ||
handler: ( | ||
res: Response<Choice[]>, | ||
rej: Response<string>, | ||
args: { | ||
postId: string; | ||
loopAttributes: string; | ||
} | ||
) => void; | ||
possibleValues: ( | ||
res: Response<Choice[]>, | ||
rej: Response<string>, | ||
data: { | ||
postId: string; | ||
loopAttributes: string; | ||
optionSource: string; | ||
type: string; | ||
search?: string; | ||
} | ||
) => 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