|
@@ -14,7 +14,7 @@ import scala.util.{Failure,Success}
|
|
|
|
|
|
|
|
// TODO: prevent user from not having any of discreet/mass/volume input
|
|
// TODO: prevent user from not having any of discreet/mass/volume input
|
|
|
// TODO: Save recipe node in cookie until ready to use
|
|
// TODO: Save recipe node in cookie until ready to use
|
|
|
-object RecipeEdit extends View[Option[String]] {
|
|
|
|
|
|
|
+object RecipeEdit extends View[(Option[String], Option[Boolean])] {
|
|
|
import com.weEat.Main.headers
|
|
import com.weEat.Main.headers
|
|
|
|
|
|
|
|
implicit val ec = com.weEat.shared.ctx
|
|
implicit val ec = com.weEat.shared.ctx
|
|
@@ -22,15 +22,21 @@ object RecipeEdit extends View[Option[String]] {
|
|
|
val navName = Some("New Recipe")
|
|
val navName = Some("New Recipe")
|
|
|
val tag = "editRecipe"
|
|
val tag = "editRecipe"
|
|
|
|
|
|
|
|
- case class ViewPage(val id: Option[String] = None) extends P {
|
|
|
|
|
|
|
+ case class ViewPage(id: Option[String] = None, fork: Boolean = false) extends P {
|
|
|
val title = "Edit Recipe"
|
|
val title = "Edit Recipe"
|
|
|
def jsonValue = Json.toJson(id)
|
|
def jsonValue = Json.toJson(id)
|
|
|
}
|
|
}
|
|
|
def parseJson(jsVal: JsValue) = ViewPage(jsVal.asOpt[String])
|
|
def parseJson(jsVal: JsValue) = ViewPage(jsVal.asOpt[String])
|
|
|
def route = Route.onlyQuery(
|
|
def route = Route.onlyQuery(
|
|
|
- encode = (page: ViewPage) => page.id,
|
|
|
|
|
- decode = (id: Option[String]) => ViewPage(id = id),
|
|
|
|
|
- pattern = (root / tag / endOfSegments) ? param[String]("id").?
|
|
|
|
|
|
|
+ encode = (page: ViewPage) => (page.id, Option.when(page.fork)(true)),
|
|
|
|
|
+ decode = {
|
|
|
|
|
+ case (id: Option[String], fork: Option[Boolean]) => ViewPage(
|
|
|
|
|
+ id,
|
|
|
|
|
+ fork.getOrElse(false)
|
|
|
|
|
+ )
|
|
|
|
|
+ },
|
|
|
|
|
+ pattern = (root / tag / endOfSegments) ?
|
|
|
|
|
+ param[String]("id").? & param[Boolean]("fork").?
|
|
|
)
|
|
)
|
|
|
def defaultPage = ViewPage()
|
|
def defaultPage = ViewPage()
|
|
|
|
|
|
|
@@ -44,6 +50,7 @@ object RecipeEdit extends View[Option[String]] {
|
|
|
FoodController.get(id)().map(_.asInstanceOf[RecipeNode])
|
|
FoodController.get(id)().map(_.asInstanceOf[RecipeNode])
|
|
|
))
|
|
))
|
|
|
).shiftOption.map(_.flatten)
|
|
).shiftOption.map(_.flatten)
|
|
|
|
|
+ val fork: Signal[Boolean] = page.map(_.fork)
|
|
|
val recipeVar = refRecipe.map(RecipeVar(_))
|
|
val recipeVar = refRecipe.map(RecipeVar(_))
|
|
|
val recipe = recipeVar.flatMap(_.newInstance)
|
|
val recipe = recipeVar.flatMap(_.newInstance)
|
|
|
|
|
|
|
@@ -60,24 +67,46 @@ object RecipeEdit extends View[Option[String]] {
|
|
|
)
|
|
)
|
|
|
)
|
|
)
|
|
|
),
|
|
),
|
|
|
- child <-- refRecipe.map({
|
|
|
|
|
- case Some(RecipeNodeId(id, uid, _, _, _, _, _, _, _, _, _, _, _)) =>
|
|
|
|
|
- button(cls := "btn",
|
|
|
|
|
- onMountBind { (ctx) =>
|
|
|
|
|
- onClick --> { (e: Event) =>
|
|
|
|
|
- implicit val owner = ctx.owner
|
|
|
|
|
- FoodController.update(id, Some(uid))(recipe.observe.now().get)
|
|
|
|
|
- .onComplete {
|
|
|
|
|
- case Success(recipe) =>
|
|
|
|
|
- View.router.pushState(RecipeView.ViewPage(recipe._id))
|
|
|
|
|
- case Failure(ex) =>
|
|
|
|
|
- println("Could not update recipe")
|
|
|
|
|
- throw ex
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ child <-- refRecipe.combineWithFn(fork) {
|
|
|
|
|
+ case (
|
|
|
|
|
+ Some(RecipeNodeId(id, uid, _, _, _, _, _, _, _, _, _, _, _)),
|
|
|
|
|
+ false
|
|
|
|
|
+ ) => button(cls := "btn",
|
|
|
|
|
+ onMountBind { (ctx) =>
|
|
|
|
|
+ onClick --> { (e: Event) =>
|
|
|
|
|
+ implicit val owner = ctx.owner
|
|
|
|
|
+ FoodController.update(id, Some(uid))(recipe.observe.now().get)
|
|
|
|
|
+ .onComplete {
|
|
|
|
|
+ case Success(recipe) =>
|
|
|
|
|
+ View.router.pushState(RecipeView.ViewPage(recipe._id))
|
|
|
|
|
+ case Failure(ex) =>
|
|
|
|
|
+ println("Could not update recipe")
|
|
|
|
|
+ throw ex
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ "Update"
|
|
|
|
|
+ )
|
|
|
|
|
+ case (
|
|
|
|
|
+ Some(RecipeNodeId(_, _, vid, _, _, _, _, _, _, _, _, _, _)),
|
|
|
|
|
+ true
|
|
|
|
|
+ ) => button(cls := "btn",
|
|
|
|
|
+ onMountBind { (ctx) =>
|
|
|
|
|
+ onClick --> { (e: Event) =>
|
|
|
|
|
+ implicit val owner = ctx.owner
|
|
|
|
|
+ FoodController.add()(recipe.observe.now().get.copy(
|
|
|
|
|
+ prevVersionVid = Some(vid)
|
|
|
|
|
+ )).onComplete {
|
|
|
|
|
+ case Success(recipe) =>
|
|
|
|
|
+ View.router.pushState(RecipeView.ViewPage(recipe._id))
|
|
|
|
|
+ case Failure(ex) =>
|
|
|
|
|
+ println("Could not add recipe")
|
|
|
|
|
+ throw ex
|
|
|
}
|
|
}
|
|
|
- },
|
|
|
|
|
- "Update"
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ }
|
|
|
|
|
+ },
|
|
|
|
|
+ "Add"
|
|
|
|
|
+ )
|
|
|
case _ => button(cls := "btn",
|
|
case _ => button(cls := "btn",
|
|
|
onMountBind { (ctx) =>
|
|
onMountBind { (ctx) =>
|
|
|
onClick --> { (e: Event) =>
|
|
onClick --> { (e: Event) =>
|
|
@@ -93,7 +122,7 @@ object RecipeEdit extends View[Option[String]] {
|
|
|
},
|
|
},
|
|
|
"Add"
|
|
"Add"
|
|
|
)
|
|
)
|
|
|
- })
|
|
|
|
|
|
|
+ }
|
|
|
)
|
|
)
|
|
|
)
|
|
)
|
|
|
}
|
|
}
|