]> Untitled Git - lemmy.git/commitdiff
Auto-select current community if creating a post from the community
authorDessalines <tyhou13@gmx.com>
Mon, 29 Apr 2019 16:07:41 +0000 (09:07 -0700)
committerDessalines <tyhou13@gmx.com>
Mon, 29 Apr 2019 16:07:41 +0000 (09:07 -0700)
page.

Fixes #133

ui/src/components/create-post.tsx
ui/src/components/navbar.tsx
ui/src/components/post-form.tsx

index e2998ca7e914238c95b3e8347b27978b195590cb..1958be72d2e1cb9004ab985959f9a1f92dac36e2 100644 (file)
@@ -18,13 +18,23 @@ export class CreatePost extends Component<any, any> {
         <div class="row">
           <div class="col-12 col-lg-6 mb-4">
             <h5>Create a Post</h5>
-            <PostForm onCreate={this.handlePostCreate}/>
+            <PostForm onCreate={this.handlePostCreate} prevCommunityName={this.prevCommunityName} />
           </div>
         </div>
       </div>
     )
   }
 
+  get prevCommunityName(): string {
+    if (this.props.location.state) {
+      let lastLocation = this.props.location.state.prevPath;
+      if (lastLocation.includes("/c/")) {
+        return lastLocation.split("/c/")[1];
+      }    
+    }
+    return undefined;
+  }
+
   handlePostCreate(id: number) {
     this.props.history.push(`/post/${id}`);
   }
index 31dab61b6489f11242c89e098547cc6a69153e6e..ee19e5c5b92182780d2958aeff3e500e90b35e16 100644 (file)
@@ -79,7 +79,7 @@ export class Navbar extends Component<any, NavbarState> {
               <Link class="nav-link" to="/search">Search</Link>
             </li>
             <li class="nav-item">
-              <Link class="nav-link" to="/create_post">Create Post</Link>
+              <Link class="nav-link" to={{pathname: '/create_post', state: { prevPath: this.currentLocation }}}>Create Post</Link>
             </li>
             <li class="nav-item">
               <Link class="nav-link" to="/create_community">Create Community</Link>
@@ -165,6 +165,10 @@ export class Navbar extends Component<any, NavbarState> {
     }
   }
 
+  get currentLocation() {
+    return this.context.router.history.location.pathname;
+  }
+
   sendRepliesCount(res: GetRepliesResponse) {
     UserService.Instance.sub.next({user: UserService.Instance.user, unreadCount: res.replies.filter(r => !r.read).length});
   }
index ab93628299614d6bd4ecd560aada035446ab456b..e4e75df458663cb34756b85b30c58936ce042015 100644 (file)
@@ -8,6 +8,7 @@ import * as autosize from 'autosize';
 
 interface PostFormProps {
   post?: Post; // If a post is given, that means this is an edit
+  prevCommunityName?: string;
   onCancel?(): any;
   onCreate?(id: number): any;
   onEdit?(post: Post): any;
@@ -170,6 +171,9 @@ export class PostForm extends Component<PostFormProps, PostFormState> {
       this.state.communities = res.communities;
       if (this.props.post) {
         this.state.postForm.community_id = this.props.post.community_id;
+      } else if (this.props.prevCommunityName) {
+        let foundCommunityId = res.communities.find(r => r.name == this.props.prevCommunityName).id;
+        this.state.postForm.community_id = foundCommunityId;
       } else {
         this.state.postForm.community_id = res.communities[0].id;
       }