]> Untitled Git - lemmy.git/blob - ui/src/components/communities.tsx
Adding Iframe expand
[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, Post as PostI, GetPostResponse, PostResponse, Comment, CommentForm as CommentFormI, CommentResponse, CommentLikeForm, CommentSortType, CreatePostLikeResponse, ListCommunitiesResponse } from '../interfaces';
6 import { WebSocketService, UserService } from '../services';
7 import { msgOp, hotRank,mdToHtml } from '../utils';
8
9 declare const Sortable: any;
10
11 interface CommunitiesState {
12   communities: Array<Community>;
13 }
14
15 export class Communities extends Component<any, CommunitiesState> {
16   private subscription: Subscription;
17   private emptyState: CommunitiesState = {
18     communities: []
19   }
20
21   constructor(props, context) {
22     super(props, context);
23     this.state = this.emptyState;
24     this.subscription = WebSocketService.Instance.subject
25       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
26       .subscribe(
27         (msg) => this.parseMessage(msg),
28         (err) => console.error(err),
29         () => console.log('complete')
30       );
31     WebSocketService.Instance.listCommunities();
32   }
33
34   componentDidMount() {
35     let table = document.querySelector('#community_table');
36     Sortable.initTable(table);
37   }
38
39   render() {
40     return (
41       <div class="container-fluid">
42         <h4>Communities</h4>
43         <div class="table-responsive">
44           <table id="community_table" class="table table-sm table-hover" data-sortable>
45             <thead>
46               <tr>
47                 <th>Name</th>
48                 <th>Title</th>
49                 <th>Category</th>
50                 <th class="text-right">Subscribers</th>
51                 <th class="text-right">Posts</th>
52                 <th class="text-right">Comments</th>
53               </tr>
54             </thead>
55             <tbody>
56               {this.state.communities.map(community =>
57                 <tr>
58                   <td><Link to={`/community/${community.id}`}>{community.name}</Link></td>
59                   <td>{community.title}</td>
60                   <td>{community.category_name}</td>
61                   <td class="text-right">{community.number_of_subscribers}</td>
62                   <td class="text-right">{community.number_of_posts}</td>
63                   <td class="text-right">{community.number_of_comments}</td>
64                 </tr>
65               )}
66             </tbody>
67           </table>
68         </div>
69       </div>
70     );
71   }
72
73
74
75   parseMessage(msg: any) {
76     console.log(msg);
77     let op: UserOperation = msgOp(msg);
78     if (msg.error) {
79       alert(msg.error);
80       return;
81     } else if (op == UserOperation.ListCommunities) {
82       let res: ListCommunitiesResponse = msg;
83       this.state.communities = res.communities;
84       this.state.communities.sort((a, b) => b.number_of_subscribers - a.number_of_subscribers);
85       this.setState(this.state);
86     }
87   }
88 }