This repository has been archived by the owner on Oct 28, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
132 lines (116 loc) · 3.69 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import A from "axios"
import * as F from "framework"
import K from "kefir"
import * as D from "selfdb"
import * as R from "ramda"
import React from "react"
import {validate} from "tcomb-validation"
import * as T from "common/types"
import PostForm from "./PostForm"
// SEED
export let seed = {
input: {
title: "",
text: "",
tags: "",
isPublished: false,
},
errors: {},
}
export default (sources, key) => {
let baseLens = ["posts"]
// INTENTS
let intents = {
changeTitle$: sources.DOM.fromName("title").listen("input")
.map(ee => ee.element.value),
changeText$: sources.DOM.fromName("text").listen("input")
.map(ee => ee.element.value),
changeTags$: sources.DOM.fromName("tags").listen("input")
.map(ee => ee.element.value),
changeIsPublished$: sources.DOM.fromName("isPublished").listen("click")
.map(ee => ee.element.checked),
submit$: sources.DOM.from("form").listen("submit")
.map(ee => (ee.event.preventDefault(), ee))
.map(R.always(true)),
}
// STATE
let form$ = D.run(
() => D.makeStore({}),
// D.withLog({key}),
)(
D.init(seed),
intents.changeTitle$.map(x => R.set(["input", "title"], x)),
intents.changeTitle$.debounce(200).map(x => {
let res = validate(x, T.PostForm.meta.props.title)
return res.isValid()
? R.unset(["errors", "title"])
: R.set(["errors", "title"], res.firstError().message)
}),
intents.changeText$.map(x => R.set(["input", "text"], x)),
intents.changeText$.debounce(200).map(x => {
let res = validate(x, T.PostForm.meta.props.text)
return res.isValid()
? R.unset(["errors", "text"])
: R.set(["errors", "text"], res.firstError().message)
}),
intents.changeTags$.map(x => R.set(["input", "tags"], x)),
intents.changeTags$.debounce(200).map(x => {
let res = validate(x, T.PostForm.meta.props.tags)
return res.isValid()
? R.unset(["errors", "tags"])
: R.set(["errors", "tags"], res.firstError().message)
}),
intents.changeIsPublished$.map(x => R.set(["input", "isPublished"], x)),
intents.changeIsPublished$.debounce(200).map(x => {
let res = validate(x, T.PostForm.meta.props.isPublished)
return res.isValid()
? R.unset(["errors", "isPublished"])
: R.set(["errors", "isPublished"], res.firstError().message)
}),
// Resets
intents.submit$.delay(1).map(_ => (form) => {
let res = validate(form.input, T.PostForm)
if (res.isValid()) {
return seed
} else {
let errors = R.reduce((z, key) => {
let e = R.find(e => R.equals(e.path, [key]), res.errors)
return e ? R.set(key, e.message, z) : z
}, {}, R.keys(form.input))
return R.set("errors", errors, form)
}
}),
).$
// COMPONENT
let Component = F.connect(
{
form: form$,
},
({form}) =>
<PostForm input={form.input} errors={form.errors}/>
)
// ACTION (external)
let action$ = K.merge([
form$.sampledBy(intents.submit$).flatMapConcat(form => {
let postForm
try {
postForm = T.PostForm(form.input)
} catch (e) {
return K.never()
}
return K.constant(postForm)
}).flatMapConcat(form => K
.fromPromise(A.post(`/api/posts/`, form))
.map(resp => resp.data.model)
)
.map(post => {
return function afterPOST(state) {
return R.set([...baseLens, post.id], post, state)
}
}).flatMapErrors(err => {
console.warn(`Request to "${err.response.config.url}" failed with message "${err.response.status} ${err.response.statusText}"`)
return K.never() // TODO add alert box
})
])
return {Component, action$}
}