]> Untitled Git - lemmy.git/blob - ui/src/components/communities.tsx
Spamtimer
[lemmy.git] / ui / src / components / communities.tsx
1 import { Component, linkEvent } from 'inferno';
2 import { Link } from 'inferno-router';
3 import { Subscription } from "rxjs";
4 import { retryWhen, delay, take } from 'rxjs/operators';
5 import { UserOperation, Community, ListCommunitiesResponse, CommunityResponse, FollowCommunityForm, ListCommunitiesForm, SortType } from '../interfaces';
6 import { WebSocketService } from '../services';
7 import { msgOp } from '../utils';
8
9 declare const Sortable: any;
10
11 interface CommunitiesState {
12   communities: Array<Community>;
13   loading: boolean;
14 }
15
16 export class Communities extends Component<any, CommunitiesState> {
17   private subscription: Subscription;
18   private emptyState: CommunitiesState = {
19     communities: [],
20     loading: true
21   }
22
23   constructor(props: any, context: any) {
24     super(props, context);
25     this.state = this.emptyState;
26     this.subscription = WebSocketService.Instance.subject
27     .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
28     .subscribe(
29       (msg) => this.parseMessage(msg),
30         (err) => console.error(err),
31         () => console.log('complete')
32     );
33
34     let listCommunitiesForm: ListCommunitiesForm = {
35       sort: SortType[SortType.TopAll],
36       limit: 100,
37     }
38
39     WebSocketService.Instance.listCommunities(listCommunitiesForm);
40
41   }
42
43   componentWillUnmount() {
44     this.subscription.unsubscribe();
45   }
46
47   componentDidMount() {
48     document.title = "Forums - Lemmy";
49     let table = document.querySelector('#community_table');
50     Sortable.initTable(table);
51   }
52
53   render() {
54     return (
55       <div class="container">
56         {this.state.loading ? 
57         <h5 class=""><svg class="icon icon-spinner spin"><use xlinkHref="#icon-spinner"></use></svg></h5> : 
58         <div>
59           <h5>Forums</h5>
60           <div class="table-responsive">
61             <table id="community_table" class="table table-sm table-hover">
62               <thead class="pointer">
63                 <tr>
64                   <th>Name</th>
65                   <th>Title</th>
66                   <th>Category</th>
67                   <th class="text-right d-none d-md-table-cell">Subscribers</th>
68                   <th class="text-right d-none d-md-table-cell">Posts</th>
69                   <th class="text-right d-none d-md-table-cell">Comments</th>
70                   <th></th>
71                 </tr>
72               </thead>
73               <tbody>
74                 {this.state.communities.map(community =>
75                   <tr>
76                     <td><Link to={`/f/${community.name}`}>{community.name}</Link></td>
77                     <td>{community.title}</td>
78                     <td>{community.category_name}</td>
79                     <td class="text-right d-none d-md-table-cell">{community.number_of_subscribers}</td>
80                     <td class="text-right d-none d-md-table-cell">{community.number_of_posts}</td>
81                     <td class="text-right d-none d-md-table-cell">{community.number_of_comments}</td>
82                     <td class="text-right">
83                       {community.subscribed ? 
84                       <span class="pointer btn-link" onClick={linkEvent(community.id, this.handleUnsubscribe)}>Unsubscribe</span> : 
85                       <span class="pointer btn-link" onClick={linkEvent(community.id, this.handleSubscribe)}>Subscribe</span>
86                       }
87                     </td>
88                   </tr>
89                 )}
90               </tbody>
91             </table>
92           </div>
93         </div>
94         }
95       </div>
96     );
97   }
98
99   handleUnsubscribe(communityId: number) {
100     let form: FollowCommunityForm = {
101       community_id: communityId,
102       follow: false
103     };
104     WebSocketService.Instance.followCommunity(form);
105   }
106
107   handleSubscribe(communityId: number) {
108     let form: FollowCommunityForm = {
109       community_id: communityId,
110       follow: true
111     };
112     WebSocketService.Instance.followCommunity(form);
113   }
114
115   parseMessage(msg: any) {
116     console.log(msg);
117     let op: UserOperation = msgOp(msg);
118     if (msg.error) {
119       alert(msg.error);
120       return;
121     } else if (op == UserOperation.ListCommunities) {
122       let res: ListCommunitiesResponse = msg;
123       this.state.communities = res.communities;
124       this.state.communities.sort((a, b) => b.number_of_subscribers - a.number_of_subscribers);
125       this.state.loading = false;
126       this.setState(this.state);
127     } else if (op == UserOperation.FollowCommunity) {
128       let res: CommunityResponse = msg;
129       let found = this.state.communities.find(c => c.id == res.community.id);
130       found.subscribed = res.community.subscribed;
131       found.number_of_subscribers = res.community.number_of_subscribers;
132       this.setState(this.state);
133     } 
134   }
135 }