1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-30 15:44:20 +01:00
ipfs-boards/pages/b/[board]/p/new.js

160 lines
4.1 KiB
JavaScript
Raw Normal View History

2019-10-26 23:58:20 +02:00
import React, { useState } from 'react'
import { openBoard } from '../../../../components/system'
import Router from 'next/router'
import { makeStyles } from '@material-ui/core/styles'
2019-11-13 01:02:56 +01:00
import { Typography, Box, Card, CardHeader, Divider, CardActions, CardContent, TextField, Button, Avatar, Tabs, Tab } from '@material-ui/core'
2019-10-26 23:58:20 +02:00
import SendIcon from '@material-ui/icons/Send'
import AddIcon from '@material-ui/icons/Add'
2019-10-26 23:58:20 +02:00
2019-11-13 01:02:56 +01:00
function TabPanel(props) {
const { children, value, index, ...other } = props
return (
<Typography
component="div"
role="tabpanel"
hidden={value !== index}
{...other}
>
<Box p={3}>{children}</Box>
</Typography>
)
}
const useStyles = makeStyles(theme => ({
card: {
maxWidth: 600,
margin: theme.spacing(2),
marginLeft: 'auto',
marginRight: 'auto'
},
field: {
marginTop: theme.spacing(2),
marginBottom: theme.spacing(2),
},
secondaryButton: {
marginLeft: theme.spacing(2)
},
buttonIcon: {
marginLeft: theme.spacing(1)
2019-11-13 01:02:56 +01:00
},
tabContainer: {
flexGrow: 1,
backgroundColor: theme.palette.background.paper,
display: 'flex',
},
tabPanel: {
flexGrow: 1
},
tabs: {
borderRight: `1px solid ${theme.palette.divider}`,
},
}))
async function createPost(boardId, postData) {
const board = await openBoard(boardId)
await board.addPost(postData)
Router.push(`/b/${boardId}`)
}
export default function CreatePost({ boardId }) {
const classes = useStyles()
2019-11-13 01:02:56 +01:00
// State
2019-10-26 23:58:20 +02:00
const [title, setTitle] = useState('')
2019-11-13 01:02:56 +01:00
const [cid, setCID] = useState('')
const [content, setContent] = useState('')
const [tab, setTab] = useState(0)
// Actions
const submitPost = () => {
const payload = { title }
if (tab === 0) payload.text = content
if (tab === 2) payload.multihash = cid
return createPost(boardId, payload)
}
return <Card className={classes.card}>
<CardHeader
avatar={<Avatar><AddIcon /></Avatar>}
title="Post Something"
subheader={<React.Fragment>
Your post will be published to the <b>{boardId}</b> board
</React.Fragment>}
/>
2019-10-26 23:58:20 +02:00
<CardContent>
<TextField
className={classes.field}
variant="outlined"
2019-10-26 23:58:20 +02:00
label="Title"
placeholder="What is your Post about?"
value={title}
onChange={e => setTitle(e.target.value)}
autoFocus
fullWidth
/>
2019-11-13 01:02:56 +01:00
</CardContent>
<Divider />
<div className={classes.tabContainer}>
<Tabs
orientation="vertical"
value={tab}
onChange={(event, value) => setTab(value)}
className={classes.tabs}
>
<Tab label="Text" />
<Tab label="Media" />
<Tab label="IPFS CID" />
</Tabs>
<TabPanel value={tab} index={0} className={classes.tabPanel}>
<TextField
multiline
fullWidth
placeholder="What's on your mind?"
rows={3}
variant="outlined"
label="Post"
value={content}
onChange={event => setContent(event.target.value)}
/>
</TabPanel>
<TabPanel value={tab} index={1} className={classes.tabPanel}>
WIP
</TabPanel>
<TabPanel value={tab} index={2} className={classes.tabPanel}>
<TextField
className={classes.field}
variant="filled"
label="CID"
placeholder="Paste the IPFS CID or your post content (WIP)"
value={cid}
onChange={e => setCID(e.target.value)}
helperText="Enter the CID or Multihash of existing IPFS content"
fullWidth
/>
</TabPanel>
</div>
<Divider />
<CardActions>
<Button
className={classes.secondaryButton}
onClick={() => Router.push(`/b/${boardId}`)}
style={{ marginLeft: 'auto' }}
>
Cancel
2019-10-26 23:58:20 +02:00
</Button>
2019-11-13 01:02:56 +01:00
<Button
variant="contained"
color="primary"
onClick={submitPost}
>
Submit <SendIcon className={classes.buttonIcon} />
</Button>
</CardActions>
2019-10-26 23:58:20 +02:00
</Card>
}
CreatePost.getInitialProps = ({ query }) => {
return { boardId: query.board }
2019-11-13 01:02:56 +01:00
}