Skip to content
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

Keep CreatePost mounted when updating URL, fixes focus resets. #2520

Merged
merged 1 commit into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/shared/components/post/create-post.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
voteDisplayMode,
} from "@utils/app";
import {
bareRoutePush,
getIdFromString,
getQueryParams,
getQueryString,
Expand Down Expand Up @@ -90,6 +91,7 @@ interface CreatePostState {
selectedCommunityChoice?: Choice;
initialCommunitiesRes: RequestState<ListCommunitiesResponse>;
isIsomorphic: boolean;
resetCounter: number; // resets PostForm when changed
}

type CreatePostPathProps = Record<string, never>;
Expand All @@ -112,6 +114,7 @@ export class CreatePost extends Component<
loading: false,
initialCommunitiesRes: EMPTY_REQUEST,
isIsomorphic: false,
resetCounter: 0,
};

constructor(props: CreatePostRouteProps, context: any) {
Expand Down Expand Up @@ -194,6 +197,15 @@ export class CreatePost extends Component<
}
}

componentWillReceiveProps(nextProps: CreatePostRouteProps) {
if (bareRoutePush(this.props, nextProps)) {
this.setState(s => ({ resetCounter: s.resetCounter + 1 }));
}
if (this.props.communityId !== nextProps.communityId) {
this.fetchCommunity(nextProps);
}
}

get documentTitle(): string {
return `${I18NextService.i18n.t("create_post")} - ${
this.state.siteRes.site_view.site.name
Expand Down Expand Up @@ -237,6 +249,7 @@ export class CreatePost extends Component<
<div id="createPostForm" className="col-12 col-lg-6 offset-lg-3 mb-4">
<h1 className="h4 mb-4">{I18NextService.i18n.t("create_post")}</h1>
<PostForm
key={this.state.resetCounter}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a key (outside of a collection) to prevent uneccessary rerenders. Clever.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still has to render even when the key stays the same. But the when the key changes it has to be recreated and resets its state. I added it so that the "Create Post" link in the top navigation completely resets the form.

onCreate={this.handlePostCreate}
params={params}
enableDownvotes={enableDownvotes(siteRes)}
Expand Down
18 changes: 9 additions & 9 deletions src/shared/components/post/post-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ interface PostFormState {
previewMode: boolean;
submitted: boolean;
bypassNavWarning: boolean;
urlBlurTimeout?: NodeJS.Timeout;
}

function handlePostSubmit(i: PostForm, event: any) {
Expand Down Expand Up @@ -155,9 +154,6 @@ function copySuggestedTitle({
suggestedTitle?: string;
}) {
if (suggestedTitle) {
clearTimeout(i.state.urlBlurTimeout);
i.setState({ urlBlurTimeout: undefined });

i.setState(
s => (
(s.form.name = suggestedTitle?.substring(0, MAX_POST_TITLE_LENGTH)), s
Expand Down Expand Up @@ -192,11 +188,7 @@ function handlePostUrlChange(i: PostForm, event: any) {
}

function handlePostUrlBlur(i: PostForm, event: any) {
i.setState({
urlBlurTimeout: setTimeout(() => {
i.updateUrl(() => i.props.onUrlBlur?.(event.target.value));
}, 500),
});
i.updateUrl(() => i.props.onUrlBlur?.(event.target.value));
}

function handlePostNsfwChange(i: PostForm, event: any) {
Expand Down Expand Up @@ -418,6 +410,14 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
if (this.props.loading && !nextProps.loading) {
this.setState({ submitted: false, bypassNavWarning: false });
}
if (this.props.params !== nextProps.params && nextProps.params) {
const params = nextProps.params;
for (const k in params) {
if (this.props.params?.[k] !== params[k]) {
this.setState(s => ({ form: { ...s.form, [k]: params[k] } }));
}
}
}
}

render() {
Expand Down
1 change: 1 addition & 0 deletions src/shared/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ export const routes: IRoutePropsWithFetch<RouteData, any, any>[] = [
component: CreatePost,
fetchInitialData: CreatePost.fetchInitialData,
getQueryParams: getCreatePostQueryParams,
mountedSameRouteNavKey: "create_post",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this do?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In App routes without that property get the location.key assigned as key, so that they unmount whenever the location changes. Before that property all routes behaved like that.

} as CreatePostFetchConfig,
{
path: `/create_community`,
Expand Down