this.props.closeCard()}
diff --git a/app/src/main/js/components/utilities/post_rant.js b/app/src/main/js/components/utilities/post_rant.js
index b80bf9ff..2a46ebb6 100644
--- a/app/src/main/js/components/utilities/post_rant.js
+++ b/app/src/main/js/components/utilities/post_rant.js
@@ -5,10 +5,17 @@ import Twemoji from 'twemoji';
import rantscript from '../../consts/rantscript';
import EmojiPicker from '../emoji_picker/emoji_picker';
+const electron = require('electron');
+
const HELPER_TYPES = {
EMOJI: 'EMOJI',
};
+function replaceAll(str, search, replacement) {
+ const target = str;
+ return target.replace(new RegExp(search, 'g'), replacement);
+}
+
class PostRant extends Component {
constructor() {
super();
@@ -16,27 +23,86 @@ class PostRant extends Component {
rant_content: '',
tags: '',
posting: false,
- limitCrossed: false,
+ limitCrossed: null,
activeHelper: null,
+ image: null,
};
}
+ static parseHtml(string) {
+ let str = string;
+ const parser = new DOMParser();
+ str = replaceAll(str, '
', '\n\r');
+ const imgtags = str.match(/<(img+)\s+\w+.*?>/g);
+ let parsedStr = '';
+ if (imgtags === null) {
+ return parser.parseFromString(` ${str}`, 'text/html').body.textContent;
+ }
+ for (let i = 0; i < imgtags.length; i += 1) {
+ parsedStr = str.replace(imgtags[i], imgtags[i].match(/alt="(.*?)"/)[1]);
+ }
+ return parser.parseFromString(` ${parsedStr}`, 'text/html').body.textContent;
+ }
+
onPost() {
const { auth } = this.props;
this.setState({ posting: true });
- const rantText = document.getElementById('post_rant_content').innerHTML;
- rantscript
- .postRant(rantText, this.state.tags, auth.user.authToken)
- .then((res) => {
- if (!res.success) {
- this.setState({ limitCrossed: true });
- }
- this.setState({ posting: false, rant_content: '', tags: '' });
- })
- .catch(() => {
- this.setState({ posting: false });
- });
+ const rantText = PostRant.parseHtml(document.getElementById('post_rant_content').innerHTML);
+ if (this.state.image !== null) {
+ rantscript
+ .postRant(rantText, this.state.tags, auth.user.authToken, this.state.image)
+ .then((res) => {
+ if (!res.success) {
+ this.setState({ limitCrossed: res.error });
+ return;
+ }
+ this.setState({
+ posting: false,
+ rant_content: '',
+ tags: '',
+ limitCrossed: null,
+ });
+ this.props.close();
+ })
+ .catch(() => {
+ this.setState({ posting: false });
+ });
+ } else {
+ rantscript
+ .postRant(rantText, this.state.tags, auth.user.authToken)
+ .then((res) => {
+ if (!res.success) {
+ this.setState({ limitCrossed: res.error });
+ return;
+ }
+ this.setState({
+ posting: false,
+ rant_content: '',
+ tags: '',
+ limitCrossed: null,
+ });
+ this.props.close();
+ })
+ .catch(() => {
+ this.setState({ posting: false });
+ });
+ }
+ }
+
+ selectImage() {
+ if (this.state.image !== null) {
+ this.setState({ image: null });
+ } else {
+ const { dialog } = electron.remote;
+ dialog.showOpenDialog({
+ title: 'Upload image',
+ filters: [{ name: 'Images', extensions: ['jpg', 'png', 'gif'] }],
+ }, (image) => {
+ this.setState({ image: image[0] });
+ });
+ }
}
+
toggleEmoji() {
if (this.state.activeHelper === HELPER_TYPES.EMOJI) {
this.setState({ activeHelper: null });
@@ -44,6 +110,7 @@ class PostRant extends Component {
}
this.setState({ activeHelper: HELPER_TYPES.EMOJI });
}
+
static addEmoji(emoji) {
const div = document.getElementById('post_rant_content');
@@ -93,15 +160,18 @@ class PostRant extends Component {
className="tags"
/>
+ this.selectImage()}
+ >
+ {this.state.image === null && 'Add Image'}
+ {this.state.image !== null && 'Remove Image'}
+
this.onPost()}
disabled={this.state.posting}
>Post Rant
- { this.state.limitCrossed ?
Right now you can only add 1 rant every 2 hours
- (every 1 hour for devRant++ members) because we want
- to make sure everyones content gets good exposure! Please contact
- info@devrant.io if you have any questions :)
: null}
+
{this.state.limitCrossed || ''}