]> 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       <div>
119         {community.subscribed 
120           ? <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button>
121           : <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
122         }
123       </div>
124       {community.description && 
125         <div>
126           <hr />
127           <div className="md-div" dangerouslySetInnerHTML={mdToHtml(community.description)} />
128           <hr />
129         </div>
130       }
131     </div>
132     );
133   }
134
135   handleEditClick(i: Sidebar) {
136     i.state.showEdit = true;
137     i.setState(i.state);
138   }
139
140   handleEditCommunity() {
141     this.state.showEdit = false;
142     this.setState(this.state);
143   }
144
145   handleEditCancel() {
146     this.state.showEdit = false;
147     this.setState(this.state);
148   }
149
150   handleDeleteClick(i: Sidebar) {
151     event.preventDefault();
152     let deleteForm: CommunityFormI = {
153       name: i.props.community.name,
154       title: i.props.community.title,
155       category_id: i.props.community.category_id,
156       edit_id: i.props.community.id,
157       deleted: !i.props.community.deleted,
158       auth: null,
159     };
160     WebSocketService.Instance.editCommunity(deleteForm);
161   }
162
163   handleUnsubscribe(communityId: number) {
164     let form: FollowCommunityForm = {
165       community_id: communityId,
166       follow: false
167     };
168     WebSocketService.Instance.followCommunity(form);
169   }
170
171   handleSubscribe(communityId: number) {
172     let form: FollowCommunityForm = {
173       community_id: communityId,
174       follow: true
175     };
176     WebSocketService.Instance.followCommunity(form);
177   }
178
179   private get amCreator(): boolean {
180     return this.props.community.creator_id == UserService.Instance.user.id;
181   }
182
183   get canMod(): boolean {
184     return UserService.Instance.user && this.props.moderators.map(m => m.user_id).includes(UserService.Instance.user.id);
185   }
186
187   get canAdmin(): boolean {
188     return UserService.Instance.user && this.props.admins.map(a => a.id).includes(UserService.Instance.user.id);
189   }
190
191   handleModRemoveShow(i: Sidebar) {
192     i.state.showRemoveDialog = true;
193     i.setState(i.state);
194   }
195
196   handleModRemoveReasonChange(i: Sidebar, event: any) {
197     i.state.removeReason = event.target.value;
198     i.setState(i.state);
199   }
200
201   handleModRemoveExpiresChange(i: Sidebar, event: any) {
202     console.log(event.target.value);
203     i.state.removeExpires = event.target.value;
204     i.setState(i.state);
205   }
206
207   handleModRemoveSubmit(i: Sidebar) {
208     event.preventDefault();
209     let deleteForm: CommunityFormI = {
210       name: i.props.community.name,
211       title: i.props.community.title,
212       category_id: i.props.community.category_id,
213       edit_id: i.props.community.id,
214       removed: !i.props.community.removed,
215       reason: i.state.removeReason,
216       expires: getUnixTime(i.state.removeExpires),
217       auth: null,
218     };
219     WebSocketService.Instance.editCommunity(deleteForm);
220
221     i.state.showRemoveDialog = false;
222     i.setState(i.state);
223   }
224 }