From c2af14a7774be07162495283e11d0a4f59781375 Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Sat, 9 Sep 2023 16:05:45 +1200 Subject: [PATCH 01/10] add poll upvoted and downvoted state --- screens/ViewPollPostPage.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/screens/ViewPollPostPage.js b/screens/ViewPollPostPage.js index a3188b0..ec71434 100644 --- a/screens/ViewPollPostPage.js +++ b/screens/ViewPollPostPage.js @@ -112,8 +112,10 @@ const ViewPollPostPage = ({route, navigation}) => { //change stuff const [pollUpOrDownVotes, setPollUpOrDownVotes] = useState("Finding") const [initialPollUpOrDownVotes, setInitialPollUpOrDownVotes] = useState("Finding") - const [pollUpOrDownVoted, setPollUpOrDownVoted] = useState("Finding") - const [initialPollUpOrDownVoted, setInitialPollUpOrDownVoted] = useState("Finding") + const [pollUpvoted, setPollUpvoted] = useState("Finding") + const [pollDownvoted, setPollDownvoted] = useState("Finding") + const [pollInitiallyUpvoted, setPollInitiallyUpvoted] = useState("Finding") + const [pollInitiallyDownvoted, setPollInitiallyDownvoted] = useState("Finding") const [pollVotesForOptions, setPollVotesForOptions] = useState({optionOne: "Finding", optionTwo: "Finding", optionThree: "Finding", optionFour: "Finding", optionFive: "Finding", optionSix: "Finding"}) const [initialPollVotesForOptions, setInitialPollVotesForOptions] = useState({optionOne: "Finding", optionTwo: "Finding", optionThree: "Finding", optionFour: "Finding", optionFive: "Finding", optionSix: "Finding"}) const [pollBarLengths, setPollBarLengths] = useState({optionOnesBarLength: 0, optionTwosBarLength: 0, optionThreesBarLength: 0, optionFoursBarLength: 0, optionFivesBarLength:0, optionSixesBarLength: 0}) @@ -246,8 +248,10 @@ const ViewPollPostPage = ({route, navigation}) => { const changeVoteTexts = (data) => { //set initial values const pollData = data.data[0] - setPollUpOrDownVoted(pollData.pollUpOrDownVoted) - setInitialPollUpOrDownVoted(pollData.pollUpOrDownVoted) + setPollUpvoted(pollData.upvoted) + setPollDownvoted(pollData.downvoted) + setPollInitiallyUpvoted(pollData.upvoted) + setPollInitiallyDownvoted(pollData.downvoted) setPollUpOrDownVotes(pollData.pollUpOrDownVotes) setPollVoteOption(pollData.votedFor) setPollinitialVoteOption(pollData.votedFor) From 739ef7bbd8647d561304597203c8fad80aaa206a Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Thu, 14 Sep 2023 18:03:48 +1200 Subject: [PATCH 02/10] Add PollWithVotes class --- components/Posts/PollPost.js | 2 + components/Posts/PollWithVotes.js | 359 ++++++++++++++++++++++++++++++ screens/ViewPollPostPage.js | 322 --------------------------- 3 files changed, 361 insertions(+), 322 deletions(-) create mode 100644 components/Posts/PollWithVotes.js diff --git a/components/Posts/PollPost.js b/components/Posts/PollPost.js index 44bf5ac..11eb87e 100644 --- a/components/Posts/PollPost.js +++ b/components/Posts/PollPost.js @@ -343,6 +343,8 @@ class Poll extends Component { } } +export const PollClass = Poll; + export default function(props) { const navigation = useNavigation(); const {serverUrl} = useContext(ServerUrlContext); diff --git a/components/Posts/PollWithVotes.js b/components/Posts/PollWithVotes.js new file mode 100644 index 0000000..2e64895 --- /dev/null +++ b/components/Posts/PollWithVotes.js @@ -0,0 +1,359 @@ +import React from 'react'; +import { PollClass } from './PollPost'; +import { ActivityIndicator } from 'react-native'; +import { PostsIconFrame } from '../../screens/screenStylings/styling'; +import { getTimeFromUTCMS } from '../../libraries/Time'; + +class PollWithVotes extends PollClass { + constructor(props) { + super(props); + } + + openOptionOne() { + alert('Coming soon') + } + + openOptionTwo() { + alert('Coming soon') + } + + openOptionThree() { + alert('Coming soon') + } + + openOptionFour() { + alert('Coming soon') + } + + openOptionFive() { + alert('Coming soon') + } + + openOptionSix() { + alert('Coming soon') + } + + optionOneInfoState = false; //Temporary + optionTwoInfoState = false; //Temporary + optionThreeInfoState = false; //Temporary + optionFourInfoState = false; //Temporary + optionFiveInfoState = false; //Temporary + optionSixInfoState = false; //Temporary + + shouldComponentUpdate(nextProps, nextState) { + const upvoteIsSame = nextProps.post.upvoted === this.props.post.upvoted; + const downvoteIsSame = nextProps.post.downvoted === this.props.post.downvoted; + const changingVoteIsSame = nextProps.post.changingVote === this.props.post.changingVote; + const colorsAreSame = nextProps.colorsIndexNum === this.props.colorsIndexNum; + const pollIdIsSame = nextProps.post.pollId === this.props.post.pollId; + const deletingIsSame = nextProps.post.deleting === this.props.post.deleting; + const profilePictureIsSame = nextProps.post.pfpB64 === this.props.post.pfpB64; + const voteOneSame = nextProps.post.optionOnesVotes === this.props.post.optionOnesVotes; + const voteTwoSame = nextProps.post.optionTwosVotes === this.props.post.optionTwosVotes; + const voteThreeSame = nextProps.post.optionThreesVotes === this.props.post.optionThreesVotes; + const voteFourSame = nextProps.post.optionFoursVotes === this.props.post.optionFoursVotes; + const voteFiveSame = nextProps.post.optionFivesVotes === this.props.post.optionFivesVotes; + const voteSixSame = nextProps.post.optionSixesVotes === this.props.post.optionSixesVotes; + + + if (upvoteIsSame && downvoteIsSame && changingVoteIsSame && colorsAreSame && pollIdIsSame && deletingIsSame && profilePictureIsSame && voteOneSame && voteTwoSame && voteThreeSame && voteFourSame && voteFiveSame && voteSixSame) return false; + + return true; + } + + render() { + return ( + <> + {this.props.post.deleting && + + Deleting... + + + } + + + + + + + {this.props.post.creatorDisplayName} + @{this.props.post.creatorName} + + + + {this.props.post.pollTitle || "Couldn't recieve poll title"} + + + {this.props.post.pollSubTitle || "Couldn't recieve poll subtitle"} + + + + 1 + + + 2 + + + 3 + + + 4 + + + 5 + + + 6 + + + + + + + + + + + + + + + + 1. {this.props.post.optionOne || "Couldn't load option one"} + + + + + + + + + Votes + + {this.props.post.optionOnesVotes} + + + {handleVoteOnPoll("optionOnesVotes")}}> + Option One + + {this.props.post.optionOne} + + + + Percent + + {this.props.post.optionOnesBarLength.toFixed(2)}% + + + + + + + + + + + 2. {this.props.post.optionTwo || "Couldn't load option two"} + + + + + + + + + Votes + + {this.props.post.optionTwosVotes} + + + {handleVoteOnPoll("optionTwosVotes")}}> + Option Two + + {this.props.post.optionTwo} + + + + Percent + + {this.props.post.optionTwosBarLength.toFixed(2)}% + + + + + + + + + 3. {this.props.post.optionThree || "Couldn't load option three"} + + + + + + + + + Votes + + {this.props.post.optionThreesVotes} + + + {handleVoteOnPoll("optionThreesVotes")}}> + Option Three + + {this.props.post.optionThree} + + + + Percent + + {this.props.post.optionThreesBarLength.toFixed(2)}% + + + + + + + + + 4. {this.props.post.optionFour || "Couldn't load option four"} + + + + + + + + + Votes + + {this.props.post.optionFoursVotes} + + + {handleVoteOnPoll("optionFoursVotes")}}> + Option Four + + {this.props.post.optionFour} + + + + Percent + + {this.props.post.optionFoursBarLength.toFixed(2)}% + + + + + + + + + 5. {this.props.post.optionFive || "Couldn't load option five"} + + + + + + + + + Votes + + {this.props.post.optionFivesVotes} + + + {handleVoteOnPoll("optionFivesVotes")}}> + Option Five + + {this.props.post.optionFive} + + + + Percent + + {this.props.post.optionFivesBarLength.toFixed(2)}% + + + + + + + + + 6. {this.props.post.optionSix || "Couldn't load option six"} + + + + + + + + + Votes + + {this.props.post.optionSixesVotes} + + + {handleVoteOnPoll("optionSixesVotes")}}> + Option Six + + {this.props.post.optionSix} + + + + Percent + + {this.props.post.optionSixesBarLength.toFixed(2)}% + + + + + + {this.props.post.changingVote ? + + + + + + : + <> + + + + + {this.props.post.votes} + + + + + + } + + + + + + + + + + + + + + {typeof message === 'string' || message instanceof String && ( + {message} + )} + + Total Votes: {this.props.post.optionOnesVotes + this.props.post.optionTwosVotes + this.props.post.optionThreesVotes + this.props.post.optionFoursVotes + this.props.post.optionFivesVotes + this.props.post.optionSixesVotes} + + {getTimeFromUTCMS(this.props.post.datePosted)} + {this.props.post.comments} comments + + + ) + } +} + +export default PollWithVotes; \ No newline at end of file diff --git a/screens/ViewPollPostPage.js b/screens/ViewPollPostPage.js index ec71434..ea62cae 100644 --- a/screens/ViewPollPostPage.js +++ b/screens/ViewPollPostPage.js @@ -971,328 +971,6 @@ const ViewPollPostPage = ({route, navigation}) => { - - - - - - - {creatorDisplayName} - @{creatorName} - - - - {objectForAllData.data.pollTitle || "Couldn't recieve data"} - - - {objectForAllData.data.pollSubTitle || "Couldn't recieve data"} - - - - 1 - - - 2 - - - 3 - - - 4 - - - 5 - - - 6 - - - - - - - - - - - - - - - - 1. {objectForAllData.data.optionOne || "Couldn't recieve data"} - - - - - - - - - Votes - - {pollVotesForOptions.optionOne} - - - {handleVoteOnPoll("optionOnesVotes")}}> - Option One - - {optionOneVoteText} - - - - Percent - - {pollBarLengths.optionOnesBarLength.toFixed(2)}% - - - - - - - - - - - 2. {objectForAllData.data.optionTwo || "Couldn't recieve data"} - - - - - - - - - Votes - - {pollVotesForOptions.optionTwo} - - - {handleVoteOnPoll("optionTwosVotes")}}> - Option Two - - {optionTwoVoteText} - - - - Percent - - {pollBarLengths.optionTwosBarLength.toFixed(2)}% - - - - - - - - - 3. {objectForAllData.data.optionThree || "Couldn't recieve data"} - - - - - - - - - Votes - - {pollVotesForOptions.optionThree} - - - {handleVoteOnPoll("optionThreesVotes")}}> - Option Three - - {optionThreeVoteText} - - - - Percent - - {pollBarLengths.optionThreesBarLength.toFixed(2)}% - - - - - - - - - 4. {objectForAllData.data.optionFour || "Couldn't recieve data"} - - - - - - - - - Votes - - {pollVotesForOptions.optionFour} - - - {handleVoteOnPoll("optionFoursVotes")}}> - Option Four - - {optionFourVoteText} - - - - Percent - - {pollBarLengths.optionFoursBarLength.toFixed(2)}% - - - - - - - - - 5. {objectForAllData.data.optionFive || "Couldn't recieve data"} - - - - - - - - - Votes - - {pollVotesForOptions.optionFive} - - - {handleVoteOnPoll("optionFivesVotes")}}> - Option Five - - {optionFiveVoteText} - - - - Percent - - {pollBarLengths.optionFivesBarLength.toFixed(2)}% - - - - - - - - - 6. {objectForAllData.data.optionSix || "Couldn't recieve data"} - - - - - - - - - Votes - - {pollVotesForOptions.optionSix} - - - {handleVoteOnPoll("optionSixesVotes")}}> - Option Six - - {optionSixVoteText} - - - - Percent - - {pollBarLengths.optionSixesBarLength.toFixed(2)}% - - - - - - {pollUpOrDownVoted == "UpVoted" && ( {UpVotePoll()}}> - - )} - {pollUpOrDownVoted == "Neither" && ( {UpVotePoll()}}> - - )} - {pollUpOrDownVoted == "DownVoted" && ( {UpVotePoll()}}> - - )} - {pollUpOrDownVoted == "Changing" && ()} - - {pollUpOrDownVoted == "Finding" && ( - {pollUpOrDownVotes} - )} - {pollUpOrDownVoted == "UpVoted" && ( - {initialPollUpOrDownVoted == "UpVoted" && ( - {pollUpOrDownVotes} - )} - {initialPollUpOrDownVoted == "Neither" && ( - {pollUpOrDownVotes+1} - )} - {initialPollUpOrDownVoted == "DownVoted" && ( - {pollUpOrDownVotes+2} - )} - )} - {pollUpOrDownVoted == "Neither" && ( - {initialPollUpOrDownVoted == "Neither" && ( - {pollUpOrDownVotes} - )} - {initialPollUpOrDownVoted == "UpVoted" && ( - {pollUpOrDownVotes-1} - )} - {initialPollUpOrDownVoted == "DownVoted" && ( - {pollUpOrDownVotes+1} - )} - )} - {pollUpOrDownVoted == "DownVoted" && ( - {initialPollUpOrDownVoted == "DownVoted" && ( - {pollUpOrDownVotes} - )} - {initialPollUpOrDownVoted == "Neither" && ( - {pollUpOrDownVotes-1} - )} - {initialPollUpOrDownVoted == "UpVoted" && ( - {pollUpOrDownVotes-2} - )} - )} - {pollUpOrDownVoted == "Changing" && ( - - )} - - {pollUpOrDownVoted == "DownVoted" && ( {DownVotePoll()}}> - - )} - {pollUpOrDownVoted == "Neither" && ( {DownVotePoll()}}> - - )} - {pollUpOrDownVoted == "UpVoted" && ( {DownVotePoll()}}> - - )} - {pollUpOrDownVoted == "Changing" && ()} - - - - - - - - - - - - - {typeof message === 'string' || message instanceof String && ( - {message} - )} - - Total Votes: {pollVotesForOptions.optionOne+pollVotesForOptions.optionTwo+pollVotesForOptions.optionThree+pollVotesForOptions.optionFour+pollVotesForOptions.optionFive+pollVotesForOptions.optionSix} - - {getTimeFromUTCMS(datePosted)} - {commentsLength} comments - {storedCredentials ? Comments From 41b7fa05867bd11d5a2573a4a2a1578ce8d8708b Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Fri, 15 Sep 2023 12:41:54 +1200 Subject: [PATCH 03/10] Add voting functionality in PollWithVotes --- components/Posts/PollPost.js | 2 +- components/Posts/PollWithVotes.js | 102 ++++++-- hooks/usePostReducer.js | 50 ++++ screens/ViewPollPostPage.js | 381 ------------------------------ 4 files changed, 132 insertions(+), 403 deletions(-) diff --git a/components/Posts/PollPost.js b/components/Posts/PollPost.js index 11eb87e..3726a9d 100644 --- a/components/Posts/PollPost.js +++ b/components/Posts/PollPost.js @@ -167,7 +167,7 @@ class Poll extends Component { optionSixesVotes: this.props.post.optionSixesVotes, optionSixesBarLength: this.props.post.optionSixesBarLength, totalNumberOfOptions: this.props.post.totalNumberOfOptions, - pollId: this.props.post.pollId, + _id: this.props.post._id, creatorPfpB64: this.props.post.pfpB64, creatorName: this.props.post.creatorName, creatorDisplayName: this.props.post.creatorDisplayName, diff --git a/components/Posts/PollWithVotes.js b/components/Posts/PollWithVotes.js index 2e64895..c434358 100644 --- a/components/Posts/PollWithVotes.js +++ b/components/Posts/PollWithVotes.js @@ -1,8 +1,12 @@ -import React from 'react'; +import React, { useContext } from 'react'; import { PollClass } from './PollPost'; import { ActivityIndicator } from 'react-native'; import { PostsIconFrame } from '../../screens/screenStylings/styling'; import { getTimeFromUTCMS } from '../../libraries/Time'; +import { useNavigation } from '@react-navigation/native'; +import { ServerUrlContext } from '../ServerUrlContext'; +import { CredentialsContext } from '../CredentialsContext'; +import ParseErrorMessage from '../ParseErrorMessage'; class PollWithVotes extends PollClass { constructor(props) { @@ -40,6 +44,48 @@ class PollWithVotes extends PollClass { optionFiveInfoState = false; //Temporary optionSixInfoState = false; //Temporary + handleVoteOnPoll = (optionSelected, voteNumber) => { + if (this.props.storedCredentials) { + this.props.dispatch({type: 'startPollVoteChange', postIndex: this.props.index}) + console.log(optionSelected) + const url = this.props.serverUrl + "/tempRoute/voteonpoll"; + + const toSend = {optionSelected: optionSelected, pollId: this.props.post._id} + + console.log(toSend) + + axios.post(url, toSend).then(() => { + this.props.dispatch({type: 'voteOnPoll', vote: voteNumber, postIndex: this.props.index}) + }).catch(error => { + this.props.dispatch({type: 'stopPollVoteChange', postIndex: this.props.index}) + console.error(error); + alert(ParseErrorMessage(error)); + }) + } else { + this.props.navigation.navigate('ModalLoginScreen', {modal: true}) + } + } + + handleRemoveVoteOnPoll = () => { + if (this.props.storedCredentials) { + this.props.dispatch({type: 'startPollVoteChange', postIndex: this.props.index}) + const url = this.props.serverUrl + "/tempRoute/removevoteonpoll" + const toSend = {pollId: this.props.post._id} + + console.log(toSend) + + axios.post(url, toSend).then(() => { + this.props.dispatch({type: 'removeVoteOnPoll', postIndex: this.props.index}) + }).catch(error => { + this.props.dispatch({type: 'stopPollVoteChange', postIndex: this.props.index}) + console.error(error) + alert(ParseErrorMessage(error)) + }) + } else { + this.props.navigation.navigate('ModalLoginScreen', {modal: true}) + } + } + shouldComponentUpdate(nextProps, nextState) { const upvoteIsSame = nextProps.post.upvoted === this.props.post.upvoted; const downvoteIsSame = nextProps.post.downvoted === this.props.post.downvoted; @@ -48,15 +94,11 @@ class PollWithVotes extends PollClass { const pollIdIsSame = nextProps.post.pollId === this.props.post.pollId; const deletingIsSame = nextProps.post.deleting === this.props.post.deleting; const profilePictureIsSame = nextProps.post.pfpB64 === this.props.post.pfpB64; - const voteOneSame = nextProps.post.optionOnesVotes === this.props.post.optionOnesVotes; - const voteTwoSame = nextProps.post.optionTwosVotes === this.props.post.optionTwosVotes; - const voteThreeSame = nextProps.post.optionThreesVotes === this.props.post.optionThreesVotes; - const voteFourSame = nextProps.post.optionFoursVotes === this.props.post.optionFoursVotes; - const voteFiveSame = nextProps.post.optionFivesVotes === this.props.post.optionFivesVotes; - const voteSixSame = nextProps.post.optionSixesVotes === this.props.post.optionSixesVotes; + const changingPollVoteIsSame = nextProps.post.pollVoteChanging === this.props.post.pollVoteChanging; + const votedForIsSame = nextProps.post.votedFor === this.props.post.votedFor - if (upvoteIsSame && downvoteIsSame && changingVoteIsSame && colorsAreSame && pollIdIsSame && deletingIsSame && profilePictureIsSame && voteOneSame && voteTwoSame && voteThreeSame && voteFourSame && voteFiveSame && voteSixSame) return false; + if (upvoteIsSame && downvoteIsSame && changingVoteIsSame && colorsAreSame && pollIdIsSame && deletingIsSame && profilePictureIsSame && changingPollVoteIsSame && votedForIsSame) return false; return true; } @@ -133,10 +175,10 @@ class PollWithVotes extends PollClass { {this.props.post.optionOnesVotes} - {handleVoteOnPoll("optionOnesVotes")}}> + {this.props.post.votedFor === "One" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionOnesVotes", "One")}}> Option One - {this.props.post.optionOne} + {this.props.post.votedFor === "One" ? "Voted" : "Vote"} @@ -167,10 +209,10 @@ class PollWithVotes extends PollClass { {this.props.post.optionTwosVotes} - {handleVoteOnPoll("optionTwosVotes")}}> + {this.props.post.votedFor === "Two" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionTwosVotes", "Two")}}> Option Two - {this.props.post.optionTwo} + {this.props.post.votedFor === "Two" ? "Voted" : "Vote"} @@ -199,10 +241,10 @@ class PollWithVotes extends PollClass { {this.props.post.optionThreesVotes} - {handleVoteOnPoll("optionThreesVotes")}}> + {this.props.post.votedFor === "Three" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionThreesVotes", "Three")}}> Option Three - {this.props.post.optionThree} + {this.props.post.votedFor === "Three" ? "Voted" : "Vote"} @@ -231,10 +273,10 @@ class PollWithVotes extends PollClass { {this.props.post.optionFoursVotes} - {handleVoteOnPoll("optionFoursVotes")}}> + {this.props.post.votedFor === "Four" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionFoursVotes", "Four")}}> Option Four - {this.props.post.optionFour} + {this.props.post.votedFor === "Four" ? "Voted" : "Vote"} @@ -263,10 +305,10 @@ class PollWithVotes extends PollClass { {this.props.post.optionFivesVotes} - {handleVoteOnPoll("optionFivesVotes")}}> + {this.props.post.votedFor === "Five" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionFivesVotes", "Five")}}> Option Five - {this.props.post.optionFive} + {this.props.post.votedFor === "Five" ? "Voted" : "Vote"} @@ -295,10 +337,10 @@ class PollWithVotes extends PollClass { {this.props.post.optionSixesVotes} - {handleVoteOnPoll("optionSixesVotes")}}> + {this.props.post.votedFor === "Six" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionSixesVotes", "Six")}}> Option Six - {this.props.post.optionSix} + {this.props.post.votedFor === "Six" ? "Voted" : "Vote"} @@ -356,4 +398,22 @@ class PollWithVotes extends PollClass { } } -export default PollWithVotes; \ No newline at end of file +export default function(props) { + const navigation = useNavigation(); + const {serverUrl} = useContext(ServerUrlContext) + const {storedCredentials} = useContext(CredentialsContext) + + const postProps = { + navigation, + post: props.post, + colors: props.colors, + colorsIndexNum: props.colorsIndexNum, + dispatch: props.dispatch, + serverUrl, + index: props.index, + //No useRawImages support yet - Might want to add that in the future + storedCredentials + } + + return +}; \ No newline at end of file diff --git a/hooks/usePostReducer.js b/hooks/usePostReducer.js index 241ce2d..a5759df 100644 --- a/hooks/usePostReducer.js +++ b/hooks/usePostReducer.js @@ -240,6 +240,56 @@ const reducer = (state, action) => { } } + if (action.type === 'startPollVoteChange') { + if (typeof action.postIndex !== 'number') throw new Error('postIndex was not provided to startPollVoteChange action of usePostReducer') + + if (typeof state.posts[action.postIndex] !== 'object' || Array.isArray(state.posts[action.postIndex]) || state.posts[action.postIndex] === null) throw new Error(`Post at index ${action.postIndex} is not an object`) + + state.posts[action.postIndex] = { + ...state.posts[action.postIndex], + pollVoteChanging: true + } + + return {...state}; + } + + if (action.type === 'stopPollVoteChange') { + if (typeof action.postIndex !== 'number') throw new Error('postIndex was not provided to stopPollVoteChange action of usePostReducer') + + if (typeof state.posts[action.postIndex] !== 'object' || Array.isArray(state.posts[action.postIndex]) || state.posts[action.postIndex] === null) throw new Error(`Post at index ${action.postIndex} is not an object`) + + state.posts[action.postIndex] = { + ...state.posts[action.postIndex], + pollVoteChanging: false + } + + return {...state}; + } + + if (action.type === 'voteOnPoll') { + if (typeof action.postIndex !== 'number') throw new Error('postIndex was not provided to voteOnPoll action of usePostReducer') + if (typeof state.posts[action.postIndex] !== 'object' || Array.isArray(state.posts[action.postIndex]) || state.posts[action.postIndex] === null) throw new Error(`Post at index ${action.postIndex} is not an object`) + + state.posts[action.postIndex] = { + ...state.posts[action.postIndex], + votedFor: action.optionSelected + } + + return {...state} + } + + if (action.type === 'removeVoteOnPoll') { + if (typeof action.postIndex !== 'number') throw new Error('postIndex was not provided to removeVoteOnPoll action of usePostReducer') + if (typeof state.posts[action.postIndex] !== 'object' || Array.isArray(state.posts[action.postIndex]) || state.posts[action.postIndex] === null) throw new Error(`Post at index ${action.postIndex} is not an object`) + + state.posts[action.postIndex] = { + ...state.posts[action.postIndex], + votedFor: 'None' + } + + return {...state} + } + throw new Error('Wrong action type was passed to usePostReducer') } diff --git a/screens/ViewPollPostPage.js b/screens/ViewPollPostPage.js index ea62cae..38c7390 100644 --- a/screens/ViewPollPostPage.js +++ b/screens/ViewPollPostPage.js @@ -440,387 +440,6 @@ const ViewPollPostPage = ({route, navigation}) => { }) } - const handleVoteOnPoll = (optionSelected) => { - if (storedCredentials) { - handleMessage(null); - console.log(optionSelected) - const url = serverUrl + "/tempRoute/voteonpoll"; - - var toSend = {optionSelected: optionSelected, pollId: pollId} - - console.log(toSend) - - axios.post(url, toSend).then((response) => { - const result = response.data; - const {message, status, data} = result; - - if (status !== 'SUCCESS') { - handleMessage(message, status); - } else { - handleMessage(message, status); - console.log("Message:") - console.log(message) - var lastVote = data.lastVote - console.log("Last Vote:") - console.log(lastVote) - if (message == "Vote successful") { - //voted - var optionOnesBarLength = 16.6666666667 - var optionTwosBarLength = 16.6666666667 - var optionThreesBarLength = 16.6666666667 - var optionFoursBarLength = 16.6666666667 - var optionFivesBarLength = 16.6666666667 - var optionSixesBarLength = 16.6666666667 - var o1V = initialPollVotesForOptions.optionOne - var o2V = initialPollVotesForOptions.optionTwo - var o3V = initialPollVotesForOptions.optionThree - var o4V = initialPollVotesForOptions.optionFour - var o5V = initialPollVotesForOptions.optionFive - var o6V = initialPollVotesForOptions.optionSix - //Change depending on initial - if (pollVoteOption == "None") { - if (optionSelected == "optionOnesVotes") { - o1V = o1V+1 - } else if (optionSelected == "optionTwosVotes") { - o2V = o2V+1 - } else if (optionSelected == "optionThreesVotes") { - o3V = o3V+1 - } else if (optionSelected == "optionFoursVotes") { - o4V = o4V+1 - } else if (optionSelected == "optionFivesVotes") { - o5V = o5V+1 - } else { - //six - o6V = o6V+1 - } - } else if (pollVoteOption == "One") { - o1V = o1V-1 - if (optionSelected == "optionOnesVotes") { - o1V = o1V+1 - } else if (optionSelected == "optionTwosVotes") { - o2V = o2V+1 - } else if (optionSelected == "optionThreesVotes") { - o3V = o3V+1 - } else if (optionSelected == "optionFoursVotes") { - o4V = o4V+1 - } else if (optionSelected == "optionFivesVotes") { - o5V = o5V+1 - } else { - //six - o6V = o6V+1 - } - } else if (pollVoteOption == "Two") { - o2V = o2V-1 - if (optionSelected == "optionOnesVotes") { - o1V = o1V+1 - } else if (optionSelected == "optionTwosVotes") { - o2V = o2V+1 - } else if (optionSelected == "optionThreesVotes") { - o3V = o3V+1 - } else if (optionSelected == "optionFoursVotes") { - o4V = o4V+1 - } else if (optionSelected == "optionFivesVotes") { - o5V = o5V+1 - } else { - //six - o6V = o6V+1 - } - } else if (pollVoteOption == "Three") { - o3V = o3V-1 - if (optionSelected == "optionOnesVotes") { - o1V = o1V+1 - } else if (optionSelected == "optionTwosVotes") { - o2V = o2V+1 - } else if (optionSelected == "optionThreesVotes") { - o3V = o3V+1 - } else if (optionSelected == "optionFoursVotes") { - o4V = o4V+1 - } else if (optionSelected == "optionFivesVotes") { - o5V = o5V+1 - } else { - //six - o6V = o6V+1 - } - } else if (pollVoteOption == "Four") { - o4V = o4V-1 - if (optionSelected == "optionOnesVotes") { - o1V = o1V+1 - } else if (optionSelected == "optionTwosVotes") { - o2V = o2V+1 - } else if (optionSelected == "optionThreesVotes") { - o3V = o3V+1 - } else if (optionSelected == "optionFoursVotes") { - o4V = o4V+1 - } else if (optionSelected == "optionFivesVotes") { - o5V = o5V+1 - } else { - //six - o6V = o6V+1 - } - } else if (pollVoteOption == "Five") { - o5V = o5V-1 - if (optionSelected == "optionOnesVotes") { - o1V = o1V+1 - } else if (optionSelected == "optionTwosVotes") { - o2V = o2V+1 - } else if (optionSelected == "optionThreesVotes") { - o3V = o3V+1 - } else if (optionSelected == "optionFoursVotes") { - o4V = o4V+1 - } else if (optionSelected == "optionFivesVotes") { - o5V = o5V+1 - } else { - //six - o6V = o6V+1 - } - } else { - o6V = o6V-1 - if (optionSelected == "optionOnesVotes") { - o1V = o1V+1 - } else if (optionSelected == "optionTwosVotes") { - o2V = o2V+1 - } else if (optionSelected == "optionThreesVotes") { - o3V = o3V+1 - } else if (optionSelected == "optionFoursVotes") { - o4V = o4V+1 - } else if (optionSelected == "optionFivesVotes") { - o5V = o5V+1 - } else { - //six - o6V = o6V+1 - } - } - var totalVotes = o1V+o2V+o3V+o4V+o5V+o6V - optionOnesBarLength = (o1V/totalVotes)*100 - console.log("O1 BL") - console.log(optionOnesBarLength) - optionTwosBarLength = (o2V/totalVotes)*100 - console.log("O2 BL") - console.log(optionTwosBarLength) - optionThreesBarLength = (o3V/totalVotes)*100 - console.log("O3 BL") - console.log(optionThreesBarLength) - optionFoursBarLength = (o4V/totalVotes)*100 - console.log("O4 BL") - console.log(optionFoursBarLength) - optionFivesBarLength = (o5V/totalVotes)*100 - console.log("O5 BL") - console.log(optionFivesBarLength) - optionSixesBarLength = (o6V/totalVotes)*100 - console.log("O6 BL") - console.log(optionSixesBarLength) - if (Number.isNaN(optionOnesBarLength)) { - optionOnesBarLength = 0 - } - if (Number.isNaN(optionTwosBarLength)) { - optionTwosBarLength = 0 - } - if (Number.isNaN(optionThreesBarLength)) { - optionThreesBarLength = 0 - } - if (Number.isNaN(optionFoursBarLength)) { - optionFoursBarLength = 0 - } - if (Number.isNaN(optionFivesBarLength)) { - optionFivesBarLength = 0 - } - if (Number.isNaN(optionSixesBarLength)) { - optionSixesBarLength = 0 - } - if (optionSelected == "optionOnesVotes") { - setOptionOneVoteText("Voted") - setOptionTwoVoteText("Vote") - setOptionThreeVoteText("Vote") - setOptionFourVoteText("Vote") - setOptionFiveVoteText("Vote") - setOptionSixVoteText("Vote") - } else if (optionSelected == "optionTwosVotes") { - setOptionOneVoteText("Vote") - setOptionTwoVoteText("Voted") - setOptionThreeVoteText("Vote") - setOptionFourVoteText("Vote") - setOptionFiveVoteText("Vote") - setOptionSixVoteText("Vote") - } else if (optionSelected == "optionThreesVotes") { - setOptionOneVoteText("Vote") - setOptionTwoVoteText("Vote") - setOptionThreeVoteText("Voted") - setOptionFourVoteText("Vote") - setOptionFiveVoteText("Vote") - setOptionSixVoteText("Vote") - } else if (optionSelected == "optionFoursVotes") { - setOptionOneVoteText("Vote") - setOptionTwoVoteText("Vote") - setOptionThreeVoteText("Vote") - setOptionFourVoteText("Voted") - setOptionFiveVoteText("Vote") - setOptionSixVoteText("Vote") - } else if (optionSelected == "optionFivesVotes") { - setOptionOneVoteText("Vote") - setOptionTwoVoteText("Vote") - setOptionThreeVoteText("Vote") - setOptionFourVoteText("Vote") - setOptionFiveVoteText("Voted") - setOptionSixVoteText("Vote") - } else { - setOptionOneVoteText("Vote") - setOptionTwoVoteText("Vote") - setOptionThreeVoteText("Vote") - setOptionFourVoteText("Vote") - setOptionFiveVoteText("Vote") - setOptionSixVoteText("Voted") - } - setPollVotesForOptions({optionOne: o1V, optionTwo: o2V, optionThree: o3V, optionFour: o4V, optionFive: o5V, optionSix: o6V}) - setPollBarLengths({optionOnesBarLength: optionOnesBarLength, optionTwosBarLength: optionTwosBarLength, optionThreesBarLength: optionThreesBarLength, optionFoursBarLength: optionFoursBarLength, optionFivesBarLength: optionFivesBarLength, optionSixesBarLength: optionSixesBarLength}) - } else { - //pulled - console.log("Change based on pull") - var optionOnesBarLength = 16.6666666667 - var optionTwosBarLength = 16.6666666667 - var optionThreesBarLength = 16.6666666667 - var optionFoursBarLength = 16.6666666667 - var optionFivesBarLength = 16.6666666667 - var optionSixesBarLength = 16.6666666667 - var o1V = initialPollVotesForOptions.optionOne - var o2V = initialPollVotesForOptions.optionTwo - var o3V = initialPollVotesForOptions.optionThree - var o4V = initialPollVotesForOptions.optionFour - var o5V = initialPollVotesForOptions.optionFive - var o6V = initialPollVotesForOptions.optionSix - if (o1V !== "Finding") { - if (pollVoteOption == "One") { - o1V = o1V-1 - } else if (pollVoteOption == "Two") { - o2V = o2V-1 - } else if (pollVoteOption == "Three") { - o3V = o3V-1 - } else if (pollVoteOption == "Four") { - o4V = o4V-1 - } else if (pollVoteOption == "Five") { - o5V = o5V-1 - } else if (pollVoteOption == "Six") { - o6V = o6V-1 - } else { - //Initial Poll Vote Option Would Be None - //Keep original - o1V = initialPollVotesForOptions.optionOne - o2V = initialPollVotesForOptions.optionTwo - o3V = initialPollVotesForOptions.optionThree - o4V = initialPollVotesForOptions.optionFour - o5V = initialPollVotesForOptions.optionFive - o6V = initialPollVotesForOptions.optionSix - } - var totalVotes = o1V+o2V+o3V+o4V+o5V+o6V - console.log("Total Votes:") - console.log(totalVotes) - if (totalVotes == 0) { - console.log("No Votes") - if (totalNumberOfOptions == "Two") { - optionOnesBarLength = 100/2 - optionTwosBarLength = 100/2 - optionThreesBarLength = 0 - optionFoursBarLength = 0 - optionFivesBarLength = 0 - optionSixesBarLength = 0 - setPollVotesForOptions({optionOne: o1V, optionTwo: o2V, optionThree: o3V, optionFour: o4V, optionFive: o5V, optionSix: o6V}) - setPollBarLengths({optionOnesBarLength: optionOnesBarLength, optionTwosBarLength: optionTwosBarLength, optionThreesBarLength: optionThreesBarLength, optionFoursBarLength: optionFoursBarLength, optionFivesBarLength: optionFivesBarLength, optionSixesBarLength: optionSixesBarLength}) - } else if (totalNumberOfOptions == "Three") { - optionOnesBarLength = 100/3 - optionTwosBarLength = 100/3 - optionThreesBarLength = 100/3 - optionFoursBarLength = 0 - optionFivesBarLength = 0 - optionSixesBarLength = 0 - setPollVotesForOptions({optionOne: o1V, optionTwo: o2V, optionThree: o3V, optionFour: o4V, optionFive: o5V, optionSix: o6V}) - setPollBarLengths({optionOnesBarLength: optionOnesBarLength, optionTwosBarLength: optionTwosBarLength, optionThreesBarLength: optionThreesBarLength, optionFoursBarLength: optionFoursBarLength, optionFivesBarLength: optionFivesBarLength, optionSixesBarLength: optionSixesBarLength}) - } else if (totalNumberOfOptions == "Four") { - optionOnesBarLength = 100/4 - optionTwosBarLength = 100/4 - optionThreesBarLength = 100/4 - optionFoursBarLength = 100/4 - optionFivesBarLength = 0 - optionSixesBarLength = 0 - setPollVotesForOptions({optionOne: o1V, optionTwo: o2V, optionThree: o3V, optionFour: o4V, optionFive: o5V, optionSix: o6V}) - setPollBarLengths({optionOnesBarLength: optionOnesBarLength, optionTwosBarLength: optionTwosBarLength, optionThreesBarLength: optionThreesBarLength, optionFoursBarLength: optionFoursBarLength, optionFivesBarLength: optionFivesBarLength, optionSixesBarLength: optionSixesBarLength}) - } else if (totalNumberOfOptions == "Five") { - optionOnesBarLength = 100/5 - optionTwosBarLength = 100/5 - optionThreesBarLength = 100/5 - optionFoursBarLength = 100/5 - optionFivesBarLength = 100/5 - optionSixesBarLength = 0 - setPollVotesForOptions({optionOne: o1V, optionTwo: o2V, optionThree: o3V, optionFour: o4V, optionFive: o5V, optionSix: o6V}) - setPollBarLengths({optionOnesBarLength: optionOnesBarLength, optionTwosBarLength: optionTwosBarLength, optionThreesBarLength: optionThreesBarLength, optionFoursBarLength: optionFoursBarLength, optionFivesBarLength: optionFivesBarLength, optionSixesBarLength: optionSixesBarLength}) - } else { - optionOnesBarLength = 100/6 - optionTwosBarLength = 100/6 - optionThreesBarLength = 100/6 - optionFoursBarLength = 100/6 - optionFivesBarLength = 100/6 - optionSixesBarLength = 100/6 - setPollVotesForOptions({optionOne: o1V, optionTwo: o2V, optionThree: o3V, optionFour: o4V, optionFive: o5V, optionSix: o6V}) - setPollBarLengths({optionOnesBarLength: optionOnesBarLength, optionTwosBarLength: optionTwosBarLength, optionThreesBarLength: optionThreesBarLength, optionFoursBarLength: optionFoursBarLength, optionFivesBarLength: optionFivesBarLength, optionSixesBarLength: optionSixesBarLength}) - } - } else { - console.log("Pulled results") - optionOnesBarLength = (o1V/totalVotes)*100 - console.log("O1 BL") - console.log(optionOnesBarLength) - optionTwosBarLength = (o2V/totalVotes)*100 - console.log("O2 BL") - console.log(optionTwosBarLength) - optionThreesBarLength = (o3V/totalVotes)*100 - console.log("O3 BL") - console.log(optionThreesBarLength) - optionFoursBarLength = (o4V/totalVotes)*100 - console.log("O4 BL") - console.log(optionFoursBarLength) - optionFivesBarLength = (o5V/totalVotes)*100 - console.log("O5 BL") - console.log(optionFivesBarLength) - optionSixesBarLength = (o6V/totalVotes)*100 - console.log("O6 BL") - console.log(optionSixesBarLength) - if (Number.isNaN(optionOnesBarLength)) { - optionOnesBarLength = 0 - } - if (Number.isNaN(optionTwosBarLength)) { - optionTwosBarLength = 0 - } - if (Number.isNaN(optionThreesBarLength)) { - optionThreesBarLength = 0 - } - if (Number.isNaN(optionFoursBarLength)) { - optionFoursBarLength = 0 - } - if (Number.isNaN(optionFivesBarLength)) { - optionFivesBarLength = 0 - } - if (Number.isNaN(optionSixesBarLength)) { - optionSixesBarLength = 0 - } - } - setPollVotesForOptions({optionOne: o1V, optionTwo: o2V, optionThree: o3V, optionFour: o4V, optionFive: o5V, optionSix: o6V}) - setPollBarLengths({optionOnesBarLength: optionOnesBarLength, optionTwosBarLength: optionTwosBarLength, optionThreesBarLength: optionThreesBarLength, optionFoursBarLength: optionFoursBarLength, optionFivesBarLength: optionFivesBarLength, optionSixesBarLength: optionSixesBarLength}) - } else { - handleMessage("Page didn't fully load try again", "FAILED") - } - } - //loadAndGetValues() - //persistLogin({...data[0]}, message, status); - } - setSubmitting(false); - - }).catch(error => { - console.error(error); - setSubmitting(false); - handleMessage(ParseErrorMessage(error)); - }) - } else { - navigation.navigate('ModalLoginScreen', {modal: true}) - } - } - const handleMessage = (message, type = 'FAILED') => { setMessage(message); setMessageType(type); From 4bbef10d292e46df5867e495052bdc8aba70bb93 Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Fri, 15 Sep 2023 12:54:52 +1200 Subject: [PATCH 04/10] Fixed issues with invalid image paths --- components/Posts/PollWithVotes.js | 46 +++++++++++++++---------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/components/Posts/PollWithVotes.js b/components/Posts/PollWithVotes.js index c434358..a2cde0c 100644 --- a/components/Posts/PollWithVotes.js +++ b/components/Posts/PollWithVotes.js @@ -171,19 +171,19 @@ class PollWithVotes extends PollClass { Votes - + {this.props.post.optionOnesVotes} {this.props.post.votedFor === "One" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionOnesVotes", "One")}}> Option One - + {this.props.post.votedFor === "One" ? "Voted" : "Vote"} Percent - + {this.props.post.optionOnesBarLength.toFixed(2)}% @@ -205,19 +205,19 @@ class PollWithVotes extends PollClass { Votes - + {this.props.post.optionTwosVotes} {this.props.post.votedFor === "Two" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionTwosVotes", "Two")}}> Option Two - + {this.props.post.votedFor === "Two" ? "Voted" : "Vote"} Percent - + {this.props.post.optionTwosBarLength.toFixed(2)}% @@ -237,19 +237,19 @@ class PollWithVotes extends PollClass { Votes - + {this.props.post.optionThreesVotes} {this.props.post.votedFor === "Three" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionThreesVotes", "Three")}}> Option Three - + {this.props.post.votedFor === "Three" ? "Voted" : "Vote"} Percent - + {this.props.post.optionThreesBarLength.toFixed(2)}% @@ -269,19 +269,19 @@ class PollWithVotes extends PollClass { Votes - + {this.props.post.optionFoursVotes} {this.props.post.votedFor === "Four" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionFoursVotes", "Four")}}> Option Four - + {this.props.post.votedFor === "Four" ? "Voted" : "Vote"} Percent - + {this.props.post.optionFoursBarLength.toFixed(2)}% @@ -301,19 +301,19 @@ class PollWithVotes extends PollClass { Votes - + {this.props.post.optionFivesVotes} {this.props.post.votedFor === "Five" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionFivesVotes", "Five")}}> Option Five - + {this.props.post.votedFor === "Five" ? "Voted" : "Vote"} Percent - + {this.props.post.optionFivesBarLength.toFixed(2)}% @@ -333,19 +333,19 @@ class PollWithVotes extends PollClass { Votes - + {this.props.post.optionSixesVotes} {this.props.post.votedFor === "Six" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionSixesVotes", "Six")}}> Option Six - + {this.props.post.votedFor === "Six" ? "Voted" : "Vote"} Percent - + {this.props.post.optionSixesBarLength.toFixed(2)}% @@ -361,13 +361,13 @@ class PollWithVotes extends PollClass { : <> - + {this.props.post.votes} - + } @@ -375,13 +375,13 @@ class PollWithVotes extends PollClass { - + - + - + {typeof message === 'string' || message instanceof String && ( From 72b178fa0639d52e8288331a22ae73a5fd5812e0 Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Fri, 15 Sep 2023 12:56:13 +1200 Subject: [PATCH 05/10] Fixed PollWithVotes not rendering --- components/Posts/PollWithVotes.js | 2 +- screens/ViewPollPostPage.js | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/components/Posts/PollWithVotes.js b/components/Posts/PollWithVotes.js index a2cde0c..5129a36 100644 --- a/components/Posts/PollWithVotes.js +++ b/components/Posts/PollWithVotes.js @@ -415,5 +415,5 @@ export default function(props) { storedCredentials } - return + return }; \ No newline at end of file diff --git a/screens/ViewPollPostPage.js b/screens/ViewPollPostPage.js index 38c7390..7dfde04 100644 --- a/screens/ViewPollPostPage.js +++ b/screens/ViewPollPostPage.js @@ -73,6 +73,7 @@ import { ServerUrlContext } from '../components/ServerUrlContext.js'; import { StatusBarHeightContext } from '../components/StatusBarHeightContext'; import ParseErrorMessage from '../components/ParseErrorMessage'; import { getTimeFromUTCMS } from '../libraries/Time'; +import PollWithVotes from '../components/Posts/PollWithVotes'; const ViewPollPostPage = ({route, navigation}) => { @@ -590,6 +591,7 @@ const ViewPollPostPage = ({route, navigation}) => { + {storedCredentials ? Comments From 32c2b1c72af0a365c17f2ccb390603b3a393e4ae Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Fri, 15 Sep 2023 16:38:52 +1200 Subject: [PATCH 06/10] ViewPollPostPage now successfully loads --- components/Posts/PollPost.js | 35 +-- components/Posts/PollWithVotes.js | 73 ++++--- screens/ViewPollPostPage.js | 339 +++--------------------------- 3 files changed, 74 insertions(+), 373 deletions(-) diff --git a/components/Posts/PollPost.js b/components/Posts/PollPost.js index 3726a9d..478c0eb 100644 --- a/components/Posts/PollPost.js +++ b/components/Posts/PollPost.js @@ -140,39 +140,8 @@ class Poll extends Component { navigateToFullScreen = () => { this.props.navigation.navigate("ViewPollPostPage", { - pollTitle: this.props.post.pollTitle, - pollSubTitle: this.props.post.pollSubTitle, - optionOne: this.props.post.optionOne, - optionOnesColor: this.props.post.optionOnesColor, - optionOnesVotes: this.props.post.optionOnesVotes, - optionOnesBarLength: this.props.post.optionOnesBarLength, - optionTwo: this.props.post.optionTwo, - optionTwosColor: this.props.post.optionTwosColor, - optionTwosVotes: this.props.post.optionTwosCotes, - optionTwosBarLength: this.props.post.optionTwosBarLength, - optionThree: this.props.post.optionThree, - optionThreesColor: this.props.post.optionThreesColor, - optionThreesVotes: this.props.post.optionThreesVotes, - optionThreesBarLength: this.props.post.optionThreesBarLength, - optionFour: this.props.post.optionFour, - optionFoursColor: this.props.post.optionFoursColor, - optionFoursVotes: this.props.post.optionFoursVotes, - optionFoursBarLength: this.props.post.optionFoursBarLength, - optionFive: this.props.post.optionFive, - optionFivesColor: this.props.post.optionFivesColor, - optionFivesVotes: this.props.post.optionFivesVotes, - optionFivesBarLength: this.props.post.optionFivesBarLength, - optionSix: this.props.post.optionSix, - optionSixesColor: this.props.post.optionSixesColor, - optionSixesVotes: this.props.post.optionSixesVotes, - optionSixesBarLength: this.props.post.optionSixesBarLength, - totalNumberOfOptions: this.props.post.totalNumberOfOptions, - _id: this.props.post._id, - creatorPfpB64: this.props.post.pfpB64, - creatorName: this.props.post.creatorName, - creatorDisplayName: this.props.post.creatorDisplayName, - datePosted: this.props.post.datePosted, - votedFor: this.props.post.votedFor + post: this.props.post, + isOwner: this.props.post.isOwner }) } diff --git a/components/Posts/PollWithVotes.js b/components/Posts/PollWithVotes.js index 5129a36..248469f 100644 --- a/components/Posts/PollWithVotes.js +++ b/components/Posts/PollWithVotes.js @@ -1,12 +1,38 @@ import React, { useContext } from 'react'; import { PollClass } from './PollPost'; -import { ActivityIndicator } from 'react-native'; -import { PostsIconFrame } from '../../screens/screenStylings/styling'; +import { ActivityIndicator, View } from 'react-native'; +import { + PostsIconFrame, + ViewScreenPollPostFrame, + PostsHorizontalView, + PostsVerticalView, + SubTitle, + PostCreatorIcon, + PollPostSubTitle, + ProfIcons, + PollPostHorizontalView, + AboveBarPollPostHorizontalView, + PollPostTitle, + PollKeysCircle, + PollKeyViewOne, + PollKeyViewTwo, + PollKeyViewThree, + PollKeyViewFour, + PollKeyViewFive, + PollKeyViewSix, + PostsIcons, + PollBarOutline, + PollBarItem, + PollHorizontalViewItem, + PollHorizontalViewItemCenter, + PostHorizontalView +} from '../../screens/screenStylings/styling'; import { getTimeFromUTCMS } from '../../libraries/Time'; import { useNavigation } from '@react-navigation/native'; import { ServerUrlContext } from '../ServerUrlContext'; import { CredentialsContext } from '../CredentialsContext'; import ParseErrorMessage from '../ParseErrorMessage'; +import Octicons from 'react-native-vector-icons/Octicons' class PollWithVotes extends PollClass { constructor(props) { @@ -125,7 +151,7 @@ class PollWithVotes extends PollClass { {this.props.post.pollTitle || "Couldn't recieve poll title"} - + {this.props.post.pollSubTitle || "Couldn't recieve poll subtitle"} @@ -149,15 +175,15 @@ class PollWithVotes extends PollClass { - - - - - - + + + + + + - + @@ -191,7 +217,7 @@ class PollWithVotes extends PollClass { - + @@ -202,7 +228,7 @@ class PollWithVotes extends PollClass { - + Votes @@ -223,7 +249,7 @@ class PollWithVotes extends PollClass { - + @@ -234,7 +260,7 @@ class PollWithVotes extends PollClass { - + Votes @@ -255,7 +281,7 @@ class PollWithVotes extends PollClass { - + @@ -266,7 +292,7 @@ class PollWithVotes extends PollClass { - + Votes @@ -287,7 +313,7 @@ class PollWithVotes extends PollClass { - + @@ -298,7 +324,7 @@ class PollWithVotes extends PollClass { - + Votes @@ -319,18 +345,18 @@ class PollWithVotes extends PollClass { - - + + 6. {this.props.post.optionSix || "Couldn't load option six"} - + - + Votes @@ -384,9 +410,6 @@ class PollWithVotes extends PollClass { - {typeof message === 'string' || message instanceof String && ( - {message} - )} Total Votes: {this.props.post.optionOnesVotes + this.props.post.optionTwosVotes + this.props.post.optionThreesVotes + this.props.post.optionFoursVotes + this.props.post.optionFivesVotes + this.props.post.optionSixesVotes} diff --git a/screens/ViewPollPostPage.js b/screens/ViewPollPostPage.js index 7dfde04..14a735f 100644 --- a/screens/ViewPollPostPage.js +++ b/screens/ViewPollPostPage.js @@ -1,4 +1,4 @@ -import React, {useState, useContext} from 'react'; +import React, {useState, useContext, useEffect} from 'react'; import { StatusBar } from 'expo-status-bar'; // formik @@ -66,7 +66,7 @@ import AsyncStorage from '@react-native-async-storage/async-storage'; import { CredentialsContext } from './../components/CredentialsContext'; import { View, ImageBackground, ScrollView, SectionList, ActivityIndicator, StyleSheet, TouchableOpacity, Text, Image } from 'react-native'; -import { useTheme } from '@react-navigation/native'; +import { useIsFocused, useTheme } from '@react-navigation/native'; import { ProfilePictureURIContext } from '../components/ProfilePictureURIContext'; import { ServerUrlContext } from '../components/ServerUrlContext.js'; @@ -74,34 +74,18 @@ import { StatusBarHeightContext } from '../components/StatusBarHeightContext'; import ParseErrorMessage from '../components/ParseErrorMessage'; import { getTimeFromUTCMS } from '../libraries/Time'; import PollWithVotes from '../components/Posts/PollWithVotes'; +import usePostReducer from '../hooks/usePostReducer'; const ViewPollPostPage = ({route, navigation}) => { - const {colors, dark} = useTheme(); - const [hidePassword, setHidePassword] = useState(true); + const [postReducer, dispatch] = usePostReducer(); + const [deleted, setDeleted] = useState(false) + const {colors, dark, colorsIndexNum} = useTheme(); const [message, setMessage] = useState(); const [messageType, setMessageType] = useState(); const {storedCredentials, setStoredCredentials} = useContext(CredentialsContext); if (storedCredentials) {var {name } = storedCredentials} else {var {name } = {name: 'SSGUEST'}}; - const { pollTitle, pollSubTitle, optionOne, optionOnesColor, optionOnesVotes, optionOnesBarLength, optionTwo, optionTwosColor, optionTwosVotes, optionTwosBarLength, optionThree, optionThreesColor, optionThreesVotes, optionThreesBarLength, optionFour, optionFoursColor, optionFoursVotes, optionFoursBarLength, optionFive, optionFivesColor, optionFivesVotes, optionFivesBarLength, optionSix, optionSixesColor, optionSixesVotes, optionSixesBarLength, totalNumberOfOptions, pollLikes, pollId, votedFor, creatorPfpB64, creatorName, creatorDisplayName, datePosted} = route.params; - //Titles use states - var allDataCollected = {data: {pollTitle: pollTitle, pollSubTitle: pollSubTitle, optionOne: optionOne, optionOnesColor: optionOnesColor, optionOnesVotes: optionOnesVotes, optionOnesBarLength: optionOnesBarLength, optionTwo: optionTwo, optionTwosColor: optionTwosColor, optionTwosVotes: optionTwosVotes, optionTwosBarLength: optionTwosBarLength, optionThree: optionThree, optionThreesColor: optionThreesColor, optionThreesVotes: optionThreesVotes, optionThreesBarLength: optionThreesBarLength, optionFour: optionFour, optionFoursColor: optionFoursColor, optionFoursVotes: optionFoursVotes, optionFoursBarLength: optionFoursBarLength, optionFive: optionFive, optionFivesColor: optionFivesColor, optionFivesVotes: optionFivesVotes, optionFivesBarLength: optionFivesBarLength, optionSix: optionSix, optionSixesColor: optionSixesColor, optionSixesVotes: optionSixesVotes, optionSixesBarLength: optionSixesBarLength, totalNumberOfOptions: totalNumberOfOptions, pollLikes: pollLikes, pollId: pollId, votedFor: votedFor, creatorPfpB64: creatorPfpB64, creatorName: creatorName, creatorDisplayName: creatorName, datePosted}} - const [objectForAllData, setObjectForAllData] = useState(allDataCollected) - // - const [optionOneInfoState, setOptionOneInfoState] = useState(false) - const [optionTwoInfoState, setOptionTwoInfoState] = useState(false) - const [optionThreeInfoState, setOptionThreeInfoState] = useState(false) - const [optionFourInfoState, setOptionFourInfoState] = useState(false) - const [optionFiveInfoState, setOptionFiveInfoState] = useState(false) - const [optionSixInfoState, setOptionSixInfoState] = useState(false) - const [optionOneVoteText, setOptionOneVoteText] = useState("Vote") - const [optionTwoVoteText, setOptionTwoVoteText] = useState("Vote") - const [optionThreeVoteText, setOptionThreeVoteText] = useState("Vote") - const [optionFourVoteText, setOptionFourVoteText] = useState("Vote") - const [optionFiveVoteText, setOptionFiveVoteText] = useState("Vote") - const [optionSixVoteText, setOptionSixVoteText] = useState("Vote") - const [limitVoteTextChange, setLimitVoteTextChange] = useState(false) - const [likeSubmitting, setLikeSubmitting] = useState(false) + const { post, isOwner } = route.params; //Comment stuff const [ifCommentText, setIfCommentText] = useState("No comments found") const [changeSections, setChangeSections] = useState([]) @@ -110,18 +94,6 @@ const ViewPollPostPage = ({route, navigation}) => { const [commentLoadMax, setCommentLoadMax] = useState(10) const [commentsLength , setCommentsLength] = useState("Loading") const [loadingMoreComments, setLoadingMoreComments] = useState(false) - //change stuff - const [pollUpOrDownVotes, setPollUpOrDownVotes] = useState("Finding") - const [initialPollUpOrDownVotes, setInitialPollUpOrDownVotes] = useState("Finding") - const [pollUpvoted, setPollUpvoted] = useState("Finding") - const [pollDownvoted, setPollDownvoted] = useState("Finding") - const [pollInitiallyUpvoted, setPollInitiallyUpvoted] = useState("Finding") - const [pollInitiallyDownvoted, setPollInitiallyDownvoted] = useState("Finding") - const [pollVotesForOptions, setPollVotesForOptions] = useState({optionOne: "Finding", optionTwo: "Finding", optionThree: "Finding", optionFour: "Finding", optionFive: "Finding", optionSix: "Finding"}) - const [initialPollVotesForOptions, setInitialPollVotesForOptions] = useState({optionOne: "Finding", optionTwo: "Finding", optionThree: "Finding", optionFour: "Finding", optionFive: "Finding", optionSix: "Finding"}) - const [pollBarLengths, setPollBarLengths] = useState({optionOnesBarLength: 0, optionTwosBarLength: 0, optionThreesBarLength: 0, optionFoursBarLength: 0, optionFivesBarLength:0, optionSixesBarLength: 0}) - const [pollVoteOption, setPollVoteOption] = useState("Finding") - const [pollinitialVoteOption, setPollinitialVoteOption] = useState("Finding") //PFP const {profilePictureUri, setProfilePictureUri} = useContext(ProfilePictureURIContext) //Server stuff @@ -129,165 +101,16 @@ const ViewPollPostPage = ({route, navigation}) => { const StatusBarHeight = useContext(StatusBarHeightContext); - //get image of post - async function getImageInPost(imageData, index) { - return axios.get(`${serverUrl}/getImageOnServer/${imageData[index].imageKey}`) - .then(res => 'data:image/jpeg;base64,' + res.data); - } - //profile image of creator - async function getImageInPfp(imageData, index) { - return axios.get(`${serverUrl}/getImageOnServer/${imageData[index].creatorPfpKey}`) - .then(res => 'data:image/jpeg;base64,' + res.data); - } + useEffect(() => { + dispatch({type: 'addPosts', posts: [post]}) + }, []) + //profile image of commenter async function getImageInPfpComments(commentData, index) { return axios.get(`${serverUrl}/getImageOnServer/${commentData[index].profileImageKey}`) .then(res => 'data:image/jpeg;base64,' + res.data); } - const layoutPollPosts = (data) => { - console.log("here is data") - console.log(data) - var pollData = data.data - console.log(pollData) - console.log(pollData.length) - var optionOnesBarLength = 16.6666666667 - var optionTwosBarLength = 16.6666666667 - var optionThreesBarLength = 16.6666666667 - var optionFoursBarLength = 16.6666666667 - var optionFivesBarLength = 16.6666666667 - var optionSixesBarLength = 16.6666666667 - var totalVotes = pollData[0].optionOnesVotes+pollData[0].optionTwosVotes+pollData[0].optionThreesVotes+pollData[0].optionFoursVotes+pollData[0].optionFivesVotes+pollData[0].optionSixesVotes - if (totalVotes !== 0) { - optionOnesBarLength = (pollData[0].optionOnesVotes/totalVotes)*100 - console.log("O1 BL") - console.log(optionOnesBarLength) - optionTwosBarLength = (pollData[0].optionTwosVotes/totalVotes)*100 - console.log("O2 BL") - console.log(optionTwosBarLength) - optionThreesBarLength = (pollData[0].optionThreesVotes/totalVotes)*100 - console.log("O3 BL") - console.log(optionThreesBarLength) - optionFoursBarLength = (pollData[0].optionFoursVotes/totalVotes)*100 - console.log("O4 BL") - console.log(optionFoursBarLength) - optionFivesBarLength = (pollData[0].optionFivesVotes/totalVotes)*100 - console.log("O5 BL") - console.log(optionFivesBarLength) - optionSixesBarLength = (pollData[0].optionSixesVotes/totalVotes)*100 - console.log("O6 BL") - console.log(optionSixesBarLength) - if (Number.isNaN(optionOnesBarLength)) { - optionOnesBarLength = 0 - } - if (Number.isNaN(optionTwosBarLength)) { - optionTwosBarLength = 0 - } - if (Number.isNaN(optionThreesBarLength)) { - optionThreesBarLength = 0 - } - if (Number.isNaN(optionFoursBarLength)) { - optionFoursBarLength = 0 - } - if (Number.isNaN(optionFivesBarLength)) { - optionFivesBarLength = 0 - } - if (Number.isNaN(optionSixesBarLength)) { - optionSixesBarLength = 0 - } - setPollBarLengths({optionOnesBarLength: optionOnesBarLength, optionTwosBarLength: optionTwosBarLength, optionThreesBarLength: optionThreesBarLength, optionFoursBarLength: optionFoursBarLength, optionFivesBarLength: optionFivesBarLength, optionSixesBarLength: optionSixesBarLength}) - } else { - if (totalVotes == 0) { - console.log("No Votes") - if (pollData[0].totalNumberOfOptions == "Two") { - optionOnesBarLength = 100/2 - optionTwosBarLength = 100/2 - optionThreesBarLength = 0 - optionFoursBarLength = 0 - optionFivesBarLength = 0 - optionSixesBarLength = 0 - setPollBarLengths({optionOnesBarLength: optionOnesBarLength, optionTwosBarLength: optionTwosBarLength, optionThreesBarLength: optionThreesBarLength, optionFoursBarLength: optionFoursBarLength, optionFivesBarLength: optionFivesBarLength, optionSixesBarLength: optionSixesBarLength}) - } else if (pollData[0].totalNumberOfOptions == "Three") { - optionOnesBarLength = 100/3 - optionTwosBarLength = 100/3 - optionThreesBarLength = 100/3 - optionFoursBarLength = 0 - optionFivesBarLength = 0 - optionSixesBarLength = 0 - setPollBarLengths({optionOnesBarLength: optionOnesBarLength, optionTwosBarLength: optionTwosBarLength, optionThreesBarLength: optionThreesBarLength, optionFoursBarLength: optionFoursBarLength, optionFivesBarLength: optionFivesBarLength, optionSixesBarLength: optionSixesBarLength}) - } else if (pollData[0].totalNumberOfOptions == "Four") { - optionOnesBarLength = 100/4 - optionTwosBarLength = 100/4 - optionThreesBarLength = 100/4 - optionFoursBarLength = 100/4 - optionFivesBarLength = 0 - optionSixesBarLength = 0 - setPollBarLengths({optionOnesBarLength: optionOnesBarLength, optionTwosBarLength: optionTwosBarLength, optionThreesBarLength: optionThreesBarLength, optionFoursBarLength: optionFoursBarLength, optionFivesBarLength: optionFivesBarLength, optionSixesBarLength: optionSixesBarLength}) - } else if (pollData[0].totalNumberOfOptions == "Five") { - optionOnesBarLength = 100/5 - optionTwosBarLength = 100/5 - optionThreesBarLength = 100/5 - optionFoursBarLength = 100/5 - optionFivesBarLength = 100/5 - optionSixesBarLength = 0 - setPollBarLengths({optionOnesBarLength: optionOnesBarLength, optionTwosBarLength: optionTwosBarLength, optionThreesBarLength: optionThreesBarLength, optionFoursBarLength: optionFoursBarLength, optionFivesBarLength: optionFivesBarLength, optionSixesBarLength: optionSixesBarLength}) - } else if (pollData[0].totalNumberOfOptions == "Six") { - optionOnesBarLength = 100/6 - optionTwosBarLength = 100/6 - optionThreesBarLength = 100/6 - optionFoursBarLength = 100/6 - optionFivesBarLength = 100/6 - optionSixesBarLength = 100/6 - setPollBarLengths({optionOnesBarLength: optionOnesBarLength, optionTwosBarLength: optionTwosBarLength, optionThreesBarLength: optionThreesBarLength, optionFoursBarLength: optionFoursBarLength, optionFivesBarLength: optionFivesBarLength, optionSixesBarLength: optionSixesBarLength}) - } - } - } - } - - //Change to voted - const prepareVotedandUpVoted = () => { - const changeVoteTexts = (data) => { - //set initial values - const pollData = data.data[0] - setPollUpvoted(pollData.upvoted) - setPollDownvoted(pollData.downvoted) - setPollInitiallyUpvoted(pollData.upvoted) - setPollInitiallyDownvoted(pollData.downvoted) - setPollUpOrDownVotes(pollData.pollUpOrDownVotes) - setPollVoteOption(pollData.votedFor) - setPollinitialVoteOption(pollData.votedFor) - setInitialPollVotesForOptions({optionOne: pollData.optionOnesVotes, optionTwo: pollData.optionTwosVotes, optionThree: pollData.optionThreesVotes, optionFour: pollData.optionFoursVotes, optionFive: pollData.optionFivesVotes, optionSix: pollData.optionSixesVotes}) - setPollVotesForOptions({optionOne: pollData.optionOnesVotes, optionTwo: pollData.optionTwosVotes, optionThree: pollData.optionThreesVotes, optionFour: pollData.optionFoursVotes, optionFive: pollData.optionFivesVotes, optionSix: pollData.optionSixesVotes}) - } - const url = serverUrl + "/tempRoute/searchforpollpostsbyid"; - - var toSend = {"pollId": pollId} - - axios.post(url, toSend).then((response) => { - const result = response.data; - const {message, status, data} = result; - - if (status !== 'SUCCESS') { - handleMessage(message, status); - } else { - changeVoteTexts({data}); - layoutPollPosts({data}); - } - //setSubmitting(false); - }).catch(error => { - console.error(error); - //setSubmitting(false); - handleMessage(ParseErrorMessage(error)); - }) - } - - if (limitVoteTextChange == false) { - setLimitVoteTextChange(true); - prepareVotedandUpVoted(); - } - - // - const Item = ({commentId, commenterName, commenterDisplayName, commentsText, commentUpVotes, commentReplies, datePosted, commenterImageB64}) => ( @@ -325,7 +148,7 @@ const ViewPollPostPage = ({route, navigation}) => { {datePosted} - {navigation.navigate("CommentViewPage", {commentId: commentId, postId: pollId, postFormat: "Poll"})}}> + {navigation.navigate("CommentViewPage", {commentId: commentId, postId: post._id, postFormat: "Poll"})}}> {commentReplies} replies @@ -389,7 +212,7 @@ const ViewPollPostPage = ({route, navigation}) => { const url = `${serverUrl}/tempRoute/searchforpollcomments`; const toSend = { - pollId + pollId: post._id } setLoadingMoreComments(true) axios.post(url, toSend).then((response) => { @@ -446,133 +269,19 @@ const ViewPollPostPage = ({route, navigation}) => { setMessageType(type); } - const UpVotePoll = () => { - if (storedCredentials) { - //Change to loading circle - const beforeChange = pollUpOrDownVoted - setPollUpOrDownVoted("Changing") - //Do rest - handleMessage(null, null, null); - const url = serverUrl + "/tempRoute/upvotepoll"; + const isFocused = useIsFocused(); - var toSend = {pollId: pollId} - - console.log(toSend) - - axios.post(url, toSend).then((response) => { - const result = response.data; - const {message, status, data} = result; - - if (status !== 'SUCCESS') { - handleMessage(message, status, postNum); - setPollUpOrDownVoted(beforeChange) - } else { - handleMessage(message, status); - if (message == "Post UpVoted") { - setPollUpOrDownVoted("UpVoted") - } else { - setPollUpOrDownVoted("Neither") - } - //loadAndGetValues() - //persistLogin({...data[0]}, message, status); - } - }).catch(error => { - console.error(error); - setPollUpOrDownVoted(beforeChange) - handleMessage(ParseErrorMessage(error), 'FAILED', postNum); - }) - } else { - navigation.navigate('ModalLoginScreen', {modal: true}) - } - } - - const DownVotePoll = () => { - if (storedCredentials) { - //Change to loading circle - const beforeChange = pollUpOrDownVoted - setPollUpOrDownVoted("Changing") - //Do rest - handleMessage(null, null, null); - const url = serverUrl + "/tempRoute/downvotepoll"; - - var toSend = {pollId: pollId} - - console.log(toSend) - - axios.post(url, toSend).then((response) => { - const result = response.data; - const {message, status, data} = result; - - if (status !== 'SUCCESS') { - handleMessage(message, status, postNum); - setPollUpOrDownVoted(beforeChange) - } else { - handleMessage(message, status); - if (message == "Post DownVoted") { - setPollUpOrDownVoted("DownVoted") - } else { - setPollUpOrDownVoted("Neither") - } - //loadAndGetValues() - //persistLogin({...data[0]}, message, status); - } - }).catch(error => { - console.error(error); - setPollUpOrDownVoted(beforeChange) - handleMessage(ParseErrorMessage(error), 'FAILED', postNum); - }) - } else { - navigation.navigate('ModalLoginScreen', {modal: true}) - } - } - - const openOptionOne = () => { - if (optionOneInfoState !== true) { - setOptionOneInfoState(true) + const onDeleteCallback = () => { + if (isFocused) { + navigation.goBack(); } else { - setOptionOneInfoState(false) + setDeleted(true) } } - const openOptionTwo = () => { - if (optionTwoInfoState !== true) { - setOptionTwoInfoState(true) - } else { - setOptionTwoInfoState(false) - } - } - - const openOptionThree = () => { - if (optionThreeInfoState !== true) { - setOptionThreeInfoState(true) - } else { - setOptionThreeInfoState(false) - } - } - - const openOptionFour = () => { - if (optionFourInfoState !== true) { - setOptionFourInfoState(true) - } else { - setOptionFourInfoState(false) - } - } - - const openOptionFive = () => { - if (optionFiveInfoState !== true) { - setOptionFiveInfoState(true) - } else { - setOptionFiveInfoState(false) - } - } - - const openOptionSix = () => { - if (optionSixInfoState !== true) { - setOptionSixInfoState(true) - } else { - setOptionSixInfoState(false) - } - } + useEffect(() => { + if (isFocused && deleted) navigation.goBack(); + }, [isFocused]) return( <> @@ -586,18 +295,18 @@ const ViewPollPostPage = ({route, navigation}) => { resizeMethod="resize" /> - {creatorDisplayName ? creatorDisplayName : creatorName}'s poll + {(post.creatorDisplayName ? post.creatorDisplayName : post.creatorName) || 'ERROR'}'s poll - + {postReducer.posts.length > 0 ? : null} {storedCredentials ? Comments { if (values.comment == "") { handleMessage('You cant post and empty comment'); From aa7c9b3d030ad408e8869b392264000582826d63 Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Fri, 15 Sep 2023 16:42:28 +1200 Subject: [PATCH 07/10] Added threeDotsMenu to PollPost --- components/Posts/PollWithVotes.js | 17 ++++++++++++++++- screens/ViewPollPostPage.js | 4 +++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/components/Posts/PollWithVotes.js b/components/Posts/PollWithVotes.js index 248469f..55f4682 100644 --- a/components/Posts/PollWithVotes.js +++ b/components/Posts/PollWithVotes.js @@ -129,6 +129,21 @@ class PollWithVotes extends PollClass { return true; } + openThreeDotsMenu = () => { + if (this.props.post.isOwner !== true && this.props.post.isOwner !== false) { + alert("isOwner is not true or false. An error has occured.") + return + } + + this.props.dispatch({ + type: 'showMenu', + postId: this.props.post._id, + postFormat: 'Poll', + isOwner: this.props.post.isOwner, + postIndex: this.props.index + }) + } + render() { return ( <> @@ -406,7 +421,7 @@ class PollWithVotes extends PollClass { - + diff --git a/screens/ViewPollPostPage.js b/screens/ViewPollPostPage.js index 14a735f..1aa7559 100644 --- a/screens/ViewPollPostPage.js +++ b/screens/ViewPollPostPage.js @@ -75,6 +75,7 @@ import ParseErrorMessage from '../components/ParseErrorMessage'; import { getTimeFromUTCMS } from '../libraries/Time'; import PollWithVotes from '../components/Posts/PollWithVotes'; import usePostReducer from '../hooks/usePostReducer'; +import ThreeDotMenuActionSheet from '../components/Posts/ThreeDotMenuActionSheet'; const ViewPollPostPage = ({route, navigation}) => { @@ -284,7 +285,8 @@ const ViewPollPostPage = ({route, navigation}) => { }, [isFocused]) return( - <> + <> + {navigation.goBack()}}> From 17f4d70e1264fbbcd714d55ed6099795bca7a703 Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Fri, 15 Sep 2023 19:14:34 +1200 Subject: [PATCH 08/10] Fixed profile picture not showing in ViewPollPostPage --- components/Posts/PollWithVotes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/Posts/PollWithVotes.js b/components/Posts/PollWithVotes.js index 55f4682..4abb8ec 100644 --- a/components/Posts/PollWithVotes.js +++ b/components/Posts/PollWithVotes.js @@ -156,7 +156,7 @@ class PollWithVotes extends PollClass { - + {this.props.post.creatorDisplayName} From c01a2942f48ffacb6dffd60bdae2fd1dbd976acd Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Sat, 16 Sep 2023 00:09:16 +1200 Subject: [PATCH 09/10] Poll vote menus now open --- components/Posts/PollWithVotes.js | 41 ++++++++++++++++--------------- hooks/usePostReducer.js | 14 ++++++++++- 2 files changed, 34 insertions(+), 21 deletions(-) diff --git a/components/Posts/PollWithVotes.js b/components/Posts/PollWithVotes.js index 4abb8ec..321ca9d 100644 --- a/components/Posts/PollWithVotes.js +++ b/components/Posts/PollWithVotes.js @@ -39,28 +39,28 @@ class PollWithVotes extends PollClass { super(props); } - openOptionOne() { - alert('Coming soon') + openOptionOne = () => { + this.props.dispatch({type: 'openPollVoteMenu', openPollVoteMenu: "One", postIndex: this.props.index}) } - openOptionTwo() { - alert('Coming soon') + openOptionTwo = () => { + this.props.dispatch({type: 'openPollVoteMenu', openPollVoteMenu: "Two", postIndex: this.props.index}) } - openOptionThree() { - alert('Coming soon') + openOptionThree = () => { + this.props.dispatch({type: 'openPollVoteMenu', openPollVoteMenu: "Three", postIndex: this.props.index}) } - openOptionFour() { - alert('Coming soon') + openOptionFour = () => { + this.props.dispatch({type: 'openPollVoteMenu', openPollVoteMenu: "Four", postIndex: this.props.index}) } - openOptionFive() { - alert('Coming soon') + openOptionFive = () => { + this.props.dispatch({type: 'openPollVoteMenu', openPollVoteMenu: "Five", postIndex: this.props.index}) } - openOptionSix() { - alert('Coming soon') + openOptionSix = () => { + this.props.dispatch({type: 'openPollVoteMenu', openPollVoteMenu: "Six", postIndex: this.props.index}) } optionOneInfoState = false; //Temporary @@ -121,10 +121,11 @@ class PollWithVotes extends PollClass { const deletingIsSame = nextProps.post.deleting === this.props.post.deleting; const profilePictureIsSame = nextProps.post.pfpB64 === this.props.post.pfpB64; const changingPollVoteIsSame = nextProps.post.pollVoteChanging === this.props.post.pollVoteChanging; - const votedForIsSame = nextProps.post.votedFor === this.props.post.votedFor + const votedForIsSame = nextProps.post.votedFor === this.props.post.votedFor; + const openPollVoteMenuSame = nextProps.post.openPollVoteMenu === this.props.post.openPollVoteMenu; - if (upvoteIsSame && downvoteIsSame && changingVoteIsSame && colorsAreSame && pollIdIsSame && deletingIsSame && profilePictureIsSame && changingPollVoteIsSame && votedForIsSame) return false; + if (upvoteIsSame && downvoteIsSame && changingVoteIsSame && colorsAreSame && pollIdIsSame && deletingIsSame && profilePictureIsSame && changingPollVoteIsSame && votedForIsSame && openPollVoteMenuSame) return false; return true; } @@ -209,7 +210,7 @@ class PollWithVotes extends PollClass { - + Votes @@ -243,7 +244,7 @@ class PollWithVotes extends PollClass { - + Votes @@ -275,7 +276,7 @@ class PollWithVotes extends PollClass { - + Votes @@ -307,7 +308,7 @@ class PollWithVotes extends PollClass { - + Votes @@ -339,7 +340,7 @@ class PollWithVotes extends PollClass { - + Votes @@ -371,7 +372,7 @@ class PollWithVotes extends PollClass { - + Votes diff --git a/hooks/usePostReducer.js b/hooks/usePostReducer.js index a5759df..7638e23 100644 --- a/hooks/usePostReducer.js +++ b/hooks/usePostReducer.js @@ -290,7 +290,19 @@ const reducer = (state, action) => { return {...state} } - throw new Error('Wrong action type was passed to usePostReducer') + if (action.type === 'openPollVoteMenu') { + if (typeof action.postIndex !== 'number') throw new Error('postIndex was not provided to openPollVoteMenu action of usePostReducer') + if (typeof state.posts[action.postIndex] !== 'object' || Array.isArray(state.posts[action.postIndex]) || state.posts[action.postIndex] === null) throw new Error(`Post at index ${action.postIndex} is not an object`) + + state.posts[action.postIndex] = { + ...state.posts[action.postIndex], + openPollVoteMenu: state.posts[action.postIndex].openPollVoteMenu === action.openPollVoteMenu ? "None" : action.openPollVoteMenu + } + + return {...state} + } + + throw new Error(`Wrong action type was passed to usePostReducer: ${action.type}`) } const initialState = { From cf49e921422e5b30daf11c91f58b4ec45c0242ba Mon Sep 17 00:00:00 2001 From: Sebastian Webster Date: Sat, 16 Sep 2023 08:35:57 +1200 Subject: [PATCH 10/10] Fixed bugs with PollWithVotes --- components/Posts/PollWithVotes.js | 55 ++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/components/Posts/PollWithVotes.js b/components/Posts/PollWithVotes.js index 321ca9d..207a53d 100644 --- a/components/Posts/PollWithVotes.js +++ b/components/Posts/PollWithVotes.js @@ -32,7 +32,8 @@ import { useNavigation } from '@react-navigation/native'; import { ServerUrlContext } from '../ServerUrlContext'; import { CredentialsContext } from '../CredentialsContext'; import ParseErrorMessage from '../ParseErrorMessage'; -import Octicons from 'react-native-vector-icons/Octicons' +import Octicons from 'react-native-vector-icons/Octicons'; +import axios from 'axios'; class PollWithVotes extends PollClass { constructor(props) { @@ -217,10 +218,14 @@ class PollWithVotes extends PollClass { {this.props.post.optionOnesVotes} - {this.props.post.votedFor === "One" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionOnesVotes", "One")}}> + {this.props.post.votedFor === "One" ? this.handleRemoveVoteOnPoll() : this.handleVoteOnPoll("optionOnesVotes", "One")}} disabled={this.props.post.pollVoteChanging}> Option One - {this.props.post.votedFor === "One" ? "Voted" : "Vote"} + {this.props.post.pollVoteChanging ? + + : + {this.props.post.votedFor === "One" ? "Voted" : "Vote"} + } @@ -251,10 +256,14 @@ class PollWithVotes extends PollClass { {this.props.post.optionTwosVotes} - {this.props.post.votedFor === "Two" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionTwosVotes", "Two")}}> + {this.props.post.votedFor === "Two" ? this.handleRemoveVoteOnPoll() : this.handleVoteOnPoll("optionTwosVotes", "Two")}}> Option Two - {this.props.post.votedFor === "Two" ? "Voted" : "Vote"} + {this.props.post.pollVoteChanging ? + + : + {this.props.post.votedFor === "Two" ? "Voted" : "Vote"} + } @@ -283,10 +292,14 @@ class PollWithVotes extends PollClass { {this.props.post.optionThreesVotes} - {this.props.post.votedFor === "Three" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionThreesVotes", "Three")}}> + {this.props.post.votedFor === "Three" ? this.handleRemoveVoteOnPoll() : this.handleVoteOnPoll("optionThreesVotes", "Three")}}> Option Three - {this.props.post.votedFor === "Three" ? "Voted" : "Vote"} + {this.props.post.pollVoteChanging ? + + : + {this.props.post.votedFor === "Three" ? "Voted" : "Vote"} + } @@ -315,10 +328,14 @@ class PollWithVotes extends PollClass { {this.props.post.optionFoursVotes} - {this.props.post.votedFor === "Four" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionFoursVotes", "Four")}}> + {this.props.post.votedFor === "Four" ? this.handleRemoveVoteOnPoll() : this.handleVoteOnPoll("optionFoursVotes", "Four")}}> Option Four - {this.props.post.votedFor === "Four" ? "Voted" : "Vote"} + {this.props.post.pollVoteChanging ? + + : + {this.props.post.votedFor === "Four" ? "Voted" : "Vote"} + } @@ -347,10 +364,14 @@ class PollWithVotes extends PollClass { {this.props.post.optionFivesVotes} - {this.props.post.votedFor === "Five" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionFivesVotes", "Five")}}> + {this.props.post.votedFor === "Five" ? this.handleRemoveVoteOnPoll() : this.handleVoteOnPoll("optionFivesVotes", "Five")}}> Option Five - {this.props.post.votedFor === "Five" ? "Voted" : "Vote"} + {this.props.post.pollVoteChanging ? + + : + {this.props.post.votedFor === "Five" ? "Voted" : "Vote"} + } @@ -379,10 +400,14 @@ class PollWithVotes extends PollClass { {this.props.post.optionSixesVotes} - {this.props.post.votedFor === "Six" ? this.handleRemoveVoteOnPoll() : handleVoteOnPoll("optionSixesVotes", "Six")}}> + {this.props.post.votedFor === "Six" ? this.handleRemoveVoteOnPoll() : this.handleVoteOnPoll("optionSixesVotes", "Six")}}> Option Six - {this.props.post.votedFor === "Six" ? "Voted" : "Vote"} + {this.props.post.pollVoteChanging ? + + : + {this.props.post.votedFor === "Six" ? "Voted" : "Vote"} + } @@ -403,13 +428,13 @@ class PollWithVotes extends PollClass { : <> - + {this.props.post.votes} - + }