1
0
mirror of https://github.com/fazo96/ipfs-boards synced 2025-01-09 12:19:49 +01:00

can now kinda create a board

This commit is contained in:
Enrico Fasoli 2018-02-04 14:48:05 +01:00
parent 27cde7efe5
commit 29a876c193
No known key found for this signature in database
GPG Key ID: 1238873C5F27DB4D
13 changed files with 2749 additions and 111 deletions

View File

@ -3,6 +3,8 @@
"version": "0.1.0", "version": "0.1.0",
"private": true, "private": true,
"dependencies": { "dependencies": {
"ipfs": "^0.27.7",
"orbit-db": "^0.19.4",
"react": "^16.2.0", "react": "^16.2.0",
"react-dom": "^16.2.0", "react-dom": "^16.2.0",
"react-hot-loader": "^3.1.3", "react-hot-loader": "^3.1.3",

View File

@ -1,3 +1,5 @@
export const ADD_POST = 'ADD_POST' export const ADD_POST = 'ADD_POST'
export const CREATE_BOARD = 'CREATE_BOARD' export const CREATE_BOARD = 'CREATE_BOARD'
export const CREATING_BOARD = 'CREATING_BOARD'
export const CREATED_BOARD = 'CREATED_BOARD'

View File

@ -1,8 +1,26 @@
import { CREATE_BOARD } from './actionTypes' import {
CREATE_BOARD,
CREATING_BOARD,
CREATED_BOARD
} from './actionTypes'
export function createBoard(board) { export function createBoard(board) {
return { return {
type: CREATE_BOARD, type: CREATE_BOARD,
board board
} }
}
export function creatingBoard(board) {
return {
type: CREATING_BOARD,
board
}
}
export function createdBoard(board) {
return {
type: CREATED_BOARD,
board
}
} }

View File

@ -15,7 +15,7 @@ export default class BoardForm extends Component {
render() { render() {
const { title, content } = this.state const { title, content } = this.state
const { onSave } = this.props const { onSave, creating } = this.props
return <Form> return <Form>
<Form.Field> <Form.Field>
<label>Title</label> <label>Title</label>
@ -28,7 +28,9 @@ export default class BoardForm extends Component {
<Button <Button
type='submit' type='submit'
onClick={() => onSave({ title, content })} onClick={() => onSave({ title, content })}
disabled={creating}
>Create</Button> >Create</Button>
{creating ? 'Creating the board...' : ''}
</Form> </Form>
} }
} }

View File

@ -1,12 +1,12 @@
import React from 'react' import React from 'react'
import { List } from 'semantic-ui-react' import { List } from 'semantic-ui-react'
export default function BoardsItem(props) { export default function BoardsItem({ title }) {
return <List.Item> return <List.Item>
<List.Icon name='board' size='large' verticalAlign='middle' /> <List.Icon name='comments' size='large' verticalAlign='middle' />
<List.Content> <List.Content>
<List.Header>Board Name</List.Header> <List.Header>{title}</List.Header>
<List.Description>Updated 10 mins ago</List.Description> <List.Description>Experimental</List.Description>
</List.Content> </List.Content>
</List.Item> </List.Item>
} }

View File

@ -1,15 +0,0 @@
class BoardIndex {
constructor(id) {
super(id);
// this._index = []
}
get() {
throw new Error('Not implemented yet')
}
updateIndex(oplog, entries) {
throw new Error('Not implemented yet')
}
}

View File

@ -1,45 +0,0 @@
const Store = require('orbit-db-store')
const BoardIndex = require('./BoardIndex')
class Board extends Store {
constructor(ipfs, id, dbname, options) {
if (!options) options = {}
if (!options.indexBy) Object.assign(options, { indexBy: '_id' })
if (!options.Index) Object.assign(options, { Index: BoardIndex })
super(ipfs, id, dbname, options)
this._type = 'board'
}
get type() {
return 'discussion-board'
}
updateMetadata() {
throw new Error('Not implemented yet')
}
getPosts() {
throw new Error('Not implemented yet')
}
addPost(title, content) {
this._addOperation({
title,
content
})
}
updatePost() {
throw new Error('Not implemented yet')
}
addComment() {
throw new Error('Not implemented yet')
}
updateComment() {
throw new Error('Not implemented yet')
}
}
module.exports = Board

View File

@ -1,17 +1,46 @@
import BoardStore from './BoardStore' import BoardStore from 'orbit-db-discussion-board'
export async function open(address, options = {}) { export async function open(address, metadata, options = {}) {
if (!window.ipfs) { if (!window.ipfs) {
window.ipfs = await import('ipfs') const IPFS = await import('ipfs')
window.ipfs = new IPFS({
repo: 'ipfs-v6-boards-v0',
EXPERIMENTAL: {
pubsub: true
},
config: {
Addresses: {
Swarm: [
'/dns4/ws-star.discovery.libp2p.io/tcp/443/wss/p2p-websocket-star',
'/dns4/ws-star-signal-1.servep2p.com/tcp/443/wss/p2p-websocket-star',
'/dns4/ws-star-signal-2.servep2p.com/tcp/443/wss/p2p-websocket-star'
]
}
}
});
await new Promise(fullfill => {
window.ipfs.on('ready', () => fullfill())
})
} }
if (!window.orbitDb) { if (!window.orbitDb) {
const OrbitDB = await import('orbit-db') const OrbitDB = await import('orbit-db')
OrbitDB.addDatabaseType(BoardStore.type, BoardStore)
window.orbitDb = new OrbitDB(window.ipfs) window.orbitDb = new OrbitDB(window.ipfs)
window.orbitDb.addDatabaseType(BoardStore.type, BoardStore)
} }
const defaultOptions = { const defaultOptions = {
create: true, create: address === undefined,
type: BoardStore.type type: BoardStore.type
} }
return await window.orbitDb.open(address, Object.assign(defaultOptions, options)) if (!address) {
address = 'board-v0'
} else if (!address.indexOf('/orbitdb/') < 0 || address.indexOf('/board-v0') < 0) {
throw new Error('invalid address')
}
const db = await window.orbitDb.open(address, Object.assign(defaultOptions, options))
if (metadata) {
await db.updateMetadata(metadata)
}
if (!window.dbs) window.dbs = {}
window.dbs[db.address.toString()] = db
return db
} }

View File

@ -1,17 +1,26 @@
import { CREATE_BOARD } from '../actions/actionTypes' import {
CREATE_BOARD,
CREATING_BOARD,
CREATED_BOARD
} from '../actions/actionTypes'
function getInitialState() { function getInitialState() {
return { return {
board: { board: {
title: '' title: ''
} },
creating: false
} }
} }
export default function BoardEditorReducer(state = getInitialState(), action) { export default function BoardEditorReducer(state = getInitialState(), action) {
switch (action.type) { switch (action.type) {
case CREATE_BOARD: case CREATE_BOARD:
return Object.assign({}, state, { board: action.board }) return Object.assign({}, state, { board: action.board, creating: false })
case CREATING_BOARD:
return Object.assign({}, state, { creating: true })
case CREATED_BOARD:
return Object.assign({}, state, { creating: false })
default: default:
return state return state
} }

View File

@ -1,3 +1,4 @@
import { CREATED_BOARD } from '../actions/actionTypes'
function getInitialState() { function getInitialState() {
return { return {
@ -7,6 +8,8 @@ function getInitialState() {
export default function BoardsReducer(state = getInitialState(), action) { export default function BoardsReducer(state = getInitialState(), action) {
switch (action.type) { switch (action.type) {
case CREATED_BOARD:
return Object.assign({}, state, { boards: state.boards.concat(action.board) })
default: default:
return state; return state;
} }

View File

@ -1,4 +1,13 @@
import { put, call } from 'redux-saga/effects'
import { open } from '../orbitdb'
import { creatingBoard, createdBoard } from '../actions/board'
export default function* boards() { export function* createBoard({ board }) {
yield put(creatingBoard(board))
const db = yield call(open, board.address)
const dbInfo = {
address: db.address.toString()
}
// TODO watch db status
yield put(createdBoard(Object.assign({}, board, dbInfo)))
} }

View File

@ -1,4 +1,7 @@
import { takeEvery } from 'redux-saga/effects'
import { CREATE_BOARD } from '../actions/actionTypes'
import { createBoard } from './boards'
export default function* saga(){ export default function* saga(){
yield takeEvery(CREATE_BOARD, createBoard)
} }

2685
yarn.lock

File diff suppressed because it is too large Load Diff