|
|
@ -8,20 +8,31 @@ import PropTypes from 'prop-types'; |
|
|
|
import { useTranslation } from 'react-i18next'; |
|
|
|
import { useDispatch, useSelector } from 'react-redux'; |
|
|
|
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 { USER_PROFILE_PICTURE } from '../../constants/UserDatabaseKeys'; |
|
|
|
import { breeze } from '../../redux/store'; |
|
|
|
import { breeze, drizzle } from '../../redux/store'; |
|
|
|
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 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 [postContent, setPostContent] = 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 [storingPost, setStoringPost] = useState(false); |
|
|
|
const userAddress = useSelector((state) => state.user.address); |
|
|
|
const users = useSelector((state) => state.orbitData.users); |
|
|
|
const dispatch = useDispatch(); |
|
|
@ -68,13 +79,58 @@ const PostCreate = (props) => { |
|
|
|
} |
|
|
|
}, [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(() => { |
|
|
|
if (postSubject === '' || postContent === '') { |
|
|
|
if (postSubject === '') { |
|
|
|
setPostSubjectInputEmptySubmit(true); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
if (postContent === '') { |
|
|
|
setPostContentInputEmptySubmit(true); |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
setPosting(true); |
|
|
|
}, [postContent, postSubject]); |
|
|
|
setCreatePostCacheSendStackId(createPost.cacheSend(...[topicId], { from: account })); |
|
|
|
}, [account, postContent, postSubject, topicId]); |
|
|
|
|
|
|
|
return ( |
|
|
|
<Feed> |
|
|
@ -93,7 +149,6 @@ const PostCreate = (props) => { |
|
|
|
size="big" |
|
|
|
inverted |
|
|
|
color="black" |
|
|
|
verticalAlign="middle" |
|
|
|
/> |
|
|
|
)} |
|
|
|
</Feed.Label> |
|
|
@ -105,11 +160,12 @@ const PostCreate = (props) => { |
|
|
|
name="postSubject" |
|
|
|
className="subject-input" |
|
|
|
size="mini" |
|
|
|
error={postSubjectInputEmptySubmit} |
|
|
|
value={postSubject} |
|
|
|
onChange={handleInputChange} |
|
|
|
/> |
|
|
|
<span className="post-summary-meta-index"> |
|
|
|
{t('post.list.row.post.id', { id: postId })} |
|
|
|
{t('post.list.row.post.id', { id: postIndexInTopic })} |
|
|
|
</span> |
|
|
|
</div> |
|
|
|
</Feed.Summary> |
|
|
@ -121,6 +177,7 @@ const PostCreate = (props) => { |
|
|
|
className="content-input" |
|
|
|
size="mini" |
|
|
|
rows={4} |
|
|
|
error={postContentInputEmptySubmit} |
|
|
|
value={postContent} |
|
|
|
onChange={handleInputChange} |
|
|
|
/> |
|
|
@ -133,8 +190,8 @@ const PostCreate = (props) => { |
|
|
|
animated |
|
|
|
type="button" |
|
|
|
color="green" |
|
|
|
disabled={posting} |
|
|
|
onClick={savePost || postSubject === '' || postContent === ''} |
|
|
|
disabled={posting || postSubject === '' || postContent === ''} |
|
|
|
onClick={savePost} |
|
|
|
> |
|
|
|
<Button.Content visible> |
|
|
|
{t('post.create.form.send.button')} |
|
|
@ -152,7 +209,8 @@ const PostCreate = (props) => { |
|
|
|
}; |
|
|
|
|
|
|
|
PostCreate.propTypes = { |
|
|
|
id: PropTypes.number.isRequired, |
|
|
|
topicId: PropTypes.number.isRequired, |
|
|
|
postIndexInTopic: PropTypes.number.isRequired, |
|
|
|
initialPostSubject: PropTypes.string.isRequired, |
|
|
|
}; |
|
|
|
|
|
|
|