-
Notifications
You must be signed in to change notification settings - Fork 331
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix search page to stop couldnt_find_object
error
#1669
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -332,9 +332,7 @@ export class Search extends Component<any, SearchState> { | |
} | ||
|
||
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()); | ||
|
@@ -432,7 +430,15 @@ export class Search extends Component<any, SearchState> { | |
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" }; | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -950,7 +956,7 @@ export class Search extends Component<any, SearchState> { | |
if (auth) { | ||
this.setState({ resolveObjectRes: { state: "loading" } }); | ||
this.setState({ | ||
resolveObjectRes: await HttpService.client.resolveObject({ | ||
resolveObjectRes: await HttpService.silent_client.resolveObject({ | ||
q, | ||
auth, | ||
}), | ||
|
@@ -1097,10 +1103,6 @@ export class Search extends Component<any, SearchState> { | |
sort: sort ?? urlSort, | ||
}; | ||
|
||
this.props.history.push(`/search${getQueryString(queryParams)}`, { | ||
searched: true, | ||
}); | ||
|
||
await this.search(); | ||
this.props.history.push(`/search${getQueryString(queryParams)}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we create a global |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
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"; | ||
}; | ||
|
||
|
@@ -45,7 +45,7 @@ export type WrappedLemmyHttp = { | |
class WrappedLemmyHttpClient { | ||
#client: LemmyHttp; | ||
|
||
constructor(client: LemmyHttp) { | ||
constructor(client: LemmyHttp, silent = false) { | ||
this.#client = client; | ||
|
||
for (const key of Object.getOwnPropertyNames( | ||
|
@@ -61,8 +61,10 @@ class WrappedLemmyHttpClient { | |
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, | ||
|
@@ -74,16 +76,23 @@ class WrappedLemmyHttpClient { | |
} | ||
} | ||
|
||
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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a nicer way to achieve this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't think of any. |
||
#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() { | ||
|
@@ -93,4 +102,8 @@ export class HttpService { | |
public static get client() { | ||
return this.#Instance.#client; | ||
} | ||
|
||
public static get silent_client() { | ||
return this.#Instance.#silent_client; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed this because it was causing no search results to display.