]> Untitled Git - lemmy.git/blob - ui/src/components/sidebar.tsx
Merge branch 'dev'
[lemmy.git] / ui / src / components / sidebar.tsx
1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Community, CommunityUser, FollowCommunityForm, CommunityForm as CommunityFormI, UserView } from '../interfaces';
4 import { WebSocketService, UserService } from '../services';
5 import { mdToHtml, getUnixTime } from '../utils';
6 import { CommunityForm } from './community-form';
7
8 interface SidebarProps {
9   community: Community;
10   moderators: Array<CommunityUser>;
11   admins: Array<UserView>;
12 }
13
14 interface SidebarState {
15   showEdit: boolean;
16   showRemoveDialog: boolean;
17   removeReason: string;
18   removeExpires: string;
19 }
20
21 export class Sidebar extends Component<SidebarProps, SidebarState> {
22
23   private emptyState: SidebarState = {
24     showEdit: false,
25     showRemoveDialog: false,
26     removeReason: null,
27     removeExpires: null
28   }
29
30   constructor(props: any, context: any) {
31     super(props, context);
32     this.state = this.emptyState;
33     this.handleEditCommunity = this.handleEditCommunity.bind(this);
34     this.handleEditCancel = this.handleEditCancel.bind(this);
35   }
36
37   render() {
38     return (
39       <div>
40         {!this.state.showEdit 
41           ? this.sidebar()
42           : <CommunityForm 
43           community={this.props.community} 
44           onEdit={this.handleEditCommunity} 
45           onCancel={this.handleEditCancel} />
46         }
47       </div>
48     )
49   }
50
51   sidebar() {
52     let community = this.props.community;
53     return (
54       <div>
55         <h5 className="mb-0">{community.title}
56         {community.removed &&
57           <small className="ml-2 text-muted font-italic">removed</small>
58         }
59         {community.deleted &&
60           <small className="ml-2 text-muted font-italic">deleted</small>
61         }
62       </h5>
63       <Link className="text-muted" to={`/c/${community.name}`}>/c/{community.name}</Link>
64       <ul class="list-inline mb-1 text-muted small font-weight-bold"> 
65         {this.canMod && 
66           <>
67             <li className="list-inline-item">
68               <span class="pointer" onClick={linkEvent(this, this.handleEditClick)}>edit</span>
69             </li>
70             {this.amCreator && 
71               <li className="list-inline-item">
72                 <span class="pointer" onClick={linkEvent(this, this.handleDeleteClick)}>
73                   {!community.deleted ? 'delete' : 'restore'}
74                 </span>
75               </li>
76             }
77           </>
78         }
79         {this.canAdmin &&
80           <li className="list-inline-item">
81             {!this.props.community.removed ? 
82             <span class="pointer" onClick={linkEvent(this, this.handleModRemoveShow)}>remove</span> :
83             <span class="pointer" onClick={linkEvent(this, this.handleModRemoveSubmit)}>restore</span>
84             }
85           </li>
86
87         }
88       </ul>
89       {this.state.showRemoveDialog && 
90         <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
91           <div class="form-group row">
92             <label class="col-form-label">Reason</label>
93             <input type="text" class="form-control mr-2" placeholder="Optional" value={this.state.removeReason} onInput={linkEvent(this, this.handleModRemoveReasonChange)} />
94           </div>
95           {/* TODO hold off on expires for now */}
96           {/* <div class="form-group row"> */}
97           {/*   <label class="col-form-label">Expires</label> */}
98           {/*   <input type="date" class="form-control mr-2" placeholder="Expires" value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
99           {/* </div> */}
100           <div class="form-group row">
101             <button type="submit" class="btn btn-secondary">Remove Community</button>
102           </div>
103         </form>
104       }
105       <ul class="my-1 list-inline">
106         <li className="list-inline-item"><Link className="badge badge-light" to="/communities">{community.category_name}</Link></li>
107         <li className="list-inline-item badge badge-light">{community.number_of_subscribers} Subscribers</li>
108         <li className="list-inline-item badge badge-light">{community.number_of_posts} Posts</li>
109         <li className="list-inline-item badge badge-light">{community.number_of_comments} Comments</li>
110         <li className="list-inline-item"><Link className="badge badge-light" to={`/modlog/community/${this.props.community.id}`}>Modlog</Link></li>
111       </ul>
112       <ul class="list-inline small"> 
113         <li class="list-inline-item">mods: </li>
114         {this.props.moderators.map(mod =>
115           <li class="list-inline-item"><Link class="text-info" to={`/u/${mod.user_name}`}>{mod.user_name}</Link></li>
116         )}
117       </ul>
118       <Link class="btn btn-sm btn-secondary btn-block mb-3" 
119         to={`/create_post/c/${community.name}`}>Create a Post</Link>
120       <div>
121         {community.subscribed 
122           ? <button class="btn btn-sm btn-secondary btn-block mb-3" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button>
123           : <button class="btn btn-sm btn-secondary btn-block mb-3" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
124         }
125       </div>
126       {community.description && 
127         <div>
128           <hr />
129           <div className="md-div" dangerouslySetInnerHTML={mdToHtml(community.description)} />
130           <hr />
131         </div>
132       }
133     </div>
134     );
135   }
136
137   handleEditClick(i: Sidebar) {
138     i.state.showEdit = true;
139     i.setState(i.state);
140   }
141
142   handleEditCommunity() {
143     this.state.showEdit = false;
144     this.setState(this.state);
145   }
146
147   handleEditCancel() {
148     this.state.showEdit = false;
149     this.setState(this.state);
150   }
151
152   handleDeleteClick(i: Sidebar) {
153     event.preventDefault();
154     let deleteForm: CommunityFormI = {
155       name: i.props.community.name,
156       title: i.props.community.title,
157       category_id: i.props.community.category_id,
158       edit_id: i.props.community.id,
159       deleted: !i.props.community.deleted,
160       auth: null,
161     };
162     WebSocketService.Instance.editCommunity(deleteForm);
163   }
164
165   handleUnsubscribe(communityId: number) {
166     let form: FollowCommunityForm = {
167       community_id: communityId,
168       follow: false
169     };
170     WebSocketService.Instance.followCommunity(form);
171   }
172
173   handleSubscribe(communityId: number) {
174     let form: FollowCommunityForm = {
175       community_id: communityId,
176       follow: true
177     };
178     WebSocketService.Instance.followCommunity(form);
179   }
180
181   private get amCreator(): boolean {
182     return this.props.community.creator_id == UserService.Instance.user.id;
183   }
184
185   get canMod(): boolean {
186     return UserService.Instance.user && this.props.moderators.map(m => m.user_id).includes(UserService.Instance.user.id);
187   }
188
189   get canAdmin(): boolean {
190     return UserService.Instance.user && this.props.admins.map(a => a.id).includes(UserService.Instance.user.id);
191   }
192
193   handleModRemoveShow(i: Sidebar) {
194     i.state.showRemoveDialog = true;
195     i.setState(i.state);
196   }
197
198   handleModRemoveReasonChange(i: Sidebar, event: any) {
199     i.state.removeReason = event.target.value;
200     i.setState(i.state);
201   }
202
203   handleModRemoveExpiresChange(i: Sidebar, event: any) {
204     console.log(event.target.value);
205     i.state.removeExpires = event.target.value;
206     i.setState(i.state);
207   }
208
209   handleModRemoveSubmit(i: Sidebar) {
210     event.preventDefault();
211     let deleteForm: CommunityFormI = {
212       name: i.props.community.name,
213       title: i.props.community.title,
214       category_id: i.props.community.category_id,
215       edit_id: i.props.community.id,
216       removed: !i.props.community.removed,
217       reason: i.state.removeReason,
218       expires: getUnixTime(i.state.removeExpires),
219       auth: null,
220     };
221     WebSocketService.Instance.editCommunity(deleteForm);
222
223     i.state.showRemoveDialog = false;
224     i.setState(i.state);
225   }
226 }