]> Untitled Git - lemmy-ui.git/commitdiff
Merge branch 'main' into route-data-refactor
authorabias <abias1122@gmail.com>
Tue, 30 May 2023 00:41:54 +0000 (20:41 -0400)
committerabias <abias1122@gmail.com>
Tue, 30 May 2023 00:41:54 +0000 (20:41 -0400)
1  2 
src/shared/components/post/create-post.tsx

index f43cbb0739761e25a7043f9df0e4c19307364e1f,28418d10ff9d7492c02632e5716810a86e8d19da..684dda2fc3b1a194a8bf67c1e9abc7c62be4dea3
@@@ -16,12 -16,10 +16,11 @@@ import { WebSocketService } from "../..
  import {
    Choice,
    QueryParams,
 +  WithPromiseKeys,
    enableDownvotes,
    enableNsfw,
    getIdFromString,
    getQueryParams,
-   getQueryString,
    isBrowser,
    myAuth,
    setIsoData,
@@@ -37,10 -35,6 +36,10 @@@ export interface CreatePostProps 
    communityId?: number;
  }
  
 +interface CreatePostData {
 +  communityResponse?: GetCommunityResponse;
 +}
 +
  function getCreatePostQueryParams() {
    return getQueryParams<CreatePostProps>({
      communityId: getIdFromString,
@@@ -57,7 -51,7 +56,7 @@@ export class CreatePost extends Compone
    RouteComponentProps<Record<string, never>>,
    CreatePostState
  > {
 -  private isoData = setIsoData(this.context);
 +  private isoData = setIsoData<CreatePostData>(this.context);
    private subscription?: Subscription;
    state: CreatePostState = {
      siteRes: this.isoData.site_res,
  
      // Only fetch the data if coming from another route
      if (this.isoData.path === this.context.router.route.match.url) {
 -      const communityRes = this.isoData.routeData[0] as
 -        | GetCommunityResponse
 -        | undefined;
 +      const { communityResponse } = this.isoData.routeData;
  
 -      if (communityRes) {
 +      if (communityResponse) {
          const communityChoice: Choice = {
-           label: communityResponse.community_view.community.name,
 -          label: communityRes.community_view.community.title,
 -          value: communityRes.community_view.community.id.toString(),
++          label: communityResponse.community_view.community.title,
 +          value: communityResponse.community_view.community.id.toString(),
          };
  
          this.state = {
    updateUrl({ communityId }: Partial<CreatePostProps>) {
      const { communityId: urlCommunityId } = getCreatePostQueryParams();
  
-     const queryParams: QueryParams<CreatePostProps> = {
-       communityId: (communityId ?? urlCommunityId)?.toString(),
-     };
      const locationState = this.props.history.location.state as
        | PostFormParams
        | undefined;
  
-     this.props.history.replace(
-       `/create_post${getQueryString(queryParams)}`,
-       locationState
-     );
+     const url = new URL(location.href);
+     const newId = (communityId ?? urlCommunityId)?.toString();
+     if (newId !== undefined) {
+       url.searchParams.set("communityId", newId);
+     } else {
+       url.searchParams.delete("communityId");
+     }
+     history.replaceState(locationState, "", url);
  
      this.fetchCommunity();
    }
      client,
      query: { communityId },
      auth,
 -  }: InitialFetchRequest<QueryParams<CreatePostProps>>): Promise<any>[] {
 -    const promises: Promise<any>[] = [];
 +  }: InitialFetchRequest<
 +    QueryParams<CreatePostProps>
 +  >): WithPromiseKeys<CreatePostData> {
 +    const data: WithPromiseKeys<CreatePostData> = {};
  
      if (communityId) {
        const form: GetCommunity = {
          id: getIdFromString(communityId),
        };
  
 -      promises.push(client.getCommunity(form));
 -    } else {
 -      promises.push(Promise.resolve());
 +      data.communityResponse = client.getCommunity(form);
      }
  
 -    return promises;
 +    return data;
    }
  
    parseMessage(msg: any) {
      if (op === UserOperation.GetCommunity) {
        const {
          community_view: {
-           community: { name, id },
+           community: { title, id },
          },
        } = wsJsonToRes<GetCommunityResponse>(msg);
  
        this.setState({
-         selectedCommunityChoice: { label: name, value: id.toString() },
+         selectedCommunityChoice: { label: title, value: id.toString() },
          loading: false,
        });
      }