Auto-select current community if creating a post from the community
page. Fixes #133
This commit is contained in:
parent
b0a6fefcf9
commit
d0099b4941
3 changed files with 20 additions and 2 deletions
|
@ -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}`);
|
||||
}
|
||||
|
|
|
@ -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});
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Reference in a new issue