Fix modlog searchable selects

This commit is contained in:
SleeplessOne1917 2023-10-27 13:48:37 -04:00
parent 548f00d59b
commit a41d782648
2 changed files with 45 additions and 21 deletions

View file

@ -109,6 +109,7 @@ interface ModlogState {
loadingUserSearch: boolean; loadingUserSearch: boolean;
modSearchOptions: Choice[]; modSearchOptions: Choice[];
userSearchOptions: Choice[]; userSearchOptions: Choice[];
isIsomorphic: boolean;
} }
interface ModlogProps { interface ModlogProps {
@ -617,27 +618,15 @@ async function createNewOptions({
oldOptions: Choice[]; oldOptions: Choice[];
text: string; text: string;
}) { }) {
const newOptions: Choice[] = [];
if (id) {
const selectedUser = oldOptions.find(
({ value }) => value === id.toString(),
);
if (selectedUser) {
newOptions.push(selectedUser);
}
}
if (text.length > 0) { if (text.length > 0) {
newOptions.push( return oldOptions
...(await fetchUsers(text)) .filter(choice => parseInt(choice.value, 10) === id)
.slice(0, Number(fetchLimit)) .concat(
.map<Choice>(personToChoice), (await fetchUsers(text)).slice(0, fetchLimit).map(personToChoice),
); );
} else {
return oldOptions;
} }
return newOptions;
} }
export class Modlog extends Component< export class Modlog extends Component<
@ -653,6 +642,7 @@ export class Modlog extends Component<
loadingUserSearch: false, loadingUserSearch: false,
userSearchOptions: [], userSearchOptions: [],
modSearchOptions: [], modSearchOptions: [],
isIsomorphic: false,
}; };
constructor( constructor(
@ -673,6 +663,7 @@ export class Modlog extends Component<
...this.state, ...this.state,
res, res,
communityRes, communityRes,
isIsomorphic: true,
}; };
if (modUserResponse.state === "success") { if (modUserResponse.state === "success") {
@ -692,7 +683,40 @@ export class Modlog extends Component<
} }
async componentDidMount() { 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() { get combined() {

View file

@ -312,7 +312,7 @@ export class Search extends Component<any, SearchState> {
searchCreatorLoading: true, searchCreatorLoading: true,
}); });
const promises: Promise<any>[] = [ const promises = [
HttpService.client HttpService.client
.listCommunities({ .listCommunities({
type_: defaultListingType, type_: defaultListingType,