Skip to content

Commit

Permalink
fix: Forking without picture bug, fixes #9 and fixes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
kewitz committed Dec 14, 2017
1 parent 084a4a1 commit 313f4ce
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
18 changes: 11 additions & 7 deletions app/components/Fork/Form.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { h } from 'hyperapp'
import { parseForm } from 'lib/helpers'

import 'style/fork.css'
import PhotoPicker from 'components/Fork/PhotoPicker'
Expand All @@ -8,10 +9,13 @@ export default ({ fork }) => {
const update = key => value => { setup.key = value }
const onSubmit = e => {
e.preventDefault()

const [ , image, author, title, description ] = Array.from(e.target).map(a => a.value)
const [ , ext, data ] = image.match(/data:image\/(\w+);base64,(.*)$/)
const photo = { ext, data }
const form = parseForm(e)
const { image, author, title, description } = form
let photo
if (image) {
const [ , ext, data ] = image.match(/data:image\/(\w+);base64,(.*)$/)
photo = { data, ext }
}
fork({ author, description, photo, title })
}

Expand All @@ -20,9 +24,9 @@ export default ({ fork }) => {
<form onsubmit={onSubmit}>
<h1>Fork new dat-medium</h1>
<PhotoPicker onload={update('image')} />
<input type='text' name='author' placeholder='Author' />
<input type='text' name='title' placeholder='Title' />
<input type='text' name='description' placeholder='Description' />
<input type='text' id='author' placeholder='Author' />
<input type='text' id='title' placeholder='Title' />
<input type='text' id='description' placeholder='Description' />
<input type='submit' class='button' value='Create' />
</form>
</section>
Expand Down
6 changes: 6 additions & 0 deletions app/lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,9 @@ export const appendStyle = css => {
style.innerHTML = css
document.head.appendChild(style)
}

export const parseForm = event =>
Array
.from(event.target)
.map(({ id, value }) => ({ [id]: value }))
.reduce((acc, v) => ({ ...acc, ...v }), {})
12 changes: 9 additions & 3 deletions app/lib/medium.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,18 @@ class Medium {

async fork ({ author, description, photo, title }) {
const fork = await window.DatArchive.fork(this.dat.url, { title, description })
const display = `/author.${photo.ext}`
// Clean Everything
await fork.unlink(this.blog.display)
await fork.rmdir('articles', { recursive: true })
// Create Files
const blog = { author }
await fork.mkdir('articles')
await fork.writeFile(display, photo.data, { encoding: 'base64' })
await fork.writeFile('/blog.json', JSON.stringify({ author, display }))
if (photo) {
blog.display = `/author.${photo.ext}`
await fork.writeFile(blog.display, photo.data, { encoding: 'base64' })
}
await fork.writeFile('/blog.json', JSON.stringify(blog))
// Create Default Post
const date = (new Date()).toISOString().slice(0, 10)
await fork.writeFile('articles/hello.md', `date: ${date}\ntitle: First Post\n\nThis is my first post.`)
await fork.commit()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dat-medium",
"version": "0.2.0",
"version": "0.2.1",
"description": "P2P blog platform inspired by Medium for Beaker.",
"author": "Leonardo Kewitz <leokewitz@gmail.com>",
"license": "MIT",
Expand Down

0 comments on commit 313f4ce

Please sign in to comment.