This repository was archived by the owner on Nov 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #94 from tahnik/rant_composer
Rant composer
- Loading branch information
Showing
9 changed files
with
4,683 additions
and
649 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,45 +5,112 @@ 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(); | ||
this.state = { | ||
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, '<br>', '\n\r'); | ||
const imgtags = str.match(/<(img+)\s+\w+.*?>/g); | ||
let parsedStr = ''; | ||
if (imgtags === null) { | ||
return parser.parseFromString(`<!doctype html><body> ${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(`<!doctype html><body> ${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 }); | ||
return; | ||
} | ||
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" | ||
/> | ||
<div className="post"> | ||
<button | ||
onClick={() => this.selectImage()} | ||
> | ||
{this.state.image === null && 'Add Image'} | ||
{this.state.image !== null && 'Remove Image'} | ||
</button> | ||
<button | ||
onClick={() => this.onPost()} | ||
disabled={this.state.posting} | ||
>Post Rant</button> | ||
</div> | ||
{ this.state.limitCrossed ? <p>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 | ||
[email protected] if you have any questions :)</p> : null} | ||
<p>{this.state.limitCrossed || ''}</p> | ||
</div> | ||
</div> | ||
</div> | ||
|
@@ -112,6 +182,7 @@ class PostRant extends Component { | |
|
||
PostRant.propTypes = { | ||
auth: PropTypes.object.isRequired, | ||
close: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default PostRant; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.