Fix search page to stop couldnt_find_object error (#1669)

* Add silent client and use it for resolve object

* more changes

* Fix more issues

* make double equals a triple equals
This commit is contained in:
Scott 2023-06-29 12:14:47 +09:30 committed by GitHub
parent 38a109ba92
commit a077924f38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 18 deletions

View file

@ -332,9 +332,7 @@ export class Search extends Component<any, SearchState> {
} }
async componentDidMount() { async componentDidMount() {
if ( if (!this.state.isIsomorphic) {
!(this.state.isIsomorphic || this.props.history.location.state?.searched)
) {
const promises = [this.fetchCommunities()]; const promises = [this.fetchCommunities()];
if (this.state.searchText) { if (this.state.searchText) {
promises.push(this.search()); promises.push(this.search());
@ -432,7 +430,15 @@ export class Search extends Component<any, SearchState> {
q: query, q: query,
auth, auth,
}; };
resolveObjectResponse = await client.resolveObject(resolveObjectForm); resolveObjectResponse = await HttpService.silent_client.resolveObject(
resolveObjectForm
);
// 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.
if (resolveObjectResponse.state === "failed") {
resolveObjectResponse = { state: "empty" };
}
} }
} }
} }
@ -950,7 +956,7 @@ export class Search extends Component<any, SearchState> {
if (auth) { if (auth) {
this.setState({ resolveObjectRes: { state: "loading" } }); this.setState({ resolveObjectRes: { state: "loading" } });
this.setState({ this.setState({
resolveObjectRes: await HttpService.client.resolveObject({ resolveObjectRes: await HttpService.silent_client.resolveObject({
q, q,
auth, auth,
}), }),
@ -1097,10 +1103,6 @@ export class Search extends Component<any, SearchState> {
sort: sort ?? urlSort, sort: sort ?? urlSort,
}; };
this.props.history.push(`/search${getQueryString(queryParams)}`, { this.props.history.push(`/search${getQueryString(queryParams)}`);
searched: true,
});
await this.search();
} }
} }

View file

@ -1,9 +1,9 @@
import { getHttpBase } from "@utils/env"; import { getHttpBase } from "@utils/env";
import { LemmyHttp } from "lemmy-js-client"; import { LemmyHttp } from "lemmy-js-client";
import { toast } from "../../shared/toast"; import { toast } from "../toast";
import { I18NextService } from "./I18NextService"; import { I18NextService } from "./I18NextService";
type EmptyRequestState = { export type EmptyRequestState = {
state: "empty"; state: "empty";
}; };
@ -45,7 +45,7 @@ export type WrappedLemmyHttp = {
class WrappedLemmyHttpClient { class WrappedLemmyHttpClient {
#client: LemmyHttp; #client: LemmyHttp;
constructor(client: LemmyHttp) { constructor(client: LemmyHttp, silent = false) {
this.#client = client; this.#client = client;
for (const key of Object.getOwnPropertyNames( for (const key of Object.getOwnPropertyNames(
@ -61,8 +61,10 @@ class WrappedLemmyHttpClient {
state: !(res === undefined || res === null) ? "success" : "empty", state: !(res === undefined || res === null) ? "success" : "empty",
}; };
} catch (error) { } catch (error) {
console.error(`API error: ${error}`); if (!silent) {
toast(I18NextService.i18n.t(error), "danger"); console.error(`API error: ${error}`);
toast(I18NextService.i18n.t(error), "danger");
}
return { return {
state: "failed", state: "failed",
msg: error, msg: error,
@ -74,16 +76,23 @@ class WrappedLemmyHttpClient {
} }
} }
export function wrapClient(client: LemmyHttp) { export function wrapClient(client: LemmyHttp, silent = false) {
return new WrappedLemmyHttpClient(client) as unknown as WrappedLemmyHttp; // unfortunately, this verbose cast is necessary // unfortunately, this verbose cast is necessary
return new WrappedLemmyHttpClient(
client,
silent
) as unknown as WrappedLemmyHttp;
} }
export class HttpService { export class HttpService {
static #_instance: HttpService; static #_instance: HttpService;
#silent_client: WrappedLemmyHttp;
#client: WrappedLemmyHttp; #client: WrappedLemmyHttp;
private constructor() { private constructor() {
this.#client = wrapClient(new LemmyHttp(getHttpBase())); const lemmyHttp = new LemmyHttp(getHttpBase());
this.#client = wrapClient(lemmyHttp);
this.#silent_client = wrapClient(lemmyHttp, true);
} }
static get #Instance() { static get #Instance() {
@ -93,4 +102,8 @@ export class HttpService {
public static get client() { public static get client() {
return this.#Instance.#client; return this.#Instance.#client;
} }
public static get silent_client() {
return this.#Instance.#silent_client;
}
} }