From db03079b55bdb06633e7ef3df8818b1345724c8e Mon Sep 17 00:00:00 2001 From: Enrico Fasoli Date: Fri, 9 Feb 2018 01:20:53 +0100 Subject: [PATCH] added board editing form and fixed metadata retrieval --- src/actions/actionTypes.js | 7 +++-- src/actions/board.js | 9 +++--- src/components/Board.js | 2 +- src/components/BoardEditorForm.js | 49 +++++++++++++++++++++++++++++++ src/components/BoardPage.js | 2 +- src/components/Boards.js | 2 +- src/components/BoardsItem.js | 4 +-- src/containers/BoardEditor.js | 36 +++++++++++++++++++++++ src/containers/WithStats.js | 7 ++--- src/sagas/boards.js | 22 +++++++++----- src/sagas/index.js | 7 +++-- src/utils/ipfs.js | 6 ++++ 12 files changed, 128 insertions(+), 25 deletions(-) create mode 100644 src/components/BoardEditorForm.js create mode 100644 src/containers/BoardEditor.js diff --git a/src/actions/actionTypes.js b/src/actions/actionTypes.js index 9a80f1b..0202029 100644 --- a/src/actions/actionTypes.js +++ b/src/actions/actionTypes.js @@ -1,15 +1,16 @@ export const ADD_POST = 'ADD_POST' +export const UPDATE_BOARD_METADATA = 'UPDATE_BOARD_METADATA' export const OPEN_BOARD = 'OPEN_BOARD' export const OPENED_BOARD = 'OPENED_BOARD' export const UPDATE_BOARD = 'UPDATE_BOARD' -export const BOARD_ERROR = 'BOARD_ERROR' - export const ORBITDB_WRITE = 'ORBITDB_WRITE' export const ORBITDB_REPLICATE = 'ORBITDB_REPLICATE' export const ORBITDB_REPLICATE_PROGRESS = 'ORBITDB_REPLICATE_PROGRESS' -export const ORBITDB_REPLICATED = 'ORBITDB_REPLICATED' \ No newline at end of file +export const ORBITDB_REPLICATED = 'ORBITDB_REPLICATED' + +export const ERROR = 'ERROR' \ No newline at end of file diff --git a/src/actions/board.js b/src/actions/board.js index 3b0f484..2859142 100644 --- a/src/actions/board.js +++ b/src/actions/board.js @@ -1,7 +1,7 @@ import { OPEN_BOARD, OPENED_BOARD, - BOARD_ERROR + UPDATE_BOARD_METADATA } from './actionTypes' export function openBoard(board) { @@ -18,9 +18,10 @@ export function createdBoard(board) { } } -export function boardError(error) { +export function updateBoardMetadata(address, metadata) { return { - type: BOARD_ERROR, - error + type: UPDATE_BOARD_METADATA, + address, + metadata } } \ No newline at end of file diff --git a/src/components/Board.js b/src/components/Board.js index ff5a9c0..0a1df45 100644 --- a/src/components/Board.js +++ b/src/components/Board.js @@ -90,7 +90,7 @@ export default function Board({ address, posts, metadata, replicating, stats, re - + + + + + } + + updateTitle(event) { + const title = event.target.value + this.setState({ title }) + } +} \ No newline at end of file diff --git a/src/components/BoardPage.js b/src/components/BoardPage.js index 3977df6..aa2c44d 100644 --- a/src/components/BoardPage.js +++ b/src/components/BoardPage.js @@ -1,7 +1,7 @@ import React from 'react' import { Switch, Route } from 'react-router-dom' import Board from '../containers/Board' -import BoardEditor from '../containers/Board' +import BoardEditor from '../containers/BoardEditor' import PostEditor from '../containers/PostEditor' import WithStats from '../containers/WithStats' diff --git a/src/components/Boards.js b/src/components/Boards.js index dc228f2..93c6ad3 100644 --- a/src/components/Boards.js +++ b/src/components/Boards.js @@ -7,7 +7,7 @@ export default function Boards({ stats, boards, createBoard }) { return
- + IPFS Boards Experimental Build diff --git a/src/components/BoardsItem.js b/src/components/BoardsItem.js index 6f34f3a..90ea0b4 100644 --- a/src/components/BoardsItem.js +++ b/src/components/BoardsItem.js @@ -3,11 +3,11 @@ import { List, Button, Card } from 'semantic-ui-react' import { Link } from 'react-router-dom' import { shortenAddress } from '../utils/orbitdb' -export default function BoardsItem({ address, title, name }) { +export default function BoardsItem({ address, metadata, name }) { return - { title || 'Unnamed board' } + { metadata.title || 'Unnamed board' } Board diff --git a/src/containers/BoardEditor.js b/src/containers/BoardEditor.js new file mode 100644 index 0000000..c2c001e --- /dev/null +++ b/src/containers/BoardEditor.js @@ -0,0 +1,36 @@ + +import React from 'react' +import { connect } from 'react-redux' +import BoardEditorForm from '../components/BoardEditorForm' +import { updateBoardMetadata } from '../actions/board' +import { getBoardAddress } from '../utils/orbitdb' + +function BoardEditor({ boards, boardEditor, match, updateBoardMetadata }) { + const { hash, name } = match.params + const address = getBoardAddress(hash, name) + const board = boards[address] + return +} + +function mapStateToProps(state){ + return { + boards: state.boards.boards, + boardEditor: state.boardEditor + } +} + +function mapDispatchToProps(dispatch) { + return { + updateBoardMetadata: (address, metadata) => dispatch(updateBoardMetadata(address, metadata)) + } +} + +export default connect( + mapStateToProps, + mapDispatchToProps +)(BoardEditor) diff --git a/src/containers/WithStats.js b/src/containers/WithStats.js index 2861c4a..80b55e4 100644 --- a/src/containers/WithStats.js +++ b/src/containers/WithStats.js @@ -12,7 +12,7 @@ export default function(WrappedComponent) { pubKey: '?', dbs: {} }, - interval: null + timeout: null } } @@ -23,10 +23,9 @@ export default function(WrappedComponent) { } refreshDelayed() { - const timeout = setTimeout(() => { + this.timeout = setTimeout(() => { this.refresh() }, 2000) - this.setState({ timeout }) } componentDidMount() { @@ -34,7 +33,7 @@ export default function(WrappedComponent) { } componentWillUnmount() { - if (this.state.interval) clearInterval(this.state.interval) + if (this.timeout) clearTimeout(this.timeout) } render() { diff --git a/src/sagas/boards.js b/src/sagas/boards.js index ba4bdf3..d2545ff 100644 --- a/src/sagas/boards.js +++ b/src/sagas/boards.js @@ -1,8 +1,8 @@ -import { put, call, fork, take } from 'redux-saga/effects' +import { put, call, fork, take, apply } from 'redux-saga/effects' import { eventChannel } from 'redux-saga' import { push } from 'react-router-redux' import { open, connectDb } from '../orbitdb' -import { createdBoard, boardError } from '../actions/board' +import { createdBoard } from '../actions/board' import { shortenAddress } from '../utils/orbitdb' import { UPDATE_BOARD } from '../actions/actionTypes' @@ -16,25 +16,33 @@ export function* updateBoard({ address }){ type: UPDATE_BOARD, address, posts: db.posts, - metadata: db.metadata, - syncRequestsReceived: db._stats.syncRequestsReceived, - opLogLength: db._oplog.length + metadata: Object.assign({}, db._index._index.metadata) // TODO: fix in lib and use db.metadata }) } +export function* updateBoardMetadata({ address, metadata }){ + const db = window.dbs[address] + if (db) { + yield apply(db, db.updateMetadata, [metadata]) + yield goToBoard({ board: { address } }); + } else { + yield put({ type: 'ERROR', error: address + ' not found' }) + } +} + export function* openBoard({ board }) { let db try { const metadata = board.title ? { title: board.title } : null db = yield call(open, board.address, metadata) } catch (error) { - yield put(boardError(error)) + yield put({ type: 'ERROR', error }) } if (db) { const address = db.address.toString() const dbInfo = { address } dbInfo.posts = db.posts - dbInfo.metadata = db.metadata + dbInfo.metadata = Object.assign({}, db._index._index.metadata) // TODO: fix in lib and use db.metadata dbInfo.name = db.dbname try { const channel = yield call(createDbEventChannel, db) diff --git a/src/sagas/index.js b/src/sagas/index.js index 5ab63f1..ca2a91f 100644 --- a/src/sagas/index.js +++ b/src/sagas/index.js @@ -1,12 +1,15 @@ import { takeEvery } from 'redux-saga/effects' -import { OPEN_BOARD, OPENED_BOARD, ADD_POST, ORBITDB_REPLICATED, ORBITDB_WRITE } from '../actions/actionTypes' -import { openBoard, updateBoard, goToBoard } from './boards' +import { OPEN_BOARD, OPENED_BOARD, ADD_POST, ORBITDB_REPLICATED, ORBITDB_WRITE, UPDATE_BOARD_METADATA } from '../actions/actionTypes' +import { openBoard, updateBoard, goToBoard, updateBoardMetadata } from './boards' import { addPost } from './posts' export default function* saga(){ yield takeEvery(OPEN_BOARD, openBoard) yield takeEvery(OPENED_BOARD, goToBoard) + yield takeEvery(ADD_POST, addPost) + yield takeEvery(UPDATE_BOARD_METADATA, updateBoardMetadata) + yield takeEvery(ORBITDB_WRITE, updateBoard) yield takeEvery(ORBITDB_REPLICATED, updateBoard) } \ No newline at end of file diff --git a/src/utils/ipfs.js b/src/utils/ipfs.js index a41cd4a..2097b30 100644 --- a/src/utils/ipfs.js +++ b/src/utils/ipfs.js @@ -18,12 +18,16 @@ export async function getStats() { const dbs = {} const stats = {} if (ipfs) { + stats.ipfsLoaded = true const peers = await ipfs.swarm.peers() const id = await ipfs.id() stats.peers = peers.map(p => p.peer.id._idB58String) stats.id = id.id + } else { + stats.ipfsLoaded = false } if (orbitDb) { + stats.orbitDbLoaded = true stats.pubKey = await orbitDb.key.getPublic('hex') Object.values(window.dbs || {}).forEach(db => { let writeable = db.access.write.indexOf('*') >= 0 || db.access.write.indexOf(stats.pubKey) >= 0 @@ -43,6 +47,8 @@ export async function getStats() { } dbs[db.address] = dbInfo }) + } else { + stats.orbitDbLoaded = false } stats.dbs = dbs return stats