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