todo-webclient/client/index.ls

170 lines
3.5 KiB
Plaintext
Raw Normal View History

2019-11-20 19:22:51 +01:00
# first:
# connection to authd
# display the authd widget
#
# second:
# connection to todod
# rewrite the whole body component
#
maquette = require "maquette"
2019-11-22 16:52:50 +01:00
nmd = require "nano-markdown"
authd = require "./authd.ls"
todows = require "./todowebsocket.ls"
bulma = require "./bulma.ls"
Task = require "./task.ls"
Project = require "./project.ls"
2019-11-20 19:22:51 +01:00
{create-projector, h} = maquette
projector = create-projector!
model = {
current-view: "login"
editing: undefined
selected: undefined
users: {}
projects: {}
list-todos: []
2019-11-22 16:52:50 +01:00
connection: {
port: 9999
url: undefined
socket: undefined
}
2019-11-20 19:22:51 +01:00
}
2019-11-22 16:52:50 +01:00
# '://' + location.hostname + \: +
model.connection.url =
(if location.protocol == 'https' then 'wss' else 'ws') +
"://www.junkos.netlib.re:" +
model.connection.port +
"/kanban.JSON"
2019-11-20 19:22:51 +01:00
2019-11-22 16:52:50 +01:00
console.log model.connection.url
2019-11-20 19:22:51 +01:00
2019-11-22 16:52:50 +01:00
#
# network configuration
#
socket = todows.create-socket model.connection.url
on-websocket-error = (event) ->
console.log "WebSocket error.", event
model.state := "network-error"
projector.schedule-render!
2019-11-20 19:22:51 +01:00
2019-11-22 16:52:50 +01:00
on-websocket-close = ->
2019-11-20 19:22:51 +01:00
model.current-view := "login"
2019-11-22 16:52:50 +01:00
# socket.reopen!
2019-11-20 19:22:51 +01:00
2019-11-22 16:52:50 +01:00
# Exporting the error in case the UI is able to deal with it.
model.previous-state := model.state
model.state := "network-error"
model.websocket-error := event.reason
projector.schedule-render!
on-websocket-message = (data) ->
message = JSON.parse data
2019-11-20 19:22:51 +01:00
switch message.type
when "login"
model.current-view := "list-todos"
when "list-projects"
for project in message.projects
2019-11-22 16:52:50 +01:00
model.projects[project.id] = Project.new project, model, socket
2019-11-20 19:22:51 +01:00
model.list-todos := message.projects
when "project"
2019-11-22 16:52:50 +01:00
model.projects[message.project.id] = Project.new message.project, model, socket
2019-11-20 19:22:51 +01:00
when "user"
if message.user
model.users[message.user.uid] := message.user
when "login-error"
model.login-error = message.error
else
console.log "RECEIVED UNKNOWN MESSAGE TYPE: #{message.type}"
projector.schedule-render!
console.log message
2019-11-22 16:52:50 +01:00
# record changes that need to happen on a network event
socket.user-on-error ++= [ on-websocket-error ]
socket.user-on-close ++= [ on-websocket-close ]
socket.user-on-message ++= [ on-websocket-message ]
#
# Pages:
# login
# todo-list
# todo
# network-error
#
2019-11-20 19:22:51 +01:00
renderer = ->
render-navbar = ->
h \div.navbar [
h \div.navbar-start [
h \a.navbar-item.is-size-1 {
onclick: ->
model.viewed-project := undefined
model.current-view := "list-todos"
} [ "⌂" ]
]
h \div.navbar-end [
h \a.navbar-item {
onclick: ->
model.current-view := "login"
socket.reopen!
} [ "Logout" ]
]
authd.login-widget model, socket
]
h \div.section [
switch model.current-view
when "login"
authd.login-page model, socket
when "list-todos"
h \div#list-todos [
render-navbar!
h \div.section (model.list-todos || []).map (project) ->
h \a.box {
key: project.id
onclick: ->
model.current-view := "project"
model.viewed-project := project.id
socket.get-project project.id
} [
bulma.title 3 project.name
]
h \div.button.is-primary.is-large.is-fullwidth {
onclick: ->
socket.new-project "Hello, there!"
} [ "New project! (random atm)" ]
]
when "project"
h \div [
render-navbar!
if model.projects[model.viewed-project]
model.projects[model.viewed-project].render!
]
else
h \div.notification.is-error [
"Wait, what? Internal error!"
]
]
document.add-event-listener 'DOMContentLoaded' ->
projector.append document.body, renderer