}
async componentDidMount() {
- if (
- !(this.state.isIsomorphic || this.props.history.location.state?.searched)
- ) {
+ if (!this.state.isIsomorphic) {
const promises = [this.fetchCommunities()];
if (this.state.searchText) {
promises.push(this.search());
q: query,
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" };
+ }
}
}
}
if (auth) {
this.setState({ resolveObjectRes: { state: "loading" } });
this.setState({
- resolveObjectRes: await HttpService.client.resolveObject({
+ resolveObjectRes: await HttpService.silent_client.resolveObject({
q,
auth,
}),
sort: sort ?? urlSort,
};
- this.props.history.push(`/search${getQueryString(queryParams)}`, {
- searched: true,
- });
-
- await this.search();
+ this.props.history.push(`/search${getQueryString(queryParams)}`);
}
}
import { getHttpBase } from "@utils/env";
import { LemmyHttp } from "lemmy-js-client";
-import { toast } from "../../shared/toast";
+import { toast } from "../toast";
import { I18NextService } from "./I18NextService";
-type EmptyRequestState = {
+export type EmptyRequestState = {
state: "empty";
};
class WrappedLemmyHttpClient {
#client: LemmyHttp;
- constructor(client: LemmyHttp) {
+ constructor(client: LemmyHttp, silent = false) {
this.#client = client;
for (const key of Object.getOwnPropertyNames(
state: !(res === undefined || res === null) ? "success" : "empty",
};
} catch (error) {
- console.error(`API error: ${error}`);
- toast(I18NextService.i18n.t(error), "danger");
+ if (!silent) {
+ console.error(`API error: ${error}`);
+ toast(I18NextService.i18n.t(error), "danger");
+ }
return {
state: "failed",
msg: error,
}
}
-export function wrapClient(client: LemmyHttp) {
- return new WrappedLemmyHttpClient(client) as unknown as WrappedLemmyHttp; // unfortunately, this verbose cast is necessary
+export function wrapClient(client: LemmyHttp, silent = false) {
+ // unfortunately, this verbose cast is necessary
+ return new WrappedLemmyHttpClient(
+ client,
+ silent
+ ) as unknown as WrappedLemmyHttp;
}
export class HttpService {
static #_instance: HttpService;
+ #silent_client: WrappedLemmyHttp;
#client: WrappedLemmyHttp;
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() {
public static get client() {
return this.#Instance.#client;
}
+
+ public static get silent_client() {
+ return this.#Instance.#silent_client;
+ }
}