1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-26 15:04:19 +01:00

start using real data in UI

This commit is contained in:
Enrico Fasoli 2019-10-26 23:58:20 +02:00
parent a42d0e9226
commit d5ea93722a
No known key found for this signature in database
GPG Key ID: 1238873C5F27DB4D
7 changed files with 7315 additions and 26 deletions

3
.gitignore vendored
View File

@ -4,6 +4,9 @@
# build # build
.next/ .next/
# data
orbitdb/
# misc # misc
.DS_Store .DS_Store

View File

@ -23,8 +23,8 @@ export async function getIPFS() {
export async function getOrbitDB() { export async function getOrbitDB() {
const data = getGlobalData() const data = getGlobalData()
if (data.orbitDb) return data.orbitDb if (data.orbitDb) return data.orbitDb
const OrbitDB = await import('orbit-db') const OrbitDB = await import('orbit-db').then(m => m.default)
const BoardStore = await import('orbit-db-discussion-board') const BoardStore = await import('orbit-db-discussion-board').then(m => m.default)
OrbitDB.addDatabaseType(BoardStore.type, BoardStore) OrbitDB.addDatabaseType(BoardStore.type, BoardStore)
data.orbitDb = await OrbitDB.createInstance(await getIPFS()) data.orbitDb = await OrbitDB.createInstance(await getIPFS())
data.boards = {} data.boards = {}
@ -32,15 +32,35 @@ export async function getOrbitDB() {
} }
export async function openBoard(id) { export async function openBoard(id) {
const data = getGlobalData()
if (data.boards && data.boards[id]) return data.boards[id] if (data.boards && data.boards[id]) return data.boards[id]
const BoardStore = await import('orbit-db-discussion-board') const BoardStore = await import('orbit-db-discussion-board').then(m => m.default)
const options = { const options = {
type: BoardStore.type, type: BoardStore.type,
create: true, create: true,
write: ['*'] write: ['*']
} }
const orbitDb = await getOrbitDB() const orbitDb = await getOrbitDB()
const db = await orbitDb.open(id) const db = await orbitDb.open(id, options)
data.boards[id] = db data.boards[id] = db
await db.load()
return db return db
} }
const defaultLocalStorage = {
favouriteBoards: ['general', 'test']
}
export function getLocalStorage() {
try {
return window.localStorage
} catch (error) {
const data = getGlobalData()
if (!data.localStorage) data.localStorage = { ...defaultLocalStorage }
return {
getItem: name => data.localStorage[name],
setItem: (name, value) => data.localStorage[name] = value,
removeItem: name => delete data.localStorage[name],
}
}
}

7195
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -2,24 +2,53 @@ import React from 'react'
import { Fab, Card, CardActions, CardHeader, Button } from '@material-ui/core' import { Fab, Card, CardActions, CardHeader, Button } from '@material-ui/core'
import AddIcon from '@material-ui/icons/Add' import AddIcon from '@material-ui/icons/Add'
import ViewIcon from '@material-ui/icons/Visibility' import ViewIcon from '@material-ui/icons/Visibility'
import { openBoard } from '../../../components/system'
import Router from 'next/router'
const Board = () => { class Board extends React.PureComponent {
return ( state = { posts: [] }
<React.Fragment>
<Card> componentDidMount() {
<CardHeader this.refreshPosts()
title="First Post" }
subheader="Last Activity X Time Ago"
/> async refreshPosts() {
<CardActions> const { boardId } = this.props
<Button variant="contained" color="primary"> const board = await openBoard(boardId)
<ViewIcon /> View this.setState({ posts: board.posts })
</Button> }
</CardActions>
</Card> render() {
<Fab color="primary"><AddIcon /></Fab> const { boardId } = this.props
</React.Fragment> const posts = this.state.posts || this.props.posts
) return (
<React.Fragment>
{posts.map(p => <Card key={p.multihash}>
<CardHeader
title={p.title}
subheader="Last Activity X Time Ago"
/>
<CardActions>
<Button variant="contained" color="primary" onClick={() => Router.push(`/b/${boardId}/p/${p.multihash}`)}>
<ViewIcon /> View
</Button>
</CardActions>
</Card>)}
{posts.length === 0 && <Card>
<CardHeader
title="No Posts Yet"
subheader="Why don't you break the ice?"
/>
</Card>}
<Fab color="primary" onClick={() => Router.push(`/b/${boardId}/p/new`)}><AddIcon /></Fab>
</React.Fragment>
)
}
}
Board.getInitialProps = async ({ query }) => {
const board = await openBoard(query.board)
return { posts: await board.posts, boardId: query.board }
} }
export default Board export default Board

21
pages/b/[board]/p/new.js Normal file
View File

@ -0,0 +1,21 @@
import React, { useState } from 'react'
import { Card, CardContent, TextField, Button } from '@material-ui/core'
import SendIcon from '@material-ui/icons/Send'
export default function CreatePost() {
const [title, setTitle] = useState('')
return <Card>
<CardContent>
<TextField
label="Title"
placeholder="What is your Post about?"
value={title}
onChange={e => setTitle(e.target.value)}
autoFocus
/>
<Button variant="contained" color="primary">
<SendIcon /> Submit
</Button>
</CardContent>
</Card>
}

21
pages/b/open.js Normal file
View File

@ -0,0 +1,21 @@
import React, { useState } from 'react'
import { Card, CardContent, TextField, Button } from '@material-ui/core'
import OpenIcon from '@material-ui/icons/Add'
export default function OpenBoard() {
const [name, setName] = useState('')
return <Card>
<CardContent>
<TextField
label="Board"
placeholder="Type a name..."
value={name}
onChange={e => setName(e.target.value)}
autoFocus
/>
<Button variant="contained" color="primary">
<OpenIcon /> Open
</Button>
</CardContent>
</Card>
}

View File

@ -3,17 +3,19 @@ import { Fab, Card, CardActions, CardHeader, Button } from '@material-ui/core'
import AddIcon from '@material-ui/icons/Add' import AddIcon from '@material-ui/icons/Add'
import ViewIcon from '@material-ui/icons/Visibility' import ViewIcon from '@material-ui/icons/Visibility'
import DeleteIcon from '@material-ui/icons/Delete' import DeleteIcon from '@material-ui/icons/Delete'
import Link from 'next/link'
import Router from 'next/router'
const Home = () => { const Home = () => {
return ( return (
<React.Fragment> <React.Fragment>
<Card> <Card>
<CardHeader <CardHeader
title="First Board" title="Test Board"
subheader="Last Activity X time ago" subheader="Last Activity X time ago"
/> />
<CardActions> <CardActions>
<Button variant="contained" color="primary"> <Button variant="contained" color="primary" onClick={() => Router.push(`/b/test`)}>
<ViewIcon /> View <ViewIcon /> View
</Button> </Button>
<Button variant="contained" color="secondary"> <Button variant="contained" color="secondary">
@ -21,7 +23,9 @@ const Home = () => {
</Button> </Button>
</CardActions> </CardActions>
</Card> </Card>
<Fab color="primary"><AddIcon /></Fab> <Link href="/b/open">
<Fab color="primary"><AddIcon /></Fab>
</Link>
</React.Fragment> </React.Fragment>
) )
} }