import React from 'react';
import PropTypes from 'prop-types';
import UrlParse from 'url-parse';
import { ApiUrlContext, MemberContext, SiteContext } from '../utils/contexts/';
import { formatInnerLink, csrfToken, postRequest } from '../utils/helpers/';
import { PageActions } from '../utils/actions/';
import { PageStore, ProfilePageStore } from '../utils/stores/';
import ProfilePagesHeader from '../components/profile-page/ProfilePagesHeader';
import ProfilePagesContent from '../components/profile-page/ProfilePagesContent';
import { MediaListRow } from '../components/MediaListRow';
import { ProfileMediaPage } from './ProfileMediaPage';
class ChannelContactForm extends React.PureComponent {
constructor(props) {
super(props);
this.state = {
subject: '',
body: '',
isSending: false,
};
this.onUpdateSubject = this.onUpdateSubject.bind(this);
this.onUpdateBody = this.onUpdateBody.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.onSubmitSuccess = this.onSubmitSuccess.bind(this);
this.onSubmitFail = this.onSubmitFail.bind(this);
}
onUpdateSubject() {
this.setState({
subject: this.refs.msgSubject.value.trim(),
});
}
onUpdateBody() {
this.setState({
body: this.refs.msgBody.value.trim(),
});
}
onSubmitSuccess(response) {
this.setState(
{
subject: '',
body: '',
isSending: false,
},
function () {
setTimeout(
function () {
// FIXME: Without delay creates conflict [ Uncaught Error: Dispatch.dispatch(...): Cannot dispatch in the middle of a dispatch. ].
PageActions.addNotification(
'Your message was successfully submitted to ' + this.props.author.name,
'messageSubmitSucceed'
);
}.bind(this),
100
);
}
);
}
onSubmitFail(response) {
this.setState(
{
isSending: false,
},
function () {
console.log(response);
setTimeout(
function () {
// FIXME: Without delay creates conflict [ Uncaught Error: Dispatch.dispatch(...): Cannot dispatch in the middle of a dispatch. ].
PageActions.addNotification('Your message failed to submit. Please try again', 'messageSubmitFailed');
}.bind(this),
100
);
}
);
}
onSubmit(ev) {
if (this.state.isSending || '' === this.state.subject || '' === this.state.body) {
return;
}
ev.preventDefault();
ev.stopPropagation();
this.setState(
{
isSending: true,
},
function () {
const url = ApiUrlContext._currentValue.users + '/' + this.props.author.username + '/contact';
postRequest(
url,
{
subject: this.state.subject,
body: this.state.body,
},
{
headers: {
'X-CSRFToken': csrfToken(),
},
},
false,
this.onSubmitSuccess,
this.onSubmitFail
);
}
);
}
render() {
return (
);
}
}
export class ProfileAboutPage extends ProfileMediaPage {
constructor(props) {
super(props, 'author-about');
this.userIsAuthor = null;
this.enabledContactForm = false;
}
pageContent() {
let description = null;
let details = [];
let socialMedia = [];
if (this.state.author) {
if (null === this.userIsAuthor) {
if (MemberContext._currentValue.is.anonymous) {
this.userIsAuthor = false;
this.enabledContactForm = false;
} else {
this.userIsAuthor = ProfilePageStore.get('author-data').username === MemberContext._currentValue.username;
this.enabledContactForm = !this.userIsAuthor && MemberContext._currentValue.can.contactUser;
}
}
let i;
if (
void 0 !== this.state.author.description &&
!!this.state.author.description &&
'' !== this.state.author.description
) {
description = this.state.author.description;
}
if (void 0 !== this.state.author.location_info && this.state.author.location_info.length) {
let locations = [];
i = 0;
while (i < this.state.author.location_info.length) {
if (
void 0 !== this.state.author.location_info[i].title &&
void 0 !== this.state.author.location_info[i].url
) {
locations.push(
{this.state.author.location_info[i].title}
);
}
i += 1;
}
details.push(
Location:
{locations}
);
} else if (
void 0 !== this.state.author.location &&
!!this.state.author.location &&
'' !== this.state.author.location
) {
// TODO: Remove it, doesn't really need. Remains for backward compatibility.
details.push(
Location:
{this.state.author.location}
);
}
let lnk;
if (
void 0 !== this.state.author.home_page &&
!!this.state.author.home_page &&
'' !== this.state.author.home_page
) {
lnk = UrlParse(this.state.author.home_page.trim()).toString();
if ('' !== lnk) {
details.push(
Website:
{lnk}
);
}
}
if (
void 0 !== this.state.author.social_media_links &&
!!this.state.author.social_media_links &&
'' !== this.state.author.social_media_links
) {
let socialMediaLinks = this.state.author.social_media_links.split(',');
if (socialMediaLinks.length) {
i = 0;
while (i < socialMediaLinks.length) {
lnk = socialMediaLinks[i].trim();
if ('' !== lnk) {
socialMedia.push({lnk});
}
i += 1;
}
details.push(
Social media:
{socialMedia}
);
}
}
}
return [
this.state.author ? (
) : null,
this.state.author ? (
{null === description && 0 < details.length ? null : PageStore.get('config-options').pages.profile
.htmlInDescription ? (
) : (
{description}
)}
{!details.length ? null : (
)}
{this.enabledContactForm ?
: null}
) : null,
];
}
}
ProfileAboutPage.propTypes = {
title: PropTypes.string.isRequired,
};
ProfileAboutPage.defaultProps = {
title: 'Biography',
};