]> Untitled Git - lemmy.git/blob - ui/src/components/sidebar.tsx
Removing expires temporarily until they can be figured out.
[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       </h5>
60       <Link className="text-muted" to={`/community/${community.id}`}>/f/{community.name}</Link>
61       <ul class="list-inline mb-1 text-muted small font-weight-bold"> 
62         {this.canMod && 
63           <>
64             <li className="list-inline-item">
65               <span class="pointer" onClick={linkEvent(this, this.handleEditClick)}>edit</span>
66             </li>
67             {this.amCreator && 
68               <li className="list-inline-item">
69                 {/* <span class="pointer" onClick={linkEvent(this, this.handleDeleteClick)}>delete</span> */}
70               </li>
71             }
72           </>
73         }
74         {this.canAdmin &&
75           <li className="list-inline-item">
76             {!this.props.community.removed ? 
77             <span class="pointer" onClick={linkEvent(this, this.handleModRemoveShow)}>remove</span> :
78             <span class="pointer" onClick={linkEvent(this, this.handleModRemoveSubmit)}>restore</span>
79             }
80           </li>
81
82         }
83       </ul>
84       {this.state.showRemoveDialog && 
85         <form onSubmit={linkEvent(this, this.handleModRemoveSubmit)}>
86           <div class="form-group row">
87             <label class="col-form-label">Reason</label>
88             <input type="text" class="form-control mr-2" placeholder="Optional" value={this.state.removeReason} onInput={linkEvent(this, this.handleModRemoveReasonChange)} />
89           </div>
90           {/* TODO hold off on expires for now */}
91           {/* <div class="form-group row"> */}
92           {/*   <label class="col-form-label">Expires</label> */}
93           {/*   <input type="date" class="form-control mr-2" placeholder="Expires" value={this.state.removeExpires} onInput={linkEvent(this, this.handleModRemoveExpiresChange)} /> */}
94           {/* </div> */}
95           <div class="form-group row">
96             <button type="submit" class="btn btn-secondary">Remove Community</button>
97           </div>
98         </form>
99       }
100       <ul class="my-1 list-inline">
101         <li className="list-inline-item"><Link className="badge badge-light" to="/communities">{community.category_name}</Link></li>
102         <li className="list-inline-item badge badge-light">{community.number_of_subscribers} Subscribers</li>
103         <li className="list-inline-item badge badge-light">{community.number_of_posts} Posts</li>
104         <li className="list-inline-item badge badge-light">{community.number_of_comments} Comments</li>
105         <li className="list-inline-item"><Link className="badge badge-light" to={`/modlog/community/${this.props.community.id}`}>Modlog</Link></li>
106       </ul>
107       <ul class="list-inline small"> 
108         <li class="list-inline-item">mods: </li>
109         {this.props.moderators.map(mod =>
110           <li class="list-inline-item"><Link class="text-info" to={`/user/${mod.user_id}`}>{mod.user_name}</Link></li>
111         )}
112       </ul>
113       <div>
114         {community.subscribed 
115           ? <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</button>
116           : <button class="btn btn-sm btn-secondary" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</button>
117         }
118       </div>
119       {community.description && 
120         <div>
121           <hr />
122           <div className="md-div" dangerouslySetInnerHTML={mdToHtml(community.description)} />
123           <hr />
124         </div>
125       }
126     </div>
127     );
128   }
129
130   handleEditClick(i: Sidebar) {
131     i.state.showEdit = true;
132     i.setState(i.state);
133   }
134
135   handleEditCommunity() {
136     this.state.showEdit = false;
137     this.setState(this.state);
138   }
139
140   handleEditCancel() {
141     this.state.showEdit = false;
142     this.setState(this.state);
143   }
144
145   // TODO no deleting communities yet
146   // handleDeleteClick(i: Sidebar, event) {
147   // }
148
149   handleUnsubscribe(communityId: number) {
150     let form: FollowCommunityForm = {
151       community_id: communityId,
152       follow: false
153     };
154     WebSocketService.Instance.followCommunity(form);
155   }
156
157   handleSubscribe(communityId: number) {
158     let form: FollowCommunityForm = {
159       community_id: communityId,
160       follow: true
161     };
162     WebSocketService.Instance.followCommunity(form);
163   }
164
165   private get amCreator(): boolean {
166     return this.props.community.creator_id == UserService.Instance.user.id;
167   }
168
169   get canMod(): boolean {
170     return UserService.Instance.user && this.props.moderators.map(m => m.user_id).includes(UserService.Instance.user.id);
171   }
172
173   get canAdmin(): boolean {
174     return UserService.Instance.user && this.props.admins.map(a => a.id).includes(UserService.Instance.user.id);
175   }
176
177   handleDeleteClick() {
178   }
179
180   handleModRemoveShow(i: Sidebar) {
181     i.state.showRemoveDialog = true;
182     i.setState(i.state);
183   }
184
185   handleModRemoveReasonChange(i: Sidebar, event: any) {
186     i.state.removeReason = event.target.value;
187     i.setState(i.state);
188   }
189
190   handleModRemoveExpiresChange(i: Sidebar, event: any) {
191     console.log(event.target.value);
192     i.state.removeExpires = event.target.value;
193     i.setState(i.state);
194   }
195
196   handleModRemoveSubmit(i: Sidebar) {
197     event.preventDefault();
198     let deleteForm: CommunityFormI = {
199       name: i.props.community.name,
200       title: i.props.community.title,
201       category_id: i.props.community.category_id,
202       edit_id: i.props.community.id,
203       removed: !i.props.community.removed,
204       reason: i.state.removeReason,
205       expires: getUnixTime(i.state.removeExpires),
206       auth: null,
207     };
208     WebSocketService.Instance.editCommunity(deleteForm);
209
210     i.state.showRemoveDialog = false;
211     i.setState(i.state);
212   }
213 }