|
| 1 | +const m = require("mithril") |
| 2 | +const Scuttle = require("scuttle-gathering") |
| 3 | +const Form = require("../../core/components/daisyui/Form.js") |
| 4 | +const TextInput = require("../../core/components/daisyui/TextInput.js") |
| 5 | +const TextArea = require("../../core/components/daisyui/TextArea.js") |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +const gathering = Scuttle(ssb.sbot) |
| 10 | +const sbot = ssb.sbot |
| 11 | + |
| 12 | +const GatheringEdit = { |
| 13 | + oninit: vnode => { |
| 14 | + vnode.state.gatheringId = vnode.attrs.gatheringId || false |
| 15 | + }, |
| 16 | + view: vnode => { |
| 17 | + let gatheringId = vnode.state.gatheringId |
| 18 | + |
| 19 | + let title = vnode.state.title |
| 20 | + let startDateTime = vnode.state.startDateTime |
| 21 | + let description = vnode.state.description |
| 22 | + let location = vnode.state.location |
| 23 | + let image = vnode.state.image |
| 24 | + |
| 25 | + const createOrUpdateGathering = () => { |
| 26 | + let opts = { |
| 27 | + title, |
| 28 | + startDateTime, |
| 29 | + description, |
| 30 | + location, |
| 31 | + image, |
| 32 | + } |
| 33 | + |
| 34 | + const cb = (err, data) => { |
| 35 | + if (err) { |
| 36 | + // vnode.state.error = err |
| 37 | + throw err |
| 38 | + } else { |
| 39 | + // vnode.state.data = data |
| 40 | + if (data.key) { |
| 41 | + patchfox.go("calendar", "view", {msgid: data.key}) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + if (gatheringId) { |
| 47 | + gathering.put(gatheringId, opts, cb) |
| 48 | + } else { |
| 49 | + gathering.post(opts, cb) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + const setField = field => { |
| 54 | + const eventHandler = ev => { |
| 55 | + vnode.state[field] = ev.target.value |
| 56 | + } |
| 57 | + return eventHandler |
| 58 | + } |
| 59 | + |
| 60 | + const makeInput = field => { |
| 61 | + return m(TextInput, { |
| 62 | + label: field, |
| 63 | + onchange: setField(field), |
| 64 | + value: vnode.state[field] |
| 65 | + }) |
| 66 | + } |
| 67 | + |
| 68 | + return m(Form, { |
| 69 | + onSave: createOrUpdateGathering |
| 70 | + }, [ |
| 71 | + makeInput("title"), |
| 72 | + makeInput("startDateTime"), |
| 73 | + makeInput("location"), |
| 74 | + ]) |
| 75 | + |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +module.exports = GatheringEdit |
0 commit comments