Skip to content

Instantly share code, notes, and snippets.

@rleibman
Last active January 7, 2019 22:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rleibman/f065b53d90ba846d93865f6e3967be01 to your computer and use it in GitHub Desktop.
Save rleibman/f065b53d90ba846d93865f6e3967be01 to your computer and use it in GitHub Desktop.
/*
* Copyright 2018 Roberto Leibman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.leibman.semanticcalendar
import japgolly.scalajs.react._
import japgolly.scalajs.react.component.Scala
import japgolly.scalajs.react.extra.StateSnapshot
import japgolly.scalajs.react.vdom.html_<^._
import scala.scalajs.js
object SemanticCalendar {
trait Resource {
def id: Int
def name: String
}
trait Event {
def id: Int
def name: String
def description: String
def startDate: js.Date
def endDate: js.UndefOr[js.Date]
def allDay: Boolean
def toolTip: String
def resources: Seq[Resource]
}
case class EventImpl(id: Int,
name: String,
description: String,
startDate: js.Date,
endDate: js.UndefOr[js.Date] = js.undefined,
allDay: Boolean = false,
toolTip: String = "",
resources: Seq[Resource] = Seq.empty)
extends Event
object Nav extends Enumeration {
type Nav = Value
val specific, back, next = Value
}
trait View {
def name: String
def title: String
def apply(
props: Props,
stateSnapshot: StateSnapshot[CalendarState]
): Scala.Unmounted[Props, StateSnapshot[CalendarState], _]
}
case class AgendaView(
override val name: String,
override val title: String
) extends View {
class Backend($ : BackendScope[Props, StateSnapshot[CalendarState]]) {
def render(props: Props, state: StateSnapshot[CalendarState]) =
<.div("Agenda View")
}
def apply(
props: Props,
stateSnapshot: StateSnapshot[CalendarState]
): Scala.Unmounted[Props, StateSnapshot[CalendarState], _] = {
val component =
ScalaComponent
.builder[Props]("AgendaView")
.initialState(stateSnapshot)
.renderBackend[Backend]
.componentWillReceiveProps { e =>
e.modState(s => e.state) //Probably wrong
}
.build
component(props)
}
}
case class CalendarState(currentView: View,
startDate: Option[js.Date] = None,
endDate: Option[js.Date] = None,
currentDate: Option[js.Date] = None)
case class Props(views: Seq[View], events: (js.Date, js.Date) => Seq[Event])
class Backend($ : BackendScope[Props, CalendarState]) {
def init(state: CalendarState): Callback = Callback.empty
def onChangeFromCalendarView()(opt: Option[CalendarState], callback: Callback): Callback =
Callback.empty
def renderHeader() = <.div("Header")
def renderFooter() = <.div("Footer")
def render(props: Props, state: CalendarState) =
<.div(
renderHeader(),
state.currentView(props, StateSnapshot(state)(onChangeFromCalendarView())),
renderFooter()
)
}
private val component = ScalaComponent
.builder[Props]("SemanticCalendar")
.initialStateFromProps(props => CalendarState(props.views.head))
.renderBackend[Backend]
.componentDidMount($ ⇒ $.backend.init($.state))
.componentWillReceiveProps { _ =>
Callback.empty
}
.build
def apply[E <: Event, R <: Resource](
events: (js.Date, js.Date) => Seq[E],
currentDate: js.UndefOr[js.Date] = js.undefined,
minDate: js.UndefOr[js.Date] = js.undefined,
maxDate: js.UndefOr[js.Date] = js.undefined,
views: Seq[View] = Seq(AgendaView("Agenda", "Agenda")),
onEventClicked: js.UndefOr[E => Callback] = js.undefined,
onEventDoubleClicked: js.UndefOr[E => Callback] = js.undefined,
onViewChanged: js.UndefOr[() => Callback] = js.undefined,
onDateRangeChanged: js.UndefOr[(js.Date, js.Date) => Callback] = js.undefined,
onEventSelected: js.UndefOr[E => Callback] = js.undefined
) =
component(Props(views, events))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment