mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-11-30 08:11:14 +00:00
Fix bug with search page creator select
This commit is contained in:
parent
4a1a6d3ef3
commit
4a55278556
3 changed files with 96 additions and 92 deletions
|
@ -15,6 +15,7 @@ import { restoreScrollPosition, saveScrollPosition } from "@utils/browser";
|
||||||
import {
|
import {
|
||||||
capitalizeFirstLetter,
|
capitalizeFirstLetter,
|
||||||
debounce,
|
debounce,
|
||||||
|
dedupByProperty,
|
||||||
getIdFromString,
|
getIdFromString,
|
||||||
getPageFromString,
|
getPageFromString,
|
||||||
getQueryParams,
|
getQueryParams,
|
||||||
|
@ -33,7 +34,6 @@ import {
|
||||||
GetPersonDetails,
|
GetPersonDetails,
|
||||||
GetPersonDetailsResponse,
|
GetPersonDetailsResponse,
|
||||||
GetSiteResponse,
|
GetSiteResponse,
|
||||||
ListCommunities,
|
|
||||||
ListCommunitiesResponse,
|
ListCommunitiesResponse,
|
||||||
ListingType,
|
ListingType,
|
||||||
PersonView,
|
PersonView,
|
||||||
|
@ -88,8 +88,6 @@ type FilterType = "creator" | "community";
|
||||||
interface SearchState {
|
interface SearchState {
|
||||||
searchRes: RequestState<SearchResponse>;
|
searchRes: RequestState<SearchResponse>;
|
||||||
resolveObjectRes: RequestState<ResolveObjectResponse>;
|
resolveObjectRes: RequestState<ResolveObjectResponse>;
|
||||||
creatorDetailsRes: RequestState<GetPersonDetailsResponse>;
|
|
||||||
communityRes: RequestState<GetCommunityResponse>;
|
|
||||||
siteRes: GetSiteResponse;
|
siteRes: GetSiteResponse;
|
||||||
searchText?: string;
|
searchText?: string;
|
||||||
communitySearchOptions: Choice[];
|
communitySearchOptions: Choice[];
|
||||||
|
@ -196,7 +194,7 @@ const Filter = ({
|
||||||
label: I18NextService.i18n.t("all"),
|
label: I18NextService.i18n.t("all"),
|
||||||
value: "0",
|
value: "0",
|
||||||
},
|
},
|
||||||
].concat(options)}
|
].concat(dedupByProperty(options, option => option.value))}
|
||||||
value={value ?? 0}
|
value={value ?? 0}
|
||||||
onSearch={onSearch}
|
onSearch={onSearch}
|
||||||
onChange={onChange}
|
onChange={onChange}
|
||||||
|
@ -244,8 +242,6 @@ export class Search extends Component<any, SearchState> {
|
||||||
|
|
||||||
state: SearchState = {
|
state: SearchState = {
|
||||||
resolveObjectRes: EMPTY_REQUEST,
|
resolveObjectRes: EMPTY_REQUEST,
|
||||||
creatorDetailsRes: EMPTY_REQUEST,
|
|
||||||
communityRes: EMPTY_REQUEST,
|
|
||||||
siteRes: this.isoData.site_res,
|
siteRes: this.isoData.site_res,
|
||||||
creatorSearchOptions: [],
|
creatorSearchOptions: [],
|
||||||
communitySearchOptions: [],
|
communitySearchOptions: [],
|
||||||
|
@ -267,10 +263,7 @@ export class Search extends Component<any, SearchState> {
|
||||||
|
|
||||||
const { q } = getSearchQueryParams();
|
const { q } = getSearchQueryParams();
|
||||||
|
|
||||||
this.state = {
|
this.state.searchText = q;
|
||||||
...this.state,
|
|
||||||
searchText: q,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Only fetch the data if coming from another route
|
// Only fetch the data if coming from another route
|
||||||
if (FirstLoadService.isFirstLoad) {
|
if (FirstLoadService.isFirstLoad) {
|
||||||
|
@ -282,28 +275,18 @@ export class Search extends Component<any, SearchState> {
|
||||||
searchResponse: searchRes,
|
searchResponse: searchRes,
|
||||||
} = this.isoData.routeData;
|
} = this.isoData.routeData;
|
||||||
|
|
||||||
this.state = {
|
this.state.isIsomorphic = true;
|
||||||
...this.state,
|
|
||||||
isIsomorphic: true,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (creatorDetailsRes?.state === "success") {
|
if (creatorDetailsRes?.state === "success") {
|
||||||
this.state = {
|
this.state.creatorSearchOptions =
|
||||||
...this.state,
|
creatorDetailsRes.state === "success"
|
||||||
creatorSearchOptions:
|
? [personToChoice(creatorDetailsRes.data.person_view)]
|
||||||
creatorDetailsRes?.state === "success"
|
: [];
|
||||||
? [personToChoice(creatorDetailsRes.data.person_view)]
|
|
||||||
: [],
|
|
||||||
creatorDetailsRes,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (communitiesRes?.state === "success") {
|
if (communitiesRes?.state === "success") {
|
||||||
this.state = {
|
this.state.communitySearchOptions =
|
||||||
...this.state,
|
communitiesRes.data.communities.map(communityToChoice);
|
||||||
communitySearchOptions:
|
|
||||||
communitiesRes.data.communities.map(communityToChoice),
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (communityRes?.state === "success") {
|
if (communityRes?.state === "success") {
|
||||||
|
@ -312,31 +295,23 @@ export class Search extends Component<any, SearchState> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (q !== "") {
|
if (searchRes?.state === "success") {
|
||||||
this.state = {
|
this.state.searchRes = searchRes;
|
||||||
...this.state,
|
}
|
||||||
};
|
|
||||||
|
|
||||||
if (searchRes?.state === "success") {
|
if (resolveObjectRes?.state === "success") {
|
||||||
this.state = {
|
this.state.resolveObjectRes = resolveObjectRes;
|
||||||
...this.state,
|
|
||||||
searchRes,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (resolveObjectRes?.state === "success") {
|
|
||||||
this.state = {
|
|
||||||
...this.state,
|
|
||||||
resolveObjectRes,
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async componentDidMount() {
|
async componentDidMount() {
|
||||||
if (!this.state.isIsomorphic) {
|
if (!this.state.isIsomorphic) {
|
||||||
this.setState({ searchCommunitiesLoading: true });
|
this.setState({
|
||||||
|
searchCommunitiesLoading: true,
|
||||||
|
searchCreatorLoading: true,
|
||||||
|
});
|
||||||
|
|
||||||
const promises: Promise<any>[] = [
|
const promises: Promise<any>[] = [
|
||||||
HttpService.client
|
HttpService.client
|
||||||
.listCommunities({
|
.listCommunities({
|
||||||
|
@ -354,7 +329,7 @@ export class Search extends Component<any, SearchState> {
|
||||||
}),
|
}),
|
||||||
];
|
];
|
||||||
|
|
||||||
const { communityId } = getSearchQueryParams();
|
const { communityId, creatorId } = getSearchQueryParams();
|
||||||
|
|
||||||
if (communityId) {
|
if (communityId) {
|
||||||
promises.push(
|
promises.push(
|
||||||
|
@ -372,6 +347,24 @@ export class Search extends Component<any, SearchState> {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (creatorId) {
|
||||||
|
promises.push(
|
||||||
|
HttpService.client
|
||||||
|
.getPersonDetails({
|
||||||
|
person_id: creatorId,
|
||||||
|
})
|
||||||
|
.then(res => {
|
||||||
|
if (res.state === "success") {
|
||||||
|
this.setState(prev => {
|
||||||
|
prev.creatorSearchOptions.push(
|
||||||
|
personToChoice(res.data.person_view),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (this.state.searchText) {
|
if (this.state.searchText) {
|
||||||
promises.push(this.search());
|
promises.push(this.search());
|
||||||
}
|
}
|
||||||
|
@ -380,6 +373,7 @@ export class Search extends Component<any, SearchState> {
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
searchCommunitiesLoading: false,
|
searchCommunitiesLoading: false,
|
||||||
|
searchCreatorLoading: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -394,26 +388,20 @@ export class Search extends Component<any, SearchState> {
|
||||||
}: InitialFetchRequest<QueryParams<SearchProps>>): Promise<SearchData> {
|
}: InitialFetchRequest<QueryParams<SearchProps>>): Promise<SearchData> {
|
||||||
const community_id = getIdFromString(communityId);
|
const community_id = getIdFromString(communityId);
|
||||||
let communityResponse: RequestState<GetCommunityResponse> = EMPTY_REQUEST;
|
let communityResponse: RequestState<GetCommunityResponse> = EMPTY_REQUEST;
|
||||||
let listCommunitiesResponse: RequestState<ListCommunitiesResponse> =
|
|
||||||
EMPTY_REQUEST;
|
|
||||||
if (community_id) {
|
if (community_id) {
|
||||||
const getCommunityForm: GetCommunity = {
|
const getCommunityForm: GetCommunity = {
|
||||||
id: community_id,
|
id: community_id,
|
||||||
};
|
};
|
||||||
|
|
||||||
communityResponse = await client.getCommunity(getCommunityForm);
|
communityResponse = await client.getCommunity(getCommunityForm);
|
||||||
} else {
|
|
||||||
const listCommunitiesForm: ListCommunities = {
|
|
||||||
type_: defaultListingType,
|
|
||||||
sort: defaultSortType,
|
|
||||||
limit: fetchLimit,
|
|
||||||
};
|
|
||||||
|
|
||||||
listCommunitiesResponse = await client.listCommunities(
|
|
||||||
listCommunitiesForm,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const listCommunitiesResponse = await client.listCommunities({
|
||||||
|
type_: defaultListingType,
|
||||||
|
sort: defaultSortType,
|
||||||
|
limit: fetchLimit,
|
||||||
|
});
|
||||||
|
|
||||||
const creator_id = getIdFromString(creatorId);
|
const creator_id = getIdFromString(creatorId);
|
||||||
let creatorDetailsResponse: RequestState<GetPersonDetailsResponse> =
|
let creatorDetailsResponse: RequestState<GetPersonDetailsResponse> =
|
||||||
EMPTY_REQUEST;
|
EMPTY_REQUEST;
|
||||||
|
@ -443,21 +431,19 @@ export class Search extends Component<any, SearchState> {
|
||||||
limit: fetchLimit,
|
limit: fetchLimit,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (query !== "") {
|
searchResponse = await client.search(form);
|
||||||
searchResponse = await client.search(form);
|
if (myAuth()) {
|
||||||
if (myAuth()) {
|
const resolveObjectForm: ResolveObject = {
|
||||||
const resolveObjectForm: ResolveObject = {
|
q: query,
|
||||||
q: query,
|
};
|
||||||
};
|
resolveObjectResponse = await HttpService.silent_client.resolveObject(
|
||||||
resolveObjectResponse = await HttpService.silent_client.resolveObject(
|
resolveObjectForm,
|
||||||
resolveObjectForm,
|
);
|
||||||
);
|
|
||||||
|
|
||||||
// If we return this object with a state of failed, the catch-all-handler will redirect
|
// If we return this object with a state of failed, the catch-all-handler will redirect
|
||||||
// to an error page, so we ignore it by covering up the error with the empty state.
|
// to an error page, so we ignore it by covering up the error with the empty state.
|
||||||
if (resolveObjectResponse.state === "failed") {
|
if (resolveObjectResponse.state === "failed") {
|
||||||
resolveObjectResponse = EMPTY_REQUEST;
|
resolveObjectResponse = EMPTY_REQUEST;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -995,34 +981,28 @@ export class Search extends Component<any, SearchState> {
|
||||||
}
|
}
|
||||||
|
|
||||||
handleCreatorSearch = debounce(async (text: string) => {
|
handleCreatorSearch = debounce(async (text: string) => {
|
||||||
const { creatorId } = getSearchQueryParams();
|
|
||||||
const { creatorSearchOptions } = this.state;
|
|
||||||
const newOptions: Choice[] = [];
|
|
||||||
|
|
||||||
this.setState({ searchCreatorLoading: true });
|
|
||||||
|
|
||||||
const selectedChoice = creatorSearchOptions.find(
|
|
||||||
choice => getIdFromString(choice.value) === creatorId,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (selectedChoice) {
|
|
||||||
newOptions.push(selectedChoice);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (text.length > 0) {
|
if (text.length > 0) {
|
||||||
newOptions.push(...(await fetchUsers(text)).map(personToChoice));
|
const { creatorId } = getSearchQueryParams();
|
||||||
}
|
const { creatorSearchOptions } = this.state;
|
||||||
|
|
||||||
this.setState({
|
this.setState({ searchCreatorLoading: true });
|
||||||
searchCreatorLoading: false,
|
|
||||||
creatorSearchOptions: newOptions,
|
const newOptions = creatorSearchOptions
|
||||||
});
|
.filter(choice => getIdFromString(choice.value) === creatorId)
|
||||||
|
.concat((await fetchUsers(text)).map(personToChoice));
|
||||||
|
|
||||||
|
this.setState({
|
||||||
|
searchCreatorLoading: false,
|
||||||
|
creatorSearchOptions: newOptions,
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
handleCommunitySearch = debounce(async (text: string) => {
|
handleCommunitySearch = debounce(async (text: string) => {
|
||||||
if (text.length > 0) {
|
if (text.length > 0) {
|
||||||
const { communityId } = getSearchQueryParams();
|
const { communityId } = getSearchQueryParams();
|
||||||
const { communitySearchOptions } = this.state;
|
const { communitySearchOptions } = this.state;
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
searchCommunitiesLoading: true,
|
searchCommunitiesLoading: true,
|
||||||
});
|
});
|
||||||
|
|
22
src/shared/utils/helpers/dedup-by-property.ts
Normal file
22
src/shared/utils/helpers/dedup-by-property.ts
Normal file
|
@ -0,0 +1,22 @@
|
||||||
|
function dedupByProperty<T, R extends number | string | boolean>(
|
||||||
|
collection: T[],
|
||||||
|
keyFn: (obj: T) => R,
|
||||||
|
) {
|
||||||
|
return collection.reduce(
|
||||||
|
(acc, cur) => {
|
||||||
|
const key = keyFn(cur);
|
||||||
|
if (!acc.foundSet.has(key)) {
|
||||||
|
acc.output.push(cur);
|
||||||
|
acc.foundSet.add(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
},
|
||||||
|
{
|
||||||
|
output: [] as T[],
|
||||||
|
foundSet: new Set<R>(),
|
||||||
|
},
|
||||||
|
).output;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default dedupByProperty;
|
|
@ -22,6 +22,7 @@ import validEmail from "./valid-email";
|
||||||
import validInstanceTLD from "./valid-instance-tld";
|
import validInstanceTLD from "./valid-instance-tld";
|
||||||
import validTitle from "./valid-title";
|
import validTitle from "./valid-title";
|
||||||
import validURL from "./valid-url";
|
import validURL from "./valid-url";
|
||||||
|
import dedupByProperty from "./dedup-by-property";
|
||||||
|
|
||||||
export {
|
export {
|
||||||
capitalizeFirstLetter,
|
capitalizeFirstLetter,
|
||||||
|
@ -48,4 +49,5 @@ export {
|
||||||
validInstanceTLD,
|
validInstanceTLD,
|
||||||
validTitle,
|
validTitle,
|
||||||
validURL,
|
validURL,
|
||||||
|
dedupByProperty,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue