mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-11-29 07:41:13 +00:00
Search page select fix (#2201)
* Fix search page community searchable select * Fix bug with search page creator select * Add stricter typing to dedup function * Fix modlog searchable selects
This commit is contained in:
parent
c22358e0d2
commit
acfcd86b9b
5 changed files with 204 additions and 165 deletions
|
@ -1 +1 @@
|
|||
Subproject commit 6fbc86932a03c4d40829ee4a3395259b2a7660e5
|
||||
Subproject commit 6b373bf7665ed58a81d8285009ad147248acfd7c
|
|
@ -109,6 +109,7 @@ interface ModlogState {
|
|||
loadingUserSearch: boolean;
|
||||
modSearchOptions: Choice[];
|
||||
userSearchOptions: Choice[];
|
||||
isIsomorphic: boolean;
|
||||
}
|
||||
|
||||
interface ModlogProps {
|
||||
|
@ -617,27 +618,15 @@ async function createNewOptions({
|
|||
oldOptions: Choice[];
|
||||
text: string;
|
||||
}) {
|
||||
const newOptions: Choice[] = [];
|
||||
|
||||
if (id) {
|
||||
const selectedUser = oldOptions.find(
|
||||
({ value }) => value === id.toString(),
|
||||
);
|
||||
|
||||
if (selectedUser) {
|
||||
newOptions.push(selectedUser);
|
||||
}
|
||||
}
|
||||
|
||||
if (text.length > 0) {
|
||||
newOptions.push(
|
||||
...(await fetchUsers(text))
|
||||
.slice(0, Number(fetchLimit))
|
||||
.map<Choice>(personToChoice),
|
||||
return oldOptions
|
||||
.filter(choice => parseInt(choice.value, 10) === id)
|
||||
.concat(
|
||||
(await fetchUsers(text)).slice(0, fetchLimit).map(personToChoice),
|
||||
);
|
||||
} else {
|
||||
return oldOptions;
|
||||
}
|
||||
|
||||
return newOptions;
|
||||
}
|
||||
|
||||
export class Modlog extends Component<
|
||||
|
@ -653,6 +642,7 @@ export class Modlog extends Component<
|
|||
loadingUserSearch: false,
|
||||
userSearchOptions: [],
|
||||
modSearchOptions: [],
|
||||
isIsomorphic: false,
|
||||
};
|
||||
|
||||
constructor(
|
||||
|
@ -673,6 +663,7 @@ export class Modlog extends Component<
|
|||
...this.state,
|
||||
res,
|
||||
communityRes,
|
||||
isIsomorphic: true,
|
||||
};
|
||||
|
||||
if (modUserResponse.state === "success") {
|
||||
|
@ -692,7 +683,40 @@ export class Modlog extends Component<
|
|||
}
|
||||
|
||||
async componentDidMount() {
|
||||
await this.refetch();
|
||||
if (!this.state.isIsomorphic) {
|
||||
const { modId, userId } = getModlogQueryParams();
|
||||
const promises = [this.refetch()];
|
||||
|
||||
if (userId) {
|
||||
promises.push(
|
||||
HttpService.client
|
||||
.getPersonDetails({ person_id: userId })
|
||||
.then(res => {
|
||||
if (res.state === "success") {
|
||||
this.setState({
|
||||
userSearchOptions: [personToChoice(res.data.person_view)],
|
||||
});
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
if (modId) {
|
||||
promises.push(
|
||||
HttpService.client
|
||||
.getPersonDetails({ person_id: modId })
|
||||
.then(res => {
|
||||
if (res.state === "success") {
|
||||
this.setState({
|
||||
modSearchOptions: [personToChoice(res.data.person_view)],
|
||||
});
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
}
|
||||
}
|
||||
|
||||
get combined() {
|
||||
|
|
|
@ -15,6 +15,7 @@ import { restoreScrollPosition, saveScrollPosition } from "@utils/browser";
|
|||
import {
|
||||
capitalizeFirstLetter,
|
||||
debounce,
|
||||
dedupByProperty,
|
||||
getIdFromString,
|
||||
getPageFromString,
|
||||
getQueryParams,
|
||||
|
@ -33,7 +34,6 @@ import {
|
|||
GetPersonDetails,
|
||||
GetPersonDetailsResponse,
|
||||
GetSiteResponse,
|
||||
ListCommunities,
|
||||
ListCommunitiesResponse,
|
||||
ListingType,
|
||||
PersonView,
|
||||
|
@ -88,9 +88,6 @@ type FilterType = "creator" | "community";
|
|||
interface SearchState {
|
||||
searchRes: RequestState<SearchResponse>;
|
||||
resolveObjectRes: RequestState<ResolveObjectResponse>;
|
||||
creatorDetailsRes: RequestState<GetPersonDetailsResponse>;
|
||||
communitiesRes: RequestState<ListCommunitiesResponse>;
|
||||
communityRes: RequestState<GetCommunityResponse>;
|
||||
siteRes: GetSiteResponse;
|
||||
searchText?: string;
|
||||
communitySearchOptions: Choice[];
|
||||
|
@ -197,7 +194,7 @@ const Filter = ({
|
|||
label: I18NextService.i18n.t("all"),
|
||||
value: "0",
|
||||
},
|
||||
].concat(options)}
|
||||
].concat(dedupByProperty(options, option => option.value))}
|
||||
value={value ?? 0}
|
||||
onSearch={onSearch}
|
||||
onChange={onChange}
|
||||
|
@ -245,9 +242,6 @@ export class Search extends Component<any, SearchState> {
|
|||
|
||||
state: SearchState = {
|
||||
resolveObjectRes: EMPTY_REQUEST,
|
||||
creatorDetailsRes: EMPTY_REQUEST,
|
||||
communitiesRes: EMPTY_REQUEST,
|
||||
communityRes: EMPTY_REQUEST,
|
||||
siteRes: this.isoData.site_res,
|
||||
creatorSearchOptions: [],
|
||||
communitySearchOptions: [],
|
||||
|
@ -269,10 +263,7 @@ export class Search extends Component<any, SearchState> {
|
|||
|
||||
const { q } = getSearchQueryParams();
|
||||
|
||||
this.state = {
|
||||
...this.state,
|
||||
searchText: q,
|
||||
};
|
||||
this.state.searchText = q;
|
||||
|
||||
// Only fetch the data if coming from another route
|
||||
if (FirstLoadService.isFirstLoad) {
|
||||
|
@ -284,79 +275,108 @@ export class Search extends Component<any, SearchState> {
|
|||
searchResponse: searchRes,
|
||||
} = this.isoData.routeData;
|
||||
|
||||
this.state = {
|
||||
...this.state,
|
||||
isIsomorphic: true,
|
||||
};
|
||||
this.state.isIsomorphic = true;
|
||||
|
||||
if (creatorDetailsRes?.state === "success") {
|
||||
this.state = {
|
||||
...this.state,
|
||||
creatorSearchOptions:
|
||||
creatorDetailsRes?.state === "success"
|
||||
this.state.creatorSearchOptions =
|
||||
creatorDetailsRes.state === "success"
|
||||
? [personToChoice(creatorDetailsRes.data.person_view)]
|
||||
: [],
|
||||
creatorDetailsRes,
|
||||
};
|
||||
: [];
|
||||
}
|
||||
|
||||
if (communitiesRes?.state === "success") {
|
||||
this.state = {
|
||||
...this.state,
|
||||
communitiesRes,
|
||||
};
|
||||
this.state.communitySearchOptions =
|
||||
communitiesRes.data.communities.map(communityToChoice);
|
||||
}
|
||||
|
||||
if (communityRes?.state === "success") {
|
||||
this.state = {
|
||||
...this.state,
|
||||
communityRes,
|
||||
};
|
||||
this.state.communitySearchOptions.unshift(
|
||||
communityToChoice(communityRes.data.community_view),
|
||||
);
|
||||
}
|
||||
|
||||
if (q !== "") {
|
||||
this.state = {
|
||||
...this.state,
|
||||
};
|
||||
|
||||
if (searchRes?.state === "success") {
|
||||
this.state = {
|
||||
...this.state,
|
||||
searchRes,
|
||||
};
|
||||
this.state.searchRes = searchRes;
|
||||
}
|
||||
|
||||
if (resolveObjectRes?.state === "success") {
|
||||
this.state = {
|
||||
...this.state,
|
||||
resolveObjectRes,
|
||||
};
|
||||
}
|
||||
this.state.resolveObjectRes = resolveObjectRes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async componentDidMount() {
|
||||
if (!this.state.isIsomorphic) {
|
||||
const promises = [this.fetchCommunities()];
|
||||
this.setState({
|
||||
searchCommunitiesLoading: true,
|
||||
searchCreatorLoading: true,
|
||||
});
|
||||
|
||||
const promises = [
|
||||
HttpService.client
|
||||
.listCommunities({
|
||||
type_: defaultListingType,
|
||||
sort: defaultSortType,
|
||||
limit: fetchLimit,
|
||||
})
|
||||
.then(res => {
|
||||
if (res.state === "success") {
|
||||
this.setState({
|
||||
communitySearchOptions:
|
||||
res.data.communities.map(communityToChoice),
|
||||
});
|
||||
}
|
||||
}),
|
||||
];
|
||||
|
||||
const { communityId, creatorId } = getSearchQueryParams();
|
||||
|
||||
if (communityId) {
|
||||
promises.push(
|
||||
HttpService.client.getCommunity({ id: communityId }).then(res => {
|
||||
if (res.state === "success") {
|
||||
this.setState(prev => {
|
||||
prev.communitySearchOptions.unshift(
|
||||
communityToChoice(res.data.community_view),
|
||||
);
|
||||
|
||||
return prev;
|
||||
});
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
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) {
|
||||
promises.push(this.search());
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
}
|
||||
}
|
||||
|
||||
async fetchCommunities() {
|
||||
this.setState({ communitiesRes: LOADING_REQUEST });
|
||||
this.setState({
|
||||
communitiesRes: await HttpService.client.listCommunities({
|
||||
type_: defaultListingType,
|
||||
sort: defaultSortType,
|
||||
limit: fetchLimit,
|
||||
}),
|
||||
searchCommunitiesLoading: false,
|
||||
searchCreatorLoading: false,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
saveScrollPosition(this.context);
|
||||
|
@ -368,25 +388,19 @@ export class Search extends Component<any, SearchState> {
|
|||
}: InitialFetchRequest<QueryParams<SearchProps>>): Promise<SearchData> {
|
||||
const community_id = getIdFromString(communityId);
|
||||
let communityResponse: RequestState<GetCommunityResponse> = EMPTY_REQUEST;
|
||||
let listCommunitiesResponse: RequestState<ListCommunitiesResponse> =
|
||||
EMPTY_REQUEST;
|
||||
if (community_id) {
|
||||
const getCommunityForm: GetCommunity = {
|
||||
id: community_id,
|
||||
};
|
||||
|
||||
communityResponse = await client.getCommunity(getCommunityForm);
|
||||
} else {
|
||||
const listCommunitiesForm: ListCommunities = {
|
||||
}
|
||||
|
||||
const listCommunitiesResponse = await client.listCommunities({
|
||||
type_: defaultListingType,
|
||||
sort: defaultSortType,
|
||||
limit: fetchLimit,
|
||||
};
|
||||
|
||||
listCommunitiesResponse = await client.listCommunities(
|
||||
listCommunitiesForm,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
const creator_id = getIdFromString(creatorId);
|
||||
let creatorDetailsResponse: RequestState<GetPersonDetailsResponse> =
|
||||
|
@ -417,7 +431,6 @@ export class Search extends Component<any, SearchState> {
|
|||
limit: fetchLimit,
|
||||
};
|
||||
|
||||
if (query !== "") {
|
||||
searchResponse = await client.search(form);
|
||||
if (myAuth()) {
|
||||
const resolveObjectForm: ResolveObject = {
|
||||
|
@ -434,7 +447,6 @@ export class Search extends Component<any, SearchState> {
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
communityResponse,
|
||||
|
@ -541,13 +553,8 @@ export class Search extends Component<any, SearchState> {
|
|||
creatorSearchOptions,
|
||||
searchCommunitiesLoading,
|
||||
searchCreatorLoading,
|
||||
communitiesRes,
|
||||
} = this.state;
|
||||
|
||||
const hasCommunities =
|
||||
communitiesRes.state === "success" &&
|
||||
communitiesRes.data.communities.length > 0;
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="row row-cols-auto g-2 g-sm-3 mb-2 mb-sm-3">
|
||||
|
@ -588,7 +595,6 @@ export class Search extends Component<any, SearchState> {
|
|||
</div>
|
||||
</div>
|
||||
<div className="row gy-2 gx-4 mb-3">
|
||||
{hasCommunities && (
|
||||
<Filter
|
||||
filterType="community"
|
||||
onChange={this.handleCommunityFilterChange}
|
||||
|
@ -597,7 +603,6 @@ export class Search extends Component<any, SearchState> {
|
|||
value={communityId}
|
||||
loading={searchCommunitiesLoading}
|
||||
/>
|
||||
)}
|
||||
<Filter
|
||||
filterType="creator"
|
||||
onChange={this.handleCreatorFilterChange}
|
||||
|
@ -976,55 +981,41 @@ export class Search extends Component<any, SearchState> {
|
|||
}
|
||||
|
||||
handleCreatorSearch = debounce(async (text: string) => {
|
||||
if (text.length > 0) {
|
||||
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) {
|
||||
newOptions.push(...(await fetchUsers(text)).map(personToChoice));
|
||||
}
|
||||
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) => {
|
||||
if (text.length > 0) {
|
||||
const { communityId } = getSearchQueryParams();
|
||||
const { communitySearchOptions } = this.state;
|
||||
|
||||
this.setState({
|
||||
searchCommunitiesLoading: true,
|
||||
});
|
||||
|
||||
const newOptions: Choice[] = [];
|
||||
|
||||
const selectedChoice = communitySearchOptions.find(
|
||||
choice => getIdFromString(choice.value) === communityId,
|
||||
);
|
||||
|
||||
if (selectedChoice) {
|
||||
newOptions.push(selectedChoice);
|
||||
}
|
||||
|
||||
if (text.length > 0) {
|
||||
newOptions.push(...(await fetchCommunities(text)).map(communityToChoice));
|
||||
}
|
||||
const newOptions = communitySearchOptions
|
||||
.filter(choice => getIdFromString(choice.value) === communityId)
|
||||
.concat((await fetchCommunities(text)).map(communityToChoice));
|
||||
|
||||
this.setState({
|
||||
searchCommunitiesLoading: false,
|
||||
communitySearchOptions: newOptions,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
handleSortChange(sort: SortType) {
|
||||
|
|
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 extends Record<string, any>,
|
||||
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 validTitle from "./valid-title";
|
||||
import validURL from "./valid-url";
|
||||
import dedupByProperty from "./dedup-by-property";
|
||||
|
||||
export {
|
||||
capitalizeFirstLetter,
|
||||
|
@ -48,4 +49,5 @@ export {
|
|||
validInstanceTLD,
|
||||
validTitle,
|
||||
validURL,
|
||||
dedupByProperty,
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue