git ssb

16+

Dominic / patchbay



Commit fa78173db5d80d49155705dbb8f60d90da768df7

/calendar mvp display of events

mixmix committed on 7/20/2018, 8:51:30 AM
Parent: ee739c123cb53210216f3fe26a35eedcf2c6eecc

Files changed

app/page/calendar.jschanged
app/page/calendar.mcsschanged
app/page/calendar.jsView
@@ -1,34 +1,100 @@
11 const nest = require('depnest')
2-const { h, Value, computed } = require('mutant')
2 +const { h, Value, computed, Dict, throttle, dictToCollection } = require('mutant')
3 +const pull = require('pull-stream')
4 +const { isMsg } = require('ssb-ref')
35
46 exports.gives = nest('app.page.calendar')
57
68 exports.needs = nest({
9 + 'sbot.pull.stream': 'first'
710 })
811
912 exports.create = (api) => {
1013 return nest('app.page.calendar', calendarPage)
1114
1215 function calendarPage (location) {
13- const cal = Cal()
16 + const store = Dict({})
17 + pull(
18 + pullGatherings(2018),
19 + pull.drain(
20 + ({ key, date }) => store.put(key, date),
21 + (err) => {
22 + if (err) console.error(err)
23 + else console.log('DONE')
24 + }
25 + )
26 + )
1427
15- return h('CalendarPage', { title: '/calendar' }, cal)
28 + return h('CalendarPage', { title: '/calendar' }, [
29 + computed(throttle(dictToCollection(store), 150), collection => {
30 + const events = collection.map(item => {
31 + return {
32 + date: item.value,
33 + data: { key: item.key }
34 + }
35 + })
36 +
37 + return Calendar(events)
38 + })
39 + ])
1640 }
41 +
42 + function pullGatherings (year) {
43 + const query = [{
44 + $filter: {
45 + value: {
46 + timestamp: {$gt: Number(new Date(year, 0, 1))}, // ordered by published time
47 + content: {
48 + type: 'about',
49 + startDateTime: {
50 + epoch: {$gt: 0}
51 + }
52 + }
53 + }
54 + }
55 + }, {
56 + $map: {
57 + key: ['value', 'content', 'about'], // gathering
58 + date: ['value', 'content', 'startDateTime', 'epoch']
59 + }
60 + }]
61 +
62 + const opts = {
63 + reverse: false,
64 + query
65 + }
66 +
67 + return pull(
68 + api.sbot.pull.stream(server => server.query.read(opts)),
69 + pull.filter(r => isMsg(r.key) && Number.isInteger(r.date)),
70 + pull.map(r => {
71 + return { key: r.key, date: new Date(r.date) }
72 + })
73 + )
74 + }
1775 }
1876
77 +// ////////////////// extract below into a module ///////////////////////
78 +
79 +// Thanks to nomand for the inspiration and code (https://github.com/nomand/Letnice),
80 +// they formed the foundationf of this work
81 +
1982 const MONTHS = [ 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December' ]
2083 const DAYS = [ 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday' ]
2184
22-function Cal () {
85 +function Calendar (events) {
86 + // TODO assert events is an Array of object
87 + // of form { date, data }
88 +
2389 const year = Value(new Date().getFullYear())
2490 const today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate())
2591 // const today = new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1, 0)
2692
2793 const root = h('Calendar', [
2894 Header(year),
2995 h('div.months', computed(year, year => {
30- return MONTHS.map((month, monthIndex) => Month({ month, monthIndex, year, today }))
96 + return MONTHS.map((month, monthIndex) => Month({ month, monthIndex, year, today, events }))
3197 }))
3298 ])
3399
34100 return root
@@ -44,15 +110,16 @@
44110 // h('p.percentage', yearProgress(year))
45111 ])
46112 }
47113
48-function Month ({ month, monthIndex, year, today }) {
114 +function Month ({ month, monthIndex, year, today, events }) {
49115 const monthLength = new Date(year, monthIndex + 1, 0).getDate()
50116 // NOTE Date takes month as a monthIndex i.e. april = 3
51117 // and day = 0 goes back a day
52118 const days = Array(monthLength).fill().map((_, i) => i + 1)
53119
54120 var date
121 + var dateEnd
55122 var weekday
56123 var week
57124 var offset = getDay(new Date(year, monthIndex, 1)) - 1
58125
@@ -65,20 +132,26 @@
65132 ])
66133
67134 function Day (day) {
68135 date = new Date(year, monthIndex, day)
136 + dateEnd = new Date(year, monthIndex, day + 1)
69137 weekday = getDay(date)
70138 week = Math.ceil((day + offset) / 7)
71139
140 + const eventsOnDay = events.filter(e => {
141 + return e.date >= date && e.date < dateEnd
142 + })
143 +
72144 return h('CalendarDay', {
73145 attributes: { 'data-date': `${year}-${monthIndex + 1}-${day}` },
74146 style: {
75147 'grid-row': `${weekday} / ${weekday + 1}`,
76148 'grid-column': `${week + 1} / ${week + 2}`
77149 // column moved by 1 to make space for labels
78150 },
79151 classList: [
80- date < today ? '-past' : '-future'
152 + date < today ? '-past' : '-future',
153 + eventsOnDay.length ? '-events' : ''
81154 ]
82155 })
83156 }
84157 }
@@ -98,4 +171,5 @@
98171
99172 // Weeks run 0...6 (Sun - Sat)
100173 // this shifts those days around by 1
101174 }
175 +
app/page/calendar.mcssView
@@ -111,8 +111,16 @@
111111
112112 -future {
113113 background: hsl(0, 0%, 80%)
114114 }
115 +
116 + -events {
117 + border-radius: var(--day-width)
118 + /* background: hsla(277, 57%, 45%, 1) */
119 +
120 + -past {
121 + }
122 + }
115123 }
116124
117125 CalendarDayName {
118126 color: hsl(0, 0%, 40%)

Built with git-ssb-web