Some cleanup.

This commit is contained in:
Dessalines 2020-09-08 22:13:26 -05:00
parent bea7a37983
commit b7f1b9c2d7
10 changed files with 81 additions and 115 deletions

View file

@ -38,7 +38,7 @@ export class App extends Component<AppProps, any> {
</Switch> </Switch>
<Symbols /> <Symbols />
</div> </div>
<Footer /> <Footer site={this.props.site} />
</div> </div>
</Provider> </Provider>
</> </>

View file

@ -1,7 +1,6 @@
import { Component } from 'inferno'; import { Component } from 'inferno';
import { Link } from 'inferno-router'; import { Link } from 'inferno-router';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { retryWhen, delay, take } from 'rxjs/operators';
import { import {
CommentNode as CommentNodeI, CommentNode as CommentNodeI,
CommentForm as CommentFormI, CommentForm as CommentFormI,
@ -9,7 +8,7 @@ import {
UserOperation, UserOperation,
CommentResponse, CommentResponse,
} from 'lemmy-js-client'; } from 'lemmy-js-client';
import { capitalizeFirstLetter, wsJsonToRes } from '../utils'; import { capitalizeFirstLetter, wsJsonToRes, wsSubscribe } from '../utils';
import { WebSocketService, UserService } from '../services'; import { WebSocketService, UserService } from '../services';
import { i18n } from '../i18next'; import { i18n } from '../i18next';
import { T } from 'inferno-i18next'; import { T } from 'inferno-i18next';
@ -71,13 +70,8 @@ export class CommentForm extends Component<CommentFormProps, CommentFormState> {
} }
} }
this.subscription = WebSocketService.Instance.subject this.parseMessage = this.parseMessage.bind(this);
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) this.subscription = wsSubscribe(this.parseMessage);
.subscribe(
msg => this.parseMessage(msg),
err => console.error(err),
() => console.log('complete')
);
} }
componentWillUnmount() { componentWillUnmount() {

View file

@ -1,7 +1,6 @@
import { Component, linkEvent } from 'inferno'; import { Component, linkEvent } from 'inferno';
import { Prompt } from 'inferno-router'; import { Prompt } from 'inferno-router';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { retryWhen, delay, take } from 'rxjs/operators';
import { import {
CommunityForm as CommunityFormI, CommunityForm as CommunityFormI,
UserOperation, UserOperation,
@ -11,7 +10,13 @@ import {
Community, Community,
} from 'lemmy-js-client'; } from 'lemmy-js-client';
import { WebSocketService } from '../services'; import { WebSocketService } from '../services';
import { wsJsonToRes, capitalizeFirstLetter, toast, randomStr } from '../utils'; import {
wsJsonToRes,
capitalizeFirstLetter,
toast,
randomStr,
wsSubscribe,
} from '../utils';
import { i18n } from '../i18next'; import { i18n } from '../i18next';
import { MarkdownTextArea } from './markdown-textarea'; import { MarkdownTextArea } from './markdown-textarea';
@ -79,13 +84,8 @@ export class CommunityForm extends Component<
}; };
} }
this.subscription = WebSocketService.Instance.subject this.parseMessage = this.parseMessage.bind(this);
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) this.subscription = wsSubscribe(this.parseMessage);
.subscribe(
msg => this.parseMessage(msg),
err => console.error(err),
() => console.log('complete')
);
} }
componentDidUpdate() { componentDidUpdate() {

View file

@ -1,43 +1,18 @@
import { Component } from 'inferno'; import { Component } from 'inferno';
import { Link } from 'inferno-router'; import { Link } from 'inferno-router';
import { i18n } from '../i18next'; import { i18n } from '../i18next';
import { Subscription } from 'rxjs'; import { repoUrl } from '../utils';
import { retryWhen, delay, take } from 'rxjs/operators'; import { GetSiteResponse } from 'lemmy-js-client';
import { WebSocketService } from '../services';
import { repoUrl, wsJsonToRes, isBrowser } from '../utils';
import {
UserOperation,
WebSocketJsonResponse,
GetSiteResponse,
} from 'lemmy-js-client';
interface FooterState { interface FooterProps {
version: string; site: GetSiteResponse;
} }
export class Footer extends Component<any, FooterState> { interface FooterState {}
private wsSub: Subscription;
emptyState: FooterState = { export class Footer extends Component<FooterProps, FooterState> {
version: null,
};
constructor(props: any, context: any) { constructor(props: any, context: any) {
super(props, context); super(props, context);
this.state = this.emptyState;
if (isBrowser()) {
this.wsSub = WebSocketService.Instance.subject
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
.subscribe(
msg => this.parseMessage(msg),
err => console.error(err),
() => console.log('complete')
);
}
}
componentWillUnmount() {
this.wsSub.unsubscribe();
} }
render() { render() {
@ -46,7 +21,7 @@ export class Footer extends Component<any, FooterState> {
<div className="navbar-collapse"> <div className="navbar-collapse">
<ul class="navbar-nav ml-auto"> <ul class="navbar-nav ml-auto">
<li class="nav-item"> <li class="nav-item">
<span class="navbar-text">{this.state.version}</span> <span class="navbar-text">{this.props.site.version}</span>
</li> </li>
<li className="nav-item"> <li className="nav-item">
<Link className="nav-link" to="/modlog"> <Link className="nav-link" to="/modlog">
@ -78,12 +53,4 @@ export class Footer extends Component<any, FooterState> {
</nav> </nav>
); );
} }
parseMessage(msg: WebSocketJsonResponse) {
let res = wsJsonToRes(msg);
if (res.op == UserOperation.GetSite) {
let data = res.data as GetSiteResponse;
this.setState({ version: data.version });
}
}
} }

View file

@ -1,7 +1,6 @@
import { Component, linkEvent } from 'inferno'; import { Component, linkEvent } from 'inferno';
import { Helmet } from 'inferno-helmet'; import { Helmet } from 'inferno-helmet';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { retryWhen, delay, take } from 'rxjs/operators';
import { import {
LoginForm, LoginForm,
RegisterForm, RegisterForm,
@ -14,7 +13,14 @@ import {
Site, Site,
} from 'lemmy-js-client'; } from 'lemmy-js-client';
import { WebSocketService, UserService } from '../services'; import { WebSocketService, UserService } from '../services';
import { wsJsonToRes, validEmail, toast } from '../utils'; import {
wsJsonToRes,
validEmail,
toast,
wsSubscribe,
isBrowser,
setIsoData,
} from '../utils';
import { i18n } from '../i18next'; import { i18n } from '../i18next';
interface State { interface State {
@ -28,6 +34,7 @@ interface State {
} }
export class Login extends Component<any, State> { export class Login extends Component<any, State> {
private isoData = setIsoData(this.context);
private subscription: Subscription; private subscription: Subscription;
emptyState: State = { emptyState: State = {
@ -48,20 +55,7 @@ export class Login extends Component<any, State> {
registerLoading: false, registerLoading: false,
captcha: undefined, captcha: undefined,
captchaPlaying: false, captchaPlaying: false,
site: { site: this.isoData.site.site,
id: undefined,
name: undefined,
creator_id: undefined,
published: undefined,
creator_name: undefined,
number_of_users: undefined,
number_of_posts: undefined,
number_of_comments: undefined,
number_of_communities: undefined,
enable_downvotes: undefined,
open_registration: undefined,
enable_nsfw: undefined,
},
}; };
constructor(props: any, context: any) { constructor(props: any, context: any) {
@ -69,21 +63,19 @@ export class Login extends Component<any, State> {
this.state = this.emptyState; this.state = this.emptyState;
this.subscription = WebSocketService.Instance.subject this.parseMessage = this.parseMessage.bind(this);
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) this.subscription = wsSubscribe(this.parseMessage);
.subscribe(
msg => this.parseMessage(msg),
err => console.error(err),
() => console.log('complete')
);
WebSocketService.Instance.getSite(); if (isBrowser()) {
WebSocketService.Instance.getCaptcha(); WebSocketService.Instance.getCaptcha();
} }
}
componentWillUnmount() { componentWillUnmount() {
if (isBrowser()) {
this.subscription.unsubscribe(); this.subscription.unsubscribe();
} }
}
get documentTitle(): string { get documentTitle(): string {
if (this.state.site.name) { if (this.state.site.name) {

View file

@ -1,17 +1,22 @@
import { Component, linkEvent } from 'inferno'; import { Component, linkEvent } from 'inferno';
import { Helmet } from 'inferno-helmet'; import { Helmet } from 'inferno-helmet';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { retryWhen, delay, take } from 'rxjs/operators';
import { import {
UserOperation, UserOperation,
LoginResponse, LoginResponse,
PasswordChangeForm, PasswordChangeForm,
WebSocketJsonResponse, WebSocketJsonResponse,
GetSiteResponse,
Site, Site,
} from 'lemmy-js-client'; } from 'lemmy-js-client';
import { WebSocketService, UserService } from '../services'; import { WebSocketService, UserService } from '../services';
import { wsJsonToRes, capitalizeFirstLetter, toast } from '../utils'; import {
wsJsonToRes,
capitalizeFirstLetter,
toast,
setIsoData,
isBrowser,
wsSubscribe,
} from '../utils';
import { i18n } from '../i18next'; import { i18n } from '../i18next';
interface State { interface State {
@ -21,6 +26,7 @@ interface State {
} }
export class PasswordChange extends Component<any, State> { export class PasswordChange extends Component<any, State> {
private isoData = setIsoData(this.context);
private subscription: Subscription; private subscription: Subscription;
emptyState: State = { emptyState: State = {
@ -30,7 +36,7 @@ export class PasswordChange extends Component<any, State> {
password_verify: undefined, password_verify: undefined,
}, },
loading: false, loading: false,
site: undefined, site: this.isoData.site.site,
}; };
constructor(props: any, context: any) { constructor(props: any, context: any) {
@ -38,26 +44,18 @@ export class PasswordChange extends Component<any, State> {
this.state = this.emptyState; this.state = this.emptyState;
this.subscription = WebSocketService.Instance.subject this.parseMessage = this.parseMessage.bind(this);
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10)))) this.subscription = wsSubscribe(this.parseMessage);
.subscribe(
msg => this.parseMessage(msg),
err => console.error(err),
() => console.log('complete')
);
WebSocketService.Instance.getSite();
} }
componentWillUnmount() { componentWillUnmount() {
if (isBrowser()) {
this.subscription.unsubscribe(); this.subscription.unsubscribe();
} }
}
get documentTitle(): string { get documentTitle(): string {
if (this.state.site) {
return `${i18n.t('password_change')} - ${this.state.site.name}`; return `${i18n.t('password_change')} - ${this.state.site.name}`;
} else {
return 'Lemmy';
}
} }
render() { render() {
@ -153,10 +151,6 @@ export class PasswordChange extends Component<any, State> {
this.setState(this.state); this.setState(this.state);
UserService.Instance.login(data); UserService.Instance.login(data);
this.props.history.push('/'); this.props.history.push('/');
} else if (res.op == UserOperation.GetSite) {
let data = res.data as GetSiteResponse;
this.state.site = data.site;
this.setState(this.state);
} }
} }
} }

View file

@ -62,7 +62,7 @@ interface PostListingState {
interface PostListingProps { interface PostListingProps {
post: Post; post: Post;
communities: Community[]; communities: Community[]; // TODO this should be an optional
showCommunity?: boolean; showCommunity?: boolean;
showBody?: boolean; showBody?: boolean;
moderators?: CommunityUser[]; moderators?: CommunityUser[];

View file

@ -27,6 +27,7 @@ export class PostListings extends Component<PostListingsProps, any> {
this.outer().map(post => ( this.outer().map(post => (
<> <>
<PostListing <PostListing
communities={[]}
post={post} post={post}
showCommunity={this.props.showCommunity} showCommunity={this.props.showCommunity}
enableDownvotes={this.props.enableDownvotes} enableDownvotes={this.props.enableDownvotes}

View file

@ -1,12 +1,12 @@
import { Component, linkEvent } from 'inferno'; import { Component, linkEvent } from 'inferno';
import { Link } from 'inferno-router';
import { import {
PrivateMessage as PrivateMessageI, PrivateMessage as PrivateMessageI,
DeletePrivateMessageForm, DeletePrivateMessageForm,
MarkPrivateMessageAsReadForm, MarkPrivateMessageAsReadForm,
UserView,
} from 'lemmy-js-client'; } from 'lemmy-js-client';
import { WebSocketService, UserService } from '../services'; import { WebSocketService, UserService } from '../services';
import { mdToHtml, pictrsAvatarThumbnail, showAvatars, toast } from '../utils'; import { mdToHtml, toast } from '../utils';
import { MomentTime } from './moment-time'; import { MomentTime } from './moment-time';
import { PrivateMessageForm } from './private-message-form'; import { PrivateMessageForm } from './private-message-form';
import { UserListing, UserOther } from './user-listing'; import { UserListing, UserOther } from './user-listing';
@ -17,6 +17,7 @@ interface PrivateMessageState {
showEdit: boolean; showEdit: boolean;
collapsed: boolean; collapsed: boolean;
viewSource: boolean; viewSource: boolean;
recipient: UserView;
} }
interface PrivateMessageProps { interface PrivateMessageProps {
@ -32,6 +33,21 @@ export class PrivateMessage extends Component<
showEdit: false, showEdit: false,
collapsed: false, collapsed: false,
viewSource: false, viewSource: false,
recipient: {
id: this.props.privateMessage.recipient_id,
actor_id: this.props.privateMessage.recipient_actor_id,
name: this.props.privateMessage.recipient_name,
local: this.props.privateMessage.recipient_local,
avatar: this.props.privateMessage.recipient_avatar,
preferred_username: this.props.privateMessage
.recipient_preferred_username,
published: undefined,
number_of_posts: 0,
post_score: 0,
number_of_comments: 0,
comment_score: 0,
banned: false,
},
}; };
constructor(props: any, context: any) { constructor(props: any, context: any) {
@ -109,6 +125,7 @@ export class PrivateMessage extends Component<
</ul> </ul>
{this.state.showEdit && ( {this.state.showEdit && (
<PrivateMessageForm <PrivateMessageForm
recipient={this.state.recipient}
privateMessage={message} privateMessage={message}
onEdit={this.handlePrivateMessageEdit} onEdit={this.handlePrivateMessageEdit}
onCreate={this.handlePrivateMessageCreate} onCreate={this.handlePrivateMessageCreate}
@ -215,9 +232,7 @@ export class PrivateMessage extends Component<
</div> </div>
{this.state.showReply && ( {this.state.showReply && (
<PrivateMessageForm <PrivateMessageForm
params={{ recipient={this.state.recipient}
recipient_id: this.props.privateMessage.creator_id,
}}
onCreate={this.handlePrivateMessageCreate} onCreate={this.handlePrivateMessageCreate}
/> />
)} )}

View file

@ -33,7 +33,10 @@ export const routes: IRoutePropsWithFetch[] = [
component: Main, component: Main,
fetchInitialData: (auth, path) => Main.fetchInitialData(auth, path), fetchInitialData: (auth, path) => Main.fetchInitialData(auth, path),
}, },
{ path: `/login`, component: Login }, {
path: `/login`,
component: Login,
},
{ {
path: `/create_post`, path: `/create_post`,
component: CreatePost, component: CreatePost,