import React from 'react'
import * as antd from 'antd'
import * as ycore from 'ycore'
import styles from './index.less'
import * as Icons from '@ant-design/icons';
import Icon from '@ant-design/icons'
import $ from 'jquery'
import * as MICONS from '@material-ui/icons'
import Post_options from './local_components/post_options'
import {toogleOptionsBox} from './local_components/post_options'
function getBase64(img, callback) {
const reader = new FileReader();
reader.addEventListener('load', () => callback(reader.result));
reader.readAsDataURL(img);
}
export function HandleVisibility(){
window.PostCreatorComponent.ToogleVisibility();
return
}
class PostCreator extends React.PureComponent{
constructor(props){
super(props),
window.PostCreatorComponent = this;
this.state = {
visible: true,
FadeIN: true,
keys_remaining: ycore.AppSettings.MaxLengthPosts,
rawtext: '',
posting: false,
posting_ok: false,
shareWith: 'any',
uploader: false,
Schedule: false,
}
}
renderPostPlayer(payload){
const {file, fileURL} = this.state
const videofilter = file.type === 'video/mp4'
const imagefilter = file.type === 'image/jpeg' || file.type === 'image/png'
if (imagefilter) {
return (
this.handleDeleteFile()} icon={} />
)
}
if (videofilter) {
return (
this.handleDeleteFile()} icon={} />
)
}
return null
}
ToogleVisibility(){
this.setState({ visible: !this.state.visible })
}
ToogleUpload(){
this.setState({ uploader: !this.state.uploader })
}
handleDeleteFile = () =>{
this.setState({ fileURL: null })
}
handleFileUpload = info => {
if (info.file.status === 'uploading') {
this.setState({ loading: true, });
return;
}
if (info.file.status === 'done') {
this.setState({ file: info.file.originFileObj, uploader: false })
getBase64(info.file.originFileObj, fileURL =>{
this.setState({fileURL,loading: false})
});
}
};
beforeUpload = (file) => {
const filter = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/jpg' || file.type === 'image/gif' || file.type === 'video/mp4';
if (!filter) {
antd.message.error(`${file.type} This file is not valid!`);
}
const maxsize = file.size / 1024 / 1024 < ycore.AppSettings.MaximunAPIPayload;
if (!maxsize) {
antd.message.error(`Image must smaller than ${ycore.AppSettings.MaximunAPIPayload} KB!`);
}
return filter && maxsize;
}
handleChanges = ({ target: { value } }) => {
this.setState({ rawtext: value, keys_remaining: (ycore.AppSettings.MaxLengthPosts - value.length) })
}
handleKeysProgressBar(){
const { keys_remaining } = this.state;
if (keys_remaining <= (ycore.AppSettings.MaxLengthPosts/100*30)) {
return 'exception'
}else return('active')
}
handleToggleToolbox = () =>{
toogleOptionsBox()
}
FlushPostState(){
this.setState({
posting_ok: true,
posting: false,
rawtext: '',
fileURL: '',
file: ''
})
setTimeout( () => {this.setState({ posting_ok: false }) }, 1000)
ycore.FeedHandler.refresh()
return true
}
handlePublishPost = (e) => {
const { rawtext, shareWith, file } = this.state;
if(!rawtext){
return null
}
this.setState({ posting: true, keys_remaining: ycore.AppSettings.MaxLengthPosts })
const payload = { privacy: ycore.GetPostPrivacy.bool(shareWith), text: rawtext, file: file }
ycore.comty_post.new((err,res)=>{
if (err) {
ycore.notify.error(err)
return false
}
this.FlushPostState()
},payload)
}
dropRef = React.createRef()
handleDragIn = (e) => {
e.preventDefault()
e.stopPropagation()
if (this.state.uploader == true) {
return
}
this.setState({ uploader: true })
}
handleDragOut = (e) => {
e.preventDefault()
e.stopPropagation()
if (this.state.uploader == false) {
return
}
this.setState({ uploader: false })
}
componentDidMount() {
const _this = this
$("body").bind('paste', function(je){
var e = je.originalEvent;
for (var i = 0; i < e.clipboardData.items.length; i++) {
var item = e.clipboardData.items[i];
ycore.yconsole.log('Item: ' + item.type);
if (item.type.indexOf('image') != -1) {
//item.
let a = item.getAsFile()
a
_this.setState({ file: a })
ycore.ReadFileAsB64(a, (res) =>{
_this.setState({ fileURL: res })
})
} else {
// ignore not images
ycore.yconsole.log('Discarding not image paste data');
}
}
}
)
let div = this.dropRef.current
div.addEventListener('dragenter', this.handleDragIn)
div.addEventListener('dragleave', this.handleDragOut)
}
componentWillUnmount() {
let div = this.dropRef.current
div.removeEventListener('dragenter', this.handleDragIn)
div.removeEventListener('dragleave', this.handleDragOut)
}
canPost(){
const { fileURL, keys_remaining, } = this.state
const isTypedSomething = (keys_remaining < ycore.AppSettings.MaxLengthPosts)
const isUploadedFile = (fileURL? true : false)
return isUploadedFile || isTypedSomething
}
render(){
const {userData} = this.props
const { keys_remaining, visible, fileURL } = this.state;
const percent = (((keys_remaining/ycore.AppSettings.MaxLengthPosts) * 100).toFixed(2) )
const changeShare = ({ key }) => {
this.setState({ shareWith: key })
}
const shareOptionsMenu = (
{ycore.GetPostPrivacy.decorator("any")}
{ycore.GetPostPrivacy.decorator("only_follow")}
{ycore.GetPostPrivacy.decorator("only_followers")}
{ycore.GetPostPrivacy.decorator("private")}
)
if (visible) {
return(
{this.state.uploader?
Drop your file here o click for upload
: <>
: (this.state.posting?
:
)} />
> }
{fileURL? this.renderPostPlayer(this.state.fileURL) : null}
)
}
return null
}
}
export default PostCreator