Skip to content

Commit

Permalink
upgrade hyperapp, reducers & effects --> actions, subscriptions is an…
Browse files Browse the repository at this point in the history
… array, argument order changed, onupdate --> onUpdate
  • Loading branch information
arturi committed Mar 7, 2017
1 parent f4d7e9c commit 1594e04
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 37 deletions.
53 changes: 20 additions & 33 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,64 +120,54 @@ const model = {
showNewDoc: false
}

const subscriptions = {
initialize: (model, actions, error) => {
// console.log('Start like an animal!')
const subscriptions = [
(model, actions, error) => {
log('Start like an animal!')

window.onbeforeunload = (ev) => {
actions.saveState()
}
window.onbeforeunload = (ev) => actions.saveState()

actions.loadDocList()
.then(actions.loadLastOpenDoc)
}
}
]

const reducers = {
updateDocList: (model, data, params) => {
const actions = {
updateDocList: (model, data, actions, error) => {
return { docList: data }
},
updateDoc: (model, data, params) => {
updateDoc: (model, data, actions, error) => {
return { doc: data.doc, docId: data.docId }
},
showNewDoc: (model, data, params) => {
showNewDoc: (model, data, actions, error) => {
return { showNewDoc: !state.showNewDoc }
}
}

const effects = {
loadDocList: (model, actions, data, error) => {
return new Promise(function (resolve, reject) {
},
loadDocList: (model, data, actions, error) => {
return new Promise((resolve, reject) => {
api.getList((err, res) => {
if (err) reject(err)
// console.log('Loaded doc list')
log('Loaded doc list')
actions.updateDocList(res.data)
return resolve(res.data)
})
})
},
loadDoc: (model, actions, data, error) => {
loadDoc: (model, data, actions, error) => {
const docId = data || model.docList[0]
return new Promise(function (resolve, reject) {
return new Promise((resolve, reject) => {
api.getDoc(docId, (err, res) => {
if (err) reject(err)
// console.log('Loaded doc:', docId)
log('Loaded doc: ' + docId)
actions.updateDoc({ doc: res.data, docId: docId })
actions.updateEditor(model, actions, data, error)
resolve(res)
})
})
},
updateEditor: (model, actions, data, error) => {
updateEditor: (model, data, actions, error) => {
log('Updating editor')
// console.log('Updating editor')
editor.update(model.doc, editorEl)
},
saveDoc: (model, actions, data, error) => {
// console.log('Saving:', data.docId)
saveDoc: (model, data, actions, error) => {
log('Saving: ' + data.docId)
return new Promise(function (resolve, reject) {
api.saveDoc(data.docId, data.doc, (err, res) => {
Expand All @@ -188,25 +178,23 @@ const effects = {
})
})
},
newDoc: (model, actions, data, error) => {
newDoc: (model, data, actions, error) => {
const docId = data
// console.log('Creating new doc:', docId)
log('Creating new doc: ' + docId)

return actions.saveDoc({ docId: docId, doc: '' })
.then(actions.loadDocList)
.then(() => actions.loadDoc(docId))
.then(actions.showNewDoc)
},
saveState: (model, actions, data, error) => {
saveState: (model, data, actions, error) => {
localStorage.setItem('tentState', JSON.stringify(model))
},
loadLastOpenDoc: (model, actions, data, error) => {
loadLastOpenDoc: (model, data, actions, error) => {
const savedStateString = localStorage.getItem('tentState')
const savedState = JSON.parse(savedStateString)
let docId
if (savedState && savedState.docId) {
// console.log('Found some saved state:', savedState)
log('Found some saved state: ' + savedStateString)
docId = savedState.docId
}
Expand All @@ -224,7 +212,7 @@ function Editor (model, actions) {
_saveDoc(data, actions)
})

return html`<div oncreate=${(el) => {
return html`<div onCreate=${(el) => {
el.appendChild(editor.el)
editorEl = el
}}>
Expand Down Expand Up @@ -265,7 +253,6 @@ function view (model, actions) {
const myApp = app({
model: model,
view: view,
reducers: reducers,
effects: effects,
actions: actions,
subscriptions: subscriptions
})
6 changes: 3 additions & 3 deletions markdown-preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ function renderMarkdown (doc, opts) {
const attributes = parsedDoc.attributes || {}
const title = attributes.title ? `<h1>${parsedDoc.attributes.title}</h1>` : ''

element = html`<div class="${styles.mdBody}" onupdate=${(el) => {
element = html`<div class="${styles.mdBody}" onUpdate=${(el) => {
el.innerHTML = title + md.render(parsedDoc.body)
}}></div>`
} else {
doc = doc || ''
element = html`<div class="${styles.mdBody}" oncreate=${(el) => {
element = html`<div class="${styles.mdBody}" onUpdate=${(el) => {
el.innerHTML = el.innerHTML = md.render(doc)
}}>bla</div>`
}}></div>`
}

return element
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"fastmatter": "^1.1.1",
"fs-extra": "^1.0.0",
"glob": "^7.1.1",
"hyperapp": "^0.6.0",
"hyperapp": "^0.7.1",
"hyperx": "^2.0.5",
"lodash": "^4.17.3",
"lodash.debounce": "^4.0.8",
Expand Down

0 comments on commit 1594e04

Please sign in to comment.