|
@ -8,20 +8,31 @@ import PropTypes from 'prop-types'; |
|
|
import { useTranslation } from 'react-i18next'; |
|
|
import { useTranslation } from 'react-i18next'; |
|
|
import { useDispatch, useSelector } from 'react-redux'; |
|
|
import { useDispatch, useSelector } from 'react-redux'; |
|
|
import determineKVAddress from '../../utils/orbitUtils'; |
|
|
import determineKVAddress from '../../utils/orbitUtils'; |
|
|
import { USER_DATABASE } from '../../constants/OrbitDatabases'; |
|
|
import { POSTS_DATABASE, USER_DATABASE } from '../../constants/OrbitDatabases'; |
|
|
import { FETCH_USER_DATABASE } from '../../redux/actions/peerDbReplicationActions'; |
|
|
import { FETCH_USER_DATABASE } from '../../redux/actions/peerDbReplicationActions'; |
|
|
import { USER_PROFILE_PICTURE } from '../../constants/UserDatabaseKeys'; |
|
|
import { USER_PROFILE_PICTURE } from '../../constants/UserDatabaseKeys'; |
|
|
import { breeze } from '../../redux/store'; |
|
|
import { breeze, drizzle } from '../../redux/store'; |
|
|
import './styles.css'; |
|
|
import './styles.css'; |
|
|
|
|
|
import { TRANSACTION_ERROR, TRANSACTION_SUCCESS } from '../../constants/TransactionStatus'; |
|
|
|
|
|
import { POST_CONTENT, POST_SUBJECT } from '../../constants/PostsDatabaseKeys'; |
|
|
|
|
|
|
|
|
|
|
|
const { contracts: { Forum: { methods: { createPost } } } } = drizzle; |
|
|
const { orbit } = breeze; |
|
|
const { orbit } = breeze; |
|
|
|
|
|
|
|
|
const PostCreate = (props) => { |
|
|
const PostCreate = (props) => { |
|
|
const { id: postId, initialPostSubject } = props; |
|
|
const { |
|
|
|
|
|
topicId, postIndexInTopic, initialPostSubject, account, |
|
|
|
|
|
} = props; |
|
|
|
|
|
const transactionStack = useSelector((state) => state.transactionStack); |
|
|
|
|
|
const transactions = useSelector((state) => state.transactions); |
|
|
const [postSubject, setPostSubject] = useState(initialPostSubject); |
|
|
const [postSubject, setPostSubject] = useState(initialPostSubject); |
|
|
const [postContent, setPostContent] = useState(''); |
|
|
const [postContent, setPostContent] = useState(''); |
|
|
const [userProfilePictureUrl, setUserProfilePictureUrl] = useState(); |
|
|
const [userProfilePictureUrl, setUserProfilePictureUrl] = useState(); |
|
|
|
|
|
const [postSubjectInputEmptySubmit, setPostSubjectInputEmptySubmit] = useState(false); |
|
|
|
|
|
const [postContentInputEmptySubmit, setPostContentInputEmptySubmit] = useState(false); |
|
|
|
|
|
const [createPostCacheSendStackId, setCreatePostCacheSendStackId] = useState(''); |
|
|
const [posting, setPosting] = useState(false); |
|
|
const [posting, setPosting] = useState(false); |
|
|
|
|
|
const [storingPost, setStoringPost] = useState(false); |
|
|
const userAddress = useSelector((state) => state.user.address); |
|
|
const userAddress = useSelector((state) => state.user.address); |
|
|
const users = useSelector((state) => state.orbitData.users); |
|
|
const users = useSelector((state) => state.orbitData.users); |
|
|
const dispatch = useDispatch(); |
|
|
const dispatch = useDispatch(); |
|
@ -68,13 +79,58 @@ const PostCreate = (props) => { |
|
|
} |
|
|
} |
|
|
}, [posting]); |
|
|
}, [posting]); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
|
|
if (posting && !storingPost && transactionStack && transactionStack[createPostCacheSendStackId] |
|
|
|
|
|
&& transactions[transactionStack[createPostCacheSendStackId]]) { |
|
|
|
|
|
if (transactions[transactionStack[createPostCacheSendStackId]].status === TRANSACTION_ERROR) { |
|
|
|
|
|
setPosting(false); |
|
|
|
|
|
} else if (transactions[transactionStack[createPostCacheSendStackId]].status === TRANSACTION_SUCCESS) { |
|
|
|
|
|
const { |
|
|
|
|
|
receipt: { events: { PostCreated: { returnValues: { postID: contractPostId } } } }, |
|
|
|
|
|
} = transactions[transactionStack[createPostCacheSendStackId]]; |
|
|
|
|
|
|
|
|
|
|
|
const { stores } = orbit; |
|
|
|
|
|
const postsDb = Object.values(stores).find((store) => store.dbname === POSTS_DATABASE); |
|
|
|
|
|
|
|
|
|
|
|
postsDb |
|
|
|
|
|
.put(contractPostId, { |
|
|
|
|
|
[POST_SUBJECT]: postSubject, |
|
|
|
|
|
[POST_CONTENT]: postContent, |
|
|
|
|
|
}, { pin: true }) |
|
|
|
|
|
.then(() => { |
|
|
|
|
|
setPostSubject(initialPostSubject); |
|
|
|
|
|
setPostContent(''); |
|
|
|
|
|
setPosting(false); |
|
|
|
|
|
setPostSubjectInputEmptySubmit(false); |
|
|
|
|
|
setPostContentInputEmptySubmit(false); |
|
|
|
|
|
setCreatePostCacheSendStackId(''); |
|
|
|
|
|
}) |
|
|
|
|
|
.catch((reason) => { |
|
|
|
|
|
console.log(reason); |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
setStoringPost(true); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
}, [ |
|
|
|
|
|
createPostCacheSendStackId, initialPostSubject, postContent, postSubject, posting, storingPost, transactionStack, |
|
|
|
|
|
transactions, |
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
const savePost = useCallback(() => { |
|
|
const savePost = useCallback(() => { |
|
|
if (postSubject === '' || postContent === '') { |
|
|
if (postSubject === '') { |
|
|
|
|
|
setPostSubjectInputEmptySubmit(true); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (postContent === '') { |
|
|
|
|
|
setPostContentInputEmptySubmit(true); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
setPosting(true); |
|
|
setPosting(true); |
|
|
}, [postContent, postSubject]); |
|
|
setCreatePostCacheSendStackId(createPost.cacheSend(...[topicId], { from: account })); |
|
|
|
|
|
}, [account, postContent, postSubject, topicId]); |
|
|
|
|
|
|
|
|
return ( |
|
|
return ( |
|
|
<Feed> |
|
|
<Feed> |
|
@ -93,7 +149,6 @@ const PostCreate = (props) => { |
|
|
size="big" |
|
|
size="big" |
|
|
inverted |
|
|
inverted |
|
|
color="black" |
|
|
color="black" |
|
|
verticalAlign="middle" |
|
|
|
|
|
/> |
|
|
/> |
|
|
)} |
|
|
)} |
|
|
</Feed.Label> |
|
|
</Feed.Label> |
|
@ -105,11 +160,12 @@ const PostCreate = (props) => { |
|
|
name="postSubject" |
|
|
name="postSubject" |
|
|
className="subject-input" |
|
|
className="subject-input" |
|
|
size="mini" |
|
|
size="mini" |
|
|
|
|
|
error={postSubjectInputEmptySubmit} |
|
|
value={postSubject} |
|
|
value={postSubject} |
|
|
onChange={handleInputChange} |
|
|
onChange={handleInputChange} |
|
|
/> |
|
|
/> |
|
|
<span className="post-summary-meta-index"> |
|
|
<span className="post-summary-meta-index"> |
|
|
{t('post.list.row.post.id', { id: postId })} |
|
|
{t('post.list.row.post.id', { id: postIndexInTopic })} |
|
|
</span> |
|
|
</span> |
|
|
</div> |
|
|
</div> |
|
|
</Feed.Summary> |
|
|
</Feed.Summary> |
|
@ -121,6 +177,7 @@ const PostCreate = (props) => { |
|
|
className="content-input" |
|
|
className="content-input" |
|
|
size="mini" |
|
|
size="mini" |
|
|
rows={4} |
|
|
rows={4} |
|
|
|
|
|
error={postContentInputEmptySubmit} |
|
|
value={postContent} |
|
|
value={postContent} |
|
|
onChange={handleInputChange} |
|
|
onChange={handleInputChange} |
|
|
/> |
|
|
/> |
|
@ -133,8 +190,8 @@ const PostCreate = (props) => { |
|
|
animated |
|
|
animated |
|
|
type="button" |
|
|
type="button" |
|
|
color="green" |
|
|
color="green" |
|
|
disabled={posting} |
|
|
disabled={posting || postSubject === '' || postContent === ''} |
|
|
onClick={savePost || postSubject === '' || postContent === ''} |
|
|
onClick={savePost} |
|
|
> |
|
|
> |
|
|
<Button.Content visible> |
|
|
<Button.Content visible> |
|
|
{t('post.create.form.send.button')} |
|
|
{t('post.create.form.send.button')} |
|
@ -152,7 +209,8 @@ const PostCreate = (props) => { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
PostCreate.propTypes = { |
|
|
PostCreate.propTypes = { |
|
|
id: PropTypes.number.isRequired, |
|
|
topicId: PropTypes.number.isRequired, |
|
|
|
|
|
postIndexInTopic: PropTypes.number.isRequired, |
|
|
initialPostSubject: PropTypes.string.isRequired, |
|
|
initialPostSubject: PropTypes.string.isRequired, |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|