From c1c6341ddf59bf8eea8fcacfd3e031a79848fcc0 Mon Sep 17 00:00:00 2001 From: Apostolof Date: Thu, 19 Nov 2020 20:38:56 +0200 Subject: [PATCH] Move all contract and event names to constants --- .../src/components/LoadingContainer.jsx | 5 ++-- .../components/PostList/PostListRow/index.jsx | 3 ++- .../src/components/PostList/index.jsx | 3 ++- .../TopicList/TopicListRow/index.jsx | 3 ++- .../src/components/TopicList/index.jsx | 3 ++- .../src/constants/ContractNames.js | 2 +- .../src/constants/ForumContractEvents.js | 13 ++++++++++ .../src/options/drizzleOptions.js | 4 +++- .../src/redux/actions/contractEventActions.js | 24 +++++++++++++++---- .../src/redux/sagas/eventSaga.js | 3 ++- .../concordia-app/src/redux/sagas/userSaga.js | 4 ++-- .../concordia-app/src/views/Home/index.jsx | 5 ++-- .../src/views/Register/SignUpStep/index.jsx | 5 ++-- .../src/views/Topic/TopicCreate/index.jsx | 6 +++-- .../src/views/Topic/TopicView/index.jsx | 5 ++-- 15 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 packages/concordia-app/src/constants/ForumContractEvents.js diff --git a/packages/concordia-app/src/components/LoadingContainer.jsx b/packages/concordia-app/src/components/LoadingContainer.jsx index 6add0ba..c44c468 100644 --- a/packages/concordia-app/src/components/LoadingContainer.jsx +++ b/packages/concordia-app/src/components/LoadingContainer.jsx @@ -5,6 +5,7 @@ import LoadingComponent from './LoadingComponent'; // CSS import '../assets/css/loading-component.css'; +import { FORUM_CONTRACT } from '../constants/ContractNames'; const LoadingContainer = ({ children }) => { const initializing = useSelector((state) => state.drizzleStatus.initializing); @@ -15,8 +16,8 @@ const LoadingContainer = ({ children }) => { const web3NetworkId = useSelector((state) => state.web3.networkId); const web3NetworkFailed = useSelector((state) => state.web3.networkFailed); const web3AccountsFailed = useSelector((state) => state.web3.accountsFailed); - const contractInitialized = useSelector((state) => state.contracts.Forum.initialized); - const contractDeployed = useSelector((state) => state.contracts.Forum.deployed); + const contractInitialized = useSelector((state) => state.contracts[FORUM_CONTRACT].initialized); + const contractDeployed = useSelector((state) => state.contracts[FORUM_CONTRACT].deployed); const userFetched = useSelector((state) => state.user.address); if ((web3Status === 'initializing' || !web3NetworkId) diff --git a/packages/concordia-app/src/components/PostList/PostListRow/index.jsx b/packages/concordia-app/src/components/PostList/PostListRow/index.jsx index 0fff102..af1a69a 100644 --- a/packages/concordia-app/src/components/PostList/PostListRow/index.jsx +++ b/packages/concordia-app/src/components/PostList/PostListRow/index.jsx @@ -16,12 +16,13 @@ import { POSTS_DATABASE, USER_DATABASE } from '../../../constants/OrbitDatabases import determineKVAddress from '../../../utils/orbitUtils'; import { USER_PROFILE_PICTURE } from '../../../constants/UserDatabaseKeys'; import { POST_CONTENT, POST_SUBJECT } from '../../../constants/PostsDatabaseKeys'; +import { FORUM_CONTRACT } from '../../../constants/ContractNames'; const { orbit } = breeze; const PostListRow = (props) => { const { id: postId, postCallHash, loading } = props; - const getPostResults = useSelector((state) => state.contracts.Forum.getPost); + const getPostResults = useSelector((state) => state.contracts[FORUM_CONTRACT].getPost); const [postAuthorAddress, setPostAuthorAddress] = useState(null); const [postAuthor, setPostAuthor] = useState(null); const [timeAgo, setTimeAgo] = useState(null); diff --git a/packages/concordia-app/src/components/PostList/index.jsx b/packages/concordia-app/src/components/PostList/index.jsx index 991fc2f..c5c8130 100644 --- a/packages/concordia-app/src/components/PostList/index.jsx +++ b/packages/concordia-app/src/components/PostList/index.jsx @@ -6,8 +6,9 @@ import { useSelector } from 'react-redux'; import { Dimmer, Feed, Loader } from 'semantic-ui-react'; import PostListRow from './PostListRow'; import { drizzle } from '../../redux/store'; +import { FORUM_CONTRACT } from '../../constants/ContractNames'; -const { contracts: { Forum: { methods: { getPost: { cacheCall: getPostChainData } } } } } = drizzle; +const { contracts: { [FORUM_CONTRACT]: { methods: { getPost: { cacheCall: getPostChainData } } } } } = drizzle; const PostList = (props) => { const { postIds, loading } = props; diff --git a/packages/concordia-app/src/components/TopicList/TopicListRow/index.jsx b/packages/concordia-app/src/components/TopicList/TopicListRow/index.jsx index 4e6196d..05974ee 100644 --- a/packages/concordia-app/src/components/TopicList/TopicListRow/index.jsx +++ b/packages/concordia-app/src/components/TopicList/TopicListRow/index.jsx @@ -16,12 +16,13 @@ import { TOPICS_DATABASE, USER_DATABASE } from '../../../constants/OrbitDatabase import determineKVAddress from '../../../utils/orbitUtils'; import { USER_PROFILE_PICTURE } from '../../../constants/UserDatabaseKeys'; import { TOPIC_SUBJECT } from '../../../constants/TopicsDatabaseKeys'; +import { FORUM_CONTRACT } from '../../../constants/ContractNames'; const { orbit } = breeze; const TopicListRow = (props) => { const { id: topicId, topicCallHash, loading } = props; - const getTopicResults = useSelector((state) => state.contracts.Forum.getTopic); + const getTopicResults = useSelector((state) => state.contracts[FORUM_CONTRACT].getTopic); const [numberOfReplies, setNumberOfReplies] = useState(null); const [topicAuthorAddress, setTopicAuthorAddress] = useState(null); const [topicAuthor, setTopicAuthor] = useState(null); diff --git a/packages/concordia-app/src/components/TopicList/index.jsx b/packages/concordia-app/src/components/TopicList/index.jsx index 511ae2b..e171caf 100644 --- a/packages/concordia-app/src/components/TopicList/index.jsx +++ b/packages/concordia-app/src/components/TopicList/index.jsx @@ -6,8 +6,9 @@ import { useSelector } from 'react-redux'; import { List } from 'semantic-ui-react'; import TopicListRow from './TopicListRow'; import { drizzle } from '../../redux/store'; +import { FORUM_CONTRACT } from '../../constants/ContractNames'; -const { contracts: { Forum: { methods: { getTopic: { cacheCall: getTopicChainData } } } } } = drizzle; +const { contracts: { [FORUM_CONTRACT]: { methods: { getTopic: { cacheCall: getTopicChainData } } } } } = drizzle; const TopicList = (props) => { const { topicIds } = props; diff --git a/packages/concordia-app/src/constants/ContractNames.js b/packages/concordia-app/src/constants/ContractNames.js index bfb65b4..95fe6f7 100644 --- a/packages/concordia-app/src/constants/ContractNames.js +++ b/packages/concordia-app/src/constants/ContractNames.js @@ -1 +1 @@ -export const FORUM = 'Forum'; +export const FORUM_CONTRACT = 'Forum'; diff --git a/packages/concordia-app/src/constants/ForumContractEvents.js b/packages/concordia-app/src/constants/ForumContractEvents.js new file mode 100644 index 0000000..600a80a --- /dev/null +++ b/packages/concordia-app/src/constants/ForumContractEvents.js @@ -0,0 +1,13 @@ +export const USER_SIGNED_UP_EVENT = 'USER_SIGNED_UP'; +export const USERNAME_UPDATED_EVENT = 'USERNAME_UPDATED'; +export const TOPIC_CREATED_EVENT = 'TOPIC_CREATED'; +export const POST_CREATED_EVENT = 'POST_CREATED'; + +const forumContractEvents = [ + USER_SIGNED_UP_EVENT, + USERNAME_UPDATED_EVENT, + TOPIC_CREATED_EVENT, + POST_CREATED_EVENT, +]; + +export default forumContractEvents; diff --git a/packages/concordia-app/src/options/drizzleOptions.js b/packages/concordia-app/src/options/drizzleOptions.js index aa563f7..80dd30c 100644 --- a/packages/concordia-app/src/options/drizzleOptions.js +++ b/packages/concordia-app/src/options/drizzleOptions.js @@ -1,6 +1,8 @@ // See also: https://truffleframework.com/docs/drizzle/reference/drizzle-options import { contracts } from 'concordia-contracts'; import web3Options from './web3Options'; +import forumContractEvents from '../constants/ForumContractEvents'; +import { FORUM_CONTRACT } from '../constants/ContractNames'; const drizzleOptions = { web3: { @@ -8,7 +10,7 @@ const drizzleOptions = { }, contracts, events: { - Forum: ['UserSignedUp', 'UsernameUpdated', 'TopicCreated', 'PostCreated'], + [FORUM_CONTRACT]: forumContractEvents, }, reloadWindowOnNetworkChange: true, reloadWindowOnAccountChange: true, // We need it to reinitialize breeze and create new Orbit databases diff --git a/packages/concordia-app/src/redux/actions/contractEventActions.js b/packages/concordia-app/src/redux/actions/contractEventActions.js index 9c54288..1dd5be1 100644 --- a/packages/concordia-app/src/redux/actions/contractEventActions.js +++ b/packages/concordia-app/src/redux/actions/contractEventActions.js @@ -1,4 +1,20 @@ -export const FORUM_EVENT_USER_SIGNED_UP = 'UserSignedUp'; -export const FORUM_EVENT_USERNAME_UPDATED = 'UsernameUpdated'; -export const FORUM_EVENT_TOPIC_CREATED = 'TopicCreated'; -export const FORUM_EVENT_POST_CREATED = 'PostCreated'; +import { + POST_CREATED_EVENT, + TOPIC_CREATED_EVENT, + USER_SIGNED_UP_EVENT, + USERNAME_UPDATED_EVENT, +} from '../../constants/ForumContractEvents'; + +export const FORUM_EVENT_USER_SIGNED_UP = 'FORUM_EVENT_USER_SIGNED_UP'; +export const FORUM_EVENT_USERNAME_UPDATED = 'FORUM_EVENT_USERNAME_UPDATED'; +export const FORUM_EVENT_TOPIC_CREATED = 'FORUM_EVENT_TOPIC_CREATED'; +export const FORUM_EVENT_POST_CREATED = 'FORUM_EVENT_POST_CREATED'; + +const eventActionMap = { + [USER_SIGNED_UP_EVENT]: FORUM_EVENT_USER_SIGNED_UP, + [USERNAME_UPDATED_EVENT]: FORUM_EVENT_USERNAME_UPDATED, + [TOPIC_CREATED_EVENT]: FORUM_EVENT_TOPIC_CREATED, + [POST_CREATED_EVENT]: FORUM_EVENT_POST_CREATED, +}; + +export default eventActionMap; diff --git a/packages/concordia-app/src/redux/sagas/eventSaga.js b/packages/concordia-app/src/redux/sagas/eventSaga.js index 5b43d1c..b6017c6 100644 --- a/packages/concordia-app/src/redux/sagas/eventSaga.js +++ b/packages/concordia-app/src/redux/sagas/eventSaga.js @@ -1,8 +1,9 @@ import { put, takeEvery } from 'redux-saga/effects'; import { CONTRACT_EVENT_FIRED } from '@ezerous/drizzle/src/contracts/constants'; +import eventActionMap from '../actions/contractEventActions'; function* eventBreakDown({ event }) { - yield put({ type: event.event, event: { ...event } }); + yield put({ type: eventActionMap[event.event], event: { ...event } }); } function* eventSaga() { diff --git a/packages/concordia-app/src/redux/sagas/userSaga.js b/packages/concordia-app/src/redux/sagas/userSaga.js index 92cc840..f436914 100644 --- a/packages/concordia-app/src/redux/sagas/userSaga.js +++ b/packages/concordia-app/src/redux/sagas/userSaga.js @@ -2,13 +2,13 @@ import { all, call, put, take, takeLatest, } from 'redux-saga/effects'; - import { drizzleActions } from '@ezerous/drizzle'; import { USER_DATA_UPDATED, USER_DATA_ERROR } from '../actions/userActions'; import { FORUM_EVENT_USER_SIGNED_UP } from '../actions/contractEventActions'; +import { FORUM_CONTRACT } from '../../constants/ContractNames'; function* fetchUserData({ drizzle, account }) { - const contract = drizzle.contracts.Forum; + const contract = drizzle.contracts[FORUM_CONTRACT]; const transaction = yield call(contract.methods.hasUserSignedUp, account); try { diff --git a/packages/concordia-app/src/views/Home/index.jsx b/packages/concordia-app/src/views/Home/index.jsx index ce9decf..3272cfd 100644 --- a/packages/concordia-app/src/views/Home/index.jsx +++ b/packages/concordia-app/src/views/Home/index.jsx @@ -6,12 +6,13 @@ import { useSelector } from 'react-redux'; import Board from './Board'; import './styles.css'; import { drizzle } from '../../redux/store'; +import { FORUM_CONTRACT } from '../../constants/ContractNames'; -const { contracts: { Forum: { methods: { getNumberOfTopics } } } } = drizzle; +const { contracts: { [FORUM_CONTRACT]: { methods: { getNumberOfTopics } } } } = drizzle; const Home = () => { const [numberOfTopicsCallHash, setNumberOfTopicsCallHash] = useState(''); - const getNumberOfTopicsResults = useSelector((state) => state.contracts.Forum.getNumberOfTopics); + const getNumberOfTopicsResults = useSelector((state) => state.contracts[FORUM_CONTRACT].getNumberOfTopics); useEffect(() => { setNumberOfTopicsCallHash(getNumberOfTopics.cacheCall()); diff --git a/packages/concordia-app/src/views/Register/SignUpStep/index.jsx b/packages/concordia-app/src/views/Register/SignUpStep/index.jsx index 006ce04..948b3ae 100644 --- a/packages/concordia-app/src/views/Register/SignUpStep/index.jsx +++ b/packages/concordia-app/src/views/Register/SignUpStep/index.jsx @@ -11,13 +11,14 @@ import { useHistory } from 'react-router'; import PropTypes from 'prop-types'; import { drizzle } from '../../../redux/store'; import { TRANSACTION_ERROR, TRANSACTION_SUCCESS } from '../../../constants/TransactionStatus'; +import { FORUM_CONTRACT } from '../../../constants/ContractNames'; -const { contracts: { Forum: { methods: { isUserNameTaken, signUp } } } } = drizzle; +const { contracts: { [FORUM_CONTRACT]: { methods: { isUserNameTaken, signUp } } } } = drizzle; const SignUpStep = (props) => { const { pushNextStep, account } = props; const user = useSelector((state) => state.user); - const isUserNameTakenResults = useSelector((state) => state.contracts.Forum.isUserNameTaken); + const isUserNameTakenResults = useSelector((state) => state.contracts[FORUM_CONTRACT].isUserNameTaken); const transactionStack = useSelector((state) => state.transactionStack); const transactions = useSelector((state) => state.transactions); const [usernameInput, setUsernameInput] = useState(''); diff --git a/packages/concordia-app/src/views/Topic/TopicCreate/index.jsx b/packages/concordia-app/src/views/Topic/TopicCreate/index.jsx index 224f11b..b6106e6 100644 --- a/packages/concordia-app/src/views/Topic/TopicCreate/index.jsx +++ b/packages/concordia-app/src/views/Topic/TopicCreate/index.jsx @@ -13,8 +13,10 @@ import { TRANSACTION_ERROR, TRANSACTION_SUCCESS } from '../../../constants/Trans import { POSTS_DATABASE, TOPICS_DATABASE } from '../../../constants/OrbitDatabases'; import { TOPIC_SUBJECT } from '../../../constants/TopicsDatabaseKeys'; import { POST_CONTENT, POST_SUBJECT } from '../../../constants/PostsDatabaseKeys'; +import { FORUM_CONTRACT } from '../../../constants/ContractNames'; +import { TOPIC_CREATED_EVENT } from '../../../constants/ForumContractEvents'; -const { contracts: { Forum: { methods: { createTopic } } } } = drizzle; +const { contracts: { [FORUM_CONTRACT]: { methods: { createTopic } } } } = drizzle; const { orbit: { stores } } = breeze; const TopicCreate = (props) => { @@ -57,7 +59,7 @@ const TopicCreate = (props) => { const { receipt: { events: { - TopicCreated: { + [TOPIC_CREATED_EVENT]: { returnValues: { topicID: topicId, postID: postId, diff --git a/packages/concordia-app/src/views/Topic/TopicView/index.jsx b/packages/concordia-app/src/views/Topic/TopicView/index.jsx index 943b804..5f747c2 100644 --- a/packages/concordia-app/src/views/Topic/TopicView/index.jsx +++ b/packages/concordia-app/src/views/Topic/TopicView/index.jsx @@ -14,8 +14,9 @@ import determineKVAddress from '../../../utils/orbitUtils'; import { USER_PROFILE_PICTURE } from '../../../constants/UserDatabaseKeys'; import { TOPIC_SUBJECT } from '../../../constants/TopicsDatabaseKeys'; import PostCreate from '../../../components/PostCreate'; +import { FORUM_CONTRACT } from '../../../constants/ContractNames'; -const { contracts: { Forum: { methods: { getTopic: { cacheCall: getTopicChainData } } } } } = drizzle; +const { contracts: { [FORUM_CONTRACT]: { methods: { getTopic: { cacheCall: getTopicChainData } } } } } = drizzle; const { orbit } = breeze; const TopicView = (props) => { @@ -26,7 +27,7 @@ const TopicView = (props) => { const drizzleInitialized = useSelector((state) => state.drizzleStatus.initialized); const drizzleInitializationFailed = useSelector((state) => state.drizzleStatus.failed); const userAddress = useSelector((state) => state.user.address); - const getTopicResults = useSelector((state) => state.contracts.Forum.getTopic); + const getTopicResults = useSelector((state) => state.contracts[FORUM_CONTRACT].getTopic); const topics = useSelector((state) => state.orbitData.topics); const users = useSelector((state) => state.orbitData.users); const [getTopicCallHash, setGetTopicCallHash] = useState([]);