2021-02-22 02:39:04 +00:00
|
|
|
import { Component, linkEvent } from "inferno";
|
|
|
|
import { Prompt } from "inferno-router";
|
2020-09-06 16:15:25 +00:00
|
|
|
import {
|
|
|
|
CommunityResponse,
|
2020-12-24 01:58:27 +00:00
|
|
|
CommunityView,
|
2021-07-17 20:42:55 +00:00
|
|
|
CreateCommunity,
|
|
|
|
EditCommunity,
|
|
|
|
UserOperation,
|
2021-02-22 02:39:04 +00:00
|
|
|
} from "lemmy-js-client";
|
2021-07-17 20:42:55 +00:00
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { i18n } from "../../i18next";
|
2021-08-20 02:56:18 +00:00
|
|
|
import { UserService, WebSocketService } from "../../services";
|
2020-09-09 03:13:26 +00:00
|
|
|
import {
|
2021-07-17 20:42:55 +00:00
|
|
|
authField,
|
2020-09-09 03:13:26 +00:00
|
|
|
capitalizeFirstLetter,
|
|
|
|
randomStr,
|
2021-07-17 20:42:55 +00:00
|
|
|
toast,
|
|
|
|
wsClient,
|
|
|
|
wsJsonToRes,
|
2020-09-09 03:13:26 +00:00
|
|
|
wsSubscribe,
|
2020-12-24 01:58:27 +00:00
|
|
|
wsUserOp,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "../../utils";
|
|
|
|
import { Icon, Spinner } from "../common/icon";
|
|
|
|
import { ImageUploadForm } from "../common/image-upload-form";
|
|
|
|
import { MarkdownTextArea } from "../common/markdown-textarea";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
interface CommunityFormProps {
|
2020-12-24 01:58:27 +00:00
|
|
|
community_view?: CommunityView; // If a community is given, that means this is an edit
|
2020-09-06 16:15:25 +00:00
|
|
|
onCancel?(): any;
|
2020-12-24 01:58:27 +00:00
|
|
|
onCreate?(community: CommunityView): any;
|
|
|
|
onEdit?(community: CommunityView): any;
|
2020-09-06 16:15:25 +00:00
|
|
|
enableNsfw: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface CommunityFormState {
|
2020-12-24 01:58:27 +00:00
|
|
|
communityForm: CreateCommunity;
|
2020-09-06 16:15:25 +00:00
|
|
|
loading: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class CommunityForm extends Component<
|
|
|
|
CommunityFormProps,
|
|
|
|
CommunityFormState
|
|
|
|
> {
|
|
|
|
private id = `community-form-${randomStr()}`;
|
|
|
|
private subscription: Subscription;
|
|
|
|
|
|
|
|
private emptyState: CommunityFormState = {
|
|
|
|
communityForm: {
|
|
|
|
name: null,
|
|
|
|
title: null,
|
|
|
|
nsfw: false,
|
|
|
|
icon: null,
|
|
|
|
banner: null,
|
2020-12-26 03:28:05 +00:00
|
|
|
auth: authField(false),
|
2020-09-06 16:15:25 +00:00
|
|
|
},
|
|
|
|
loading: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props: any, context: any) {
|
|
|
|
super(props, context);
|
|
|
|
|
|
|
|
this.state = this.emptyState;
|
|
|
|
|
2021-07-17 20:42:55 +00:00
|
|
|
this.handleCommunityDescriptionChange =
|
|
|
|
this.handleCommunityDescriptionChange.bind(this);
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
this.handleIconUpload = this.handleIconUpload.bind(this);
|
|
|
|
this.handleIconRemove = this.handleIconRemove.bind(this);
|
|
|
|
|
|
|
|
this.handleBannerUpload = this.handleBannerUpload.bind(this);
|
|
|
|
this.handleBannerRemove = this.handleBannerRemove.bind(this);
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
let cv = this.props.community_view;
|
|
|
|
if (cv) {
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.communityForm = {
|
2020-12-24 01:58:27 +00:00
|
|
|
name: cv.community.name,
|
|
|
|
title: cv.community.title,
|
|
|
|
description: cv.community.description,
|
|
|
|
nsfw: cv.community.nsfw,
|
|
|
|
icon: cv.community.icon,
|
|
|
|
banner: cv.community.banner,
|
2020-12-24 22:05:57 +00:00
|
|
|
auth: authField(),
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-09 03:13:26 +00:00
|
|
|
this.parseMessage = this.parseMessage.bind(this);
|
|
|
|
this.subscription = wsSubscribe(this.parseMessage);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
// TODO this should be checked out
|
2020-09-06 16:15:25 +00:00
|
|
|
componentDidUpdate() {
|
|
|
|
if (
|
|
|
|
!this.state.loading &&
|
|
|
|
(this.state.communityForm.name ||
|
|
|
|
this.state.communityForm.title ||
|
|
|
|
this.state.communityForm.description)
|
|
|
|
) {
|
|
|
|
window.onbeforeunload = () => true;
|
|
|
|
} else {
|
|
|
|
window.onbeforeunload = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.subscription.unsubscribe();
|
|
|
|
window.onbeforeunload = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Prompt
|
|
|
|
when={
|
|
|
|
!this.state.loading &&
|
|
|
|
(this.state.communityForm.name ||
|
|
|
|
this.state.communityForm.title ||
|
|
|
|
this.state.communityForm.description)
|
|
|
|
}
|
2021-02-22 02:39:04 +00:00
|
|
|
message={i18n.t("block_leaving")}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
<form onSubmit={linkEvent(this, this.handleCreateCommunitySubmit)}>
|
2020-12-24 01:58:27 +00:00
|
|
|
{!this.props.community_view && (
|
2020-09-06 16:15:25 +00:00
|
|
|
<div class="form-group row">
|
|
|
|
<label class="col-12 col-form-label" htmlFor="community-name">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("name")}
|
2020-09-06 16:15:25 +00:00
|
|
|
<span
|
|
|
|
class="pointer unselectable ml-2 text-muted"
|
2021-02-22 02:39:04 +00:00
|
|
|
data-tippy-content={i18n.t("name_explain")}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="help-circle" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</span>
|
|
|
|
</label>
|
|
|
|
<div class="col-12">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="community-name"
|
|
|
|
class="form-control"
|
|
|
|
value={this.state.communityForm.name}
|
|
|
|
onInput={linkEvent(this, this.handleCommunityNameChange)}
|
|
|
|
required
|
|
|
|
minLength={3}
|
|
|
|
pattern="[a-z0-9_]+"
|
2021-02-22 02:39:04 +00:00
|
|
|
title={i18n.t("community_reqs")}
|
2020-09-06 16:15:25 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div class="form-group row">
|
|
|
|
<label class="col-12 col-form-label" htmlFor="community-title">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("display_name")}
|
2020-09-06 16:15:25 +00:00
|
|
|
<span
|
|
|
|
class="pointer unselectable ml-2 text-muted"
|
2021-02-22 02:39:04 +00:00
|
|
|
data-tippy-content={i18n.t("display_name_explain")}
|
2020-09-06 16:15:25 +00:00
|
|
|
>
|
2021-02-11 20:35:27 +00:00
|
|
|
<Icon icon="help-circle" classes="icon-inline" />
|
2020-09-06 16:15:25 +00:00
|
|
|
</span>
|
|
|
|
</label>
|
|
|
|
<div class="col-12">
|
|
|
|
<input
|
|
|
|
type="text"
|
|
|
|
id="community-title"
|
|
|
|
value={this.state.communityForm.title}
|
|
|
|
onInput={linkEvent(this, this.handleCommunityTitleChange)}
|
|
|
|
class="form-control"
|
|
|
|
required
|
|
|
|
minLength={3}
|
|
|
|
maxLength={100}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="form-group">
|
2021-02-22 02:39:04 +00:00
|
|
|
<label>{i18n.t("icon")}</label>
|
2020-09-06 16:15:25 +00:00
|
|
|
<ImageUploadForm
|
2021-02-22 02:39:04 +00:00
|
|
|
uploadTitle={i18n.t("upload_icon")}
|
2020-09-06 16:15:25 +00:00
|
|
|
imageSrc={this.state.communityForm.icon}
|
|
|
|
onUpload={this.handleIconUpload}
|
|
|
|
onRemove={this.handleIconRemove}
|
|
|
|
rounded
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="form-group">
|
2021-02-22 02:39:04 +00:00
|
|
|
<label>{i18n.t("banner")}</label>
|
2020-09-06 16:15:25 +00:00
|
|
|
<ImageUploadForm
|
2021-02-22 02:39:04 +00:00
|
|
|
uploadTitle={i18n.t("upload_banner")}
|
2020-09-06 16:15:25 +00:00
|
|
|
imageSrc={this.state.communityForm.banner}
|
|
|
|
onUpload={this.handleBannerUpload}
|
|
|
|
onRemove={this.handleBannerRemove}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row">
|
|
|
|
<label class="col-12 col-form-label" htmlFor={this.id}>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("sidebar")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
|
|
|
<div class="col-12">
|
|
|
|
<MarkdownTextArea
|
|
|
|
initialContent={this.state.communityForm.description}
|
|
|
|
onContentChange={this.handleCommunityDescriptionChange}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{this.props.enableNsfw && (
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<div class="form-check">
|
|
|
|
<input
|
|
|
|
class="form-check-input"
|
|
|
|
id="community-nsfw"
|
|
|
|
type="checkbox"
|
|
|
|
checked={this.state.communityForm.nsfw}
|
|
|
|
onChange={linkEvent(this, this.handleCommunityNsfwChange)}
|
|
|
|
/>
|
|
|
|
<label class="form-check-label" htmlFor="community-nsfw">
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("nsfw")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
<div class="form-group row">
|
|
|
|
<div class="col-12">
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
class="btn btn-secondary mr-2"
|
|
|
|
disabled={this.state.loading}
|
|
|
|
>
|
|
|
|
{this.state.loading ? (
|
2021-02-11 20:35:27 +00:00
|
|
|
<Spinner />
|
2020-12-24 01:58:27 +00:00
|
|
|
) : this.props.community_view ? (
|
2021-02-22 02:39:04 +00:00
|
|
|
capitalizeFirstLetter(i18n.t("save"))
|
2020-09-06 16:15:25 +00:00
|
|
|
) : (
|
2021-02-22 02:39:04 +00:00
|
|
|
capitalizeFirstLetter(i18n.t("create"))
|
2020-09-06 16:15:25 +00:00
|
|
|
)}
|
|
|
|
</button>
|
2020-12-24 01:58:27 +00:00
|
|
|
{this.props.community_view && (
|
2020-09-06 16:15:25 +00:00
|
|
|
<button
|
|
|
|
type="button"
|
|
|
|
class="btn btn-secondary"
|
|
|
|
onClick={linkEvent(this, this.handleCancel)}
|
|
|
|
>
|
2021-02-22 02:39:04 +00:00
|
|
|
{i18n.t("cancel")}
|
2020-09-06 16:15:25 +00:00
|
|
|
</button>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCreateCommunitySubmit(i: CommunityForm, event: any) {
|
|
|
|
event.preventDefault();
|
|
|
|
i.state.loading = true;
|
2020-12-24 01:58:27 +00:00
|
|
|
if (i.props.community_view) {
|
|
|
|
let form: EditCommunity = {
|
|
|
|
...i.state.communityForm,
|
2021-01-18 22:42:41 +00:00
|
|
|
community_id: i.props.community_view.community.id,
|
2020-12-24 01:58:27 +00:00
|
|
|
};
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(wsClient.editCommunity(form));
|
2020-09-06 16:15:25 +00:00
|
|
|
} else {
|
2020-12-24 22:05:57 +00:00
|
|
|
WebSocketService.Instance.send(
|
|
|
|
wsClient.createCommunity(i.state.communityForm)
|
|
|
|
);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCommunityNameChange(i: CommunityForm, event: any) {
|
|
|
|
i.state.communityForm.name = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCommunityTitleChange(i: CommunityForm, event: any) {
|
|
|
|
i.state.communityForm.title = event.target.value;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCommunityDescriptionChange(val: string) {
|
|
|
|
this.state.communityForm.description = val;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCommunityNsfwChange(i: CommunityForm, event: any) {
|
|
|
|
i.state.communityForm.nsfw = event.target.checked;
|
|
|
|
i.setState(i.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleCancel(i: CommunityForm) {
|
|
|
|
i.props.onCancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
handleIconUpload(url: string) {
|
|
|
|
this.state.communityForm.icon = url;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleIconRemove() {
|
2021-02-22 02:39:04 +00:00
|
|
|
this.state.communityForm.icon = "";
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBannerUpload(url: string) {
|
|
|
|
this.state.communityForm.banner = url;
|
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleBannerRemove() {
|
2021-02-22 02:39:04 +00:00
|
|
|
this.state.communityForm.banner = "";
|
2020-09-06 16:15:25 +00:00
|
|
|
this.setState(this.state);
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
parseMessage(msg: any) {
|
|
|
|
let op = wsUserOp(msg);
|
2021-04-07 15:54:38 +00:00
|
|
|
console.log(msg);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (msg.error) {
|
2021-02-22 02:39:04 +00:00
|
|
|
toast(i18n.t(msg.error), "danger");
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.loading = false;
|
|
|
|
this.setState(this.state);
|
|
|
|
return;
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.CreateCommunity) {
|
|
|
|
let data = wsJsonToRes<CommunityResponse>(msg).data;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.loading = false;
|
2020-12-24 01:58:27 +00:00
|
|
|
this.props.onCreate(data.community_view);
|
2021-08-20 02:56:18 +00:00
|
|
|
|
|
|
|
// Update myUserInfo
|
|
|
|
let community = data.community_view.community;
|
|
|
|
let person = UserService.Instance.myUserInfo.local_user_view.person;
|
|
|
|
UserService.Instance.myUserInfo.follows.push({
|
|
|
|
community,
|
|
|
|
follower: person,
|
|
|
|
});
|
|
|
|
UserService.Instance.myUserInfo.moderates.push({
|
|
|
|
community,
|
|
|
|
moderator: person,
|
|
|
|
});
|
2020-12-24 01:58:27 +00:00
|
|
|
} else if (op == UserOperation.EditCommunity) {
|
|
|
|
let data = wsJsonToRes<CommunityResponse>(msg).data;
|
2020-09-06 16:15:25 +00:00
|
|
|
this.state.loading = false;
|
2020-12-24 01:58:27 +00:00
|
|
|
this.props.onEdit(data.community_view);
|
2021-08-20 02:56:18 +00:00
|
|
|
let community = data.community_view.community;
|
|
|
|
|
|
|
|
let followFound = UserService.Instance.myUserInfo.follows.findIndex(
|
|
|
|
f => f.community.id == community.id
|
|
|
|
);
|
|
|
|
if (followFound) {
|
|
|
|
UserService.Instance.myUserInfo.follows[followFound].community =
|
|
|
|
community;
|
|
|
|
}
|
|
|
|
|
|
|
|
let moderatesFound = UserService.Instance.myUserInfo.moderates.findIndex(
|
|
|
|
f => f.community.id == community.id
|
|
|
|
);
|
|
|
|
if (moderatesFound) {
|
|
|
|
UserService.Instance.myUserInfo.moderates[moderatesFound].community =
|
|
|
|
community;
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|