From c330e1cb34a70a566c42b247fdf3737d4da1ba69 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Thu, 23 Jul 2020 16:04:22 -0400 Subject: [PATCH] add initial poll scafold --- dapp/src/App.tsx | 4 +- dapp/src/components/ListPolls.tsx | 27 ++++++----- dapp/src/components/Poll.tsx | 75 +++++++++++++++++++++++++++++++ dapp/src/styles/listPolls.js | 8 ++++ dapp/src/styles/poll.js | 38 ++++++++++++++++ dapp/src/types.ts | 3 -- 6 files changed, 141 insertions(+), 14 deletions(-) create mode 100644 dapp/src/components/Poll.tsx create mode 100644 dapp/src/styles/poll.js diff --git a/dapp/src/App.tsx b/dapp/src/App.tsx index 09ef41f..f991001 100644 --- a/dapp/src/App.tsx +++ b/dapp/src/App.tsx @@ -8,6 +8,7 @@ import { SetAccount } from './types' import Header from './components/Header' import CreatePoll from './components/CreatePoll' import ListPolls from './components/ListPolls' +import Poll from './components/Poll' import { MessagesProvider } from './context/messages/context' declare global { @@ -74,7 +75,8 @@ function App() { - + + diff --git a/dapp/src/components/ListPolls.tsx b/dapp/src/components/ListPolls.tsx index 2120578..84663d4 100644 --- a/dapp/src/components/ListPolls.tsx +++ b/dapp/src/components/ListPolls.tsx @@ -1,19 +1,21 @@ import React, { Fragment, useEffect, useState, useContext } from 'react' +import { Link } from 'react-router-dom' import classnames from 'classnames' import { gotoPublicChat, getChatMessages, useChatMessages } from '../utils/status' -import { MessagesContext, IMessagesContext } from '../context/messages/context' +import { MessagesContext } from '../context/messages/context' import { verifySignedMessage } from '../utils/signing' -import { Topics, Message, ISignedMessage, IEnrichedMessage, IPollInfo, IFormattedDate } from '../types' +import { Topics, Message, ISignedMessage, IPollInfo, IFormattedDate } from '../types' import Typography from '@material-ui/core/Typography' import StatusButton from './base/Button' import useStyles from '../styles/listPolls' import { getFromIpfs } from '../utils/ipfs' import { POLLS_CHANNEL } from './constants' import { getFormattedDate } from '../utils/dates' +import { getNetwork } from '../utils/network' async function gotoPolls() { await gotoPublicChat(POLLS_CHANNEL) @@ -24,11 +26,18 @@ async function parseEnrichMessages(messages: Topics, setState: Function) { const parsed: Topics | undefined = await parseMessages(messages) if (!parsed) return const rawPolls: Message[] = parsed['polls'] - const polls: Message[] = await enrichMessages(rawPolls) + const network = await getNetwork() + const enrichedPolls: Message[] = await enrichMessages(rawPolls) + const polls = enrichedPolls.filter( + p => { + if (!!p.pollInfo && !!p.pollInfo.network) { + return p.pollInfo.network === network + } + }) setState({ ...parsed, polls }) } -async function enrichMessages(messages: Message[]) { +async function enrichMessages(messages: Message[]): Promise { const updated = messages.map(async (message): Promise => { const { sigMsg } = message if (!message || !sigMsg || !sigMsg.msg) return message @@ -79,7 +88,6 @@ const isOdd = (num: number): boolean => !!(num % 2) function TableCards({ polls }: ITableCard) { const classes: any = useStyles() const { cardText, cellColor } = classes - console.log({polls}) return ( @@ -97,7 +105,9 @@ function TableCards({ polls }: ITableCard) { {title} {description} {plainText} - Vote now + + Vote now + ) @@ -108,10 +118,8 @@ function TableCards({ polls }: ITableCard) { function ListPolls() { const [rawMessages] = useChatMessages() - //const [chatMessages, setChatMessages] = useState() - const [enrichedPolls, setEnrichedPolls] = useState() const messagesContext = useContext(MessagesContext) - const { dispatchSetPolls, dispatchSetTopics, chatMessages } = messagesContext + const { dispatchSetTopics, chatMessages } = messagesContext const classes: any = useStyles() @@ -123,7 +131,6 @@ function ListPolls() { if (rawMessages && dispatchSetTopics) parseEnrichMessages(rawMessages, dispatchSetTopics) }, [rawMessages]) - console.log({chatMessages, rawMessages, messagesContext}) return (
diff --git a/dapp/src/components/Poll.tsx b/dapp/src/components/Poll.tsx new file mode 100644 index 0000000..3265282 --- /dev/null +++ b/dapp/src/components/Poll.tsx @@ -0,0 +1,75 @@ +import React, { Fragment, useContext } from 'react' +import useStyles from '../styles/poll' +import { useParams } from "react-router-dom" +import { MessagesContext } from '../context/messages/context' +import Typography from '@material-ui/core/Typography' +import TextField from '@material-ui/core/TextField' +import MenuItem from '@material-ui/core/MenuItem' +import { Formik, FormikProps } from 'formik' + +type IBallot = { + option: string +} + +function Poll() { + const { id } = useParams() + const classes: any = useStyles() + const messagesContext = useContext(MessagesContext) + const { chatMessages } = messagesContext + console.log({chatMessages}) + if (!chatMessages) return + const selectedPoll= chatMessages['polls'].find(p => p.messageId === id) + if (!selectedPoll || !selectedPoll.pollInfo) return + const { pollInfo } = selectedPoll + const { description, title, subtitle, pollOptions } = pollInfo + const options = pollOptions.split(',') + // TODO Display ^above data + // TODO Add method to vote + //TODO vote options are drop down menu + // TODO Add method to tabulate votes + console.log({selectedPoll, options}) + + return ( + { + console.log({values}) + }} + > + {({ + values, + errors, + handleChange, + handleSubmit + }: FormikProps) => { + return ( +
+ {title} + {subtitle} + {description} + + {options.map((option) => ( + + {option} + + ))} + +
+ )} + } +
+ ) +} + +export default Poll diff --git a/dapp/src/styles/listPolls.js b/dapp/src/styles/listPolls.js index 606dab9..32ec762 100644 --- a/dapp/src/styles/listPolls.js +++ b/dapp/src/styles/listPolls.js @@ -32,6 +32,14 @@ const useStyles = makeStyles(theme => ({ }, voteNow: { color: theme.palette.primary[500] + }, + link: { + gridColumn: '1 / 49', + padding: 0, + textDecoration: 'none', + '&:hover': { + textDecoration: 'underline' + } } })) diff --git a/dapp/src/styles/poll.js b/dapp/src/styles/poll.js new file mode 100644 index 0000000..03c1e09 --- /dev/null +++ b/dapp/src/styles/poll.js @@ -0,0 +1,38 @@ +import { makeStyles } from '@material-ui/core/styles' + +const useStyles = makeStyles(theme => ({ + root: { + display: 'grid', + gridTemplateColumns: 'repeat(48, [col] 1fr)', + gridColumn: '3 / 45', + [theme.breakpoints.up('md')]: { + gridTemplateRows: '4rem 4rem auto auto 6rem', + gridColumn: '8 / 42', + } + }, + title: { + gridColumn: '3 / 45', + fontSize: '3rem' + }, + subtitle: { + gridColumn: '2 / 45', + fontSize: '2rem' + }, + description: { + gridColumn: '3 / 45', + fontSize: '1rem' + }, + dropDown: { + gridColumn: '3 / 45' + }, + link: { + gridColumn: '1 / 49', + padding: 0, + textDecoration: 'none', + '&:hover': { + textDecoration: 'underline' + } + } +})) + +export default useStyles diff --git a/dapp/src/types.ts b/dapp/src/types.ts index 73fc083..2a27724 100644 --- a/dapp/src/types.ts +++ b/dapp/src/types.ts @@ -33,9 +33,6 @@ export type Message = { export type Topics = { [chat: string]: Message[] } -export interface IEnrichedMessage extends ISignedMessage { - pollInfo: IPollInfo -} export type IFormattedDate = { plainText: string, daysRemaining: number,