Files: fdb2ee6f55290529cd2d95134844044dd149a47c / views / new.js
1801 bytesRaw
1 | const { h, Struct, Array: MutantArray, computed, resolve } = require('mutant') |
2 | const Invoke = require('./new-steps/invoke') |
3 | const PickTimes = require('./new-steps/pick-times') |
4 | |
5 | module.exports = function ScryNew (opts) { |
6 | const { |
7 | scuttle, |
8 | afterPublish = () => console.log('published scry poll') |
9 | } = opts |
10 | |
11 | const initialState = { |
12 | step: 0, |
13 | title: '', |
14 | closesAt: getInitialClosesAt(), |
15 | monthIndex: new Date().getMonth(), |
16 | days: MutantArray([]), |
17 | times: MutantArray([]) |
18 | } |
19 | const state = Struct(initialState) |
20 | |
21 | return h('ScryNew', [ |
22 | computed(state.step, step => { |
23 | switch (step) { |
24 | case 0: return Invoke({ |
25 | state, |
26 | next: () => state.step.set(step + 1) |
27 | }) |
28 | case 1: return PickTimes({ |
29 | state, |
30 | prev: () => state.step.set(step - 1), |
31 | next: publish |
32 | }) |
33 | } |
34 | }) |
35 | ]) |
36 | |
37 | function publish () { |
38 | const { title, days, times, closesAt } = resolve(state) |
39 | |
40 | const _days = days |
41 | .map(ev => ev.date) |
42 | .sort((a, b) => a - b) |
43 | .map(d => [d.getFullYear(), d.getMonth(), d.getDate()]) |
44 | const _times = times |
45 | .map(ev => ev.date) |
46 | .sort((a, b) => a - b) |
47 | .map(t => [t.getHours(), t.getMinutes()]) |
48 | const choices = _days.reduce((acc, d) => { |
49 | _times.forEach(t => { |
50 | acc.push(new Date(...d, ...t).toISOString()) |
51 | }) |
52 | |
53 | return acc |
54 | }, []) |
55 | |
56 | const opts = { |
57 | title, |
58 | choices, |
59 | closesAt: closesAt.toISOString() |
60 | } |
61 | |
62 | scuttle.poll.async.publishMeetingTime(opts, (err, data) => { |
63 | if (err) return console.error(err) |
64 | |
65 | state.set(initialState) |
66 | afterPublish(data) |
67 | }) |
68 | } |
69 | } |
70 | |
71 | function getInitialClosesAt () { |
72 | const d = new Date() |
73 | |
74 | return new Date(d.getFullYear(), d.getMonth(), d.getDate() + 3, 12) |
75 | } |
76 |
Built with git-ssb-web