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