diff --git a/frontend/client/src/bowtie_client.gleam b/frontend/client/src/bowtie_client.gleam index 7181b29..7478b44 100644 --- a/frontend/client/src/bowtie_client.gleam +++ b/frontend/client/src/bowtie_client.gleam @@ -42,13 +42,16 @@ import rsvp import witness // Main ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -pub fn main(state: String) -> Result(lustre.Runtime(Message), lustre.Error) { +pub fn main() -> Result(lustre.Runtime(Message), lustre.Error) { case lustre.is_browser() { True -> { + let state: String = get_state() // The SPA is ran as SPA, this means we start up the application and get ready for running! witness.default_config() |> witness.set_config() - witness.this(witness.Info, "Hello from bowtie_client!", []) + witness.this(witness.Info, "Hello from bowtie_client!", [ + witness.string("Startup state", state), + ]) let app = lustre.application(init, update, view) let assert Ok(_) = lustre.start(app, "body", state) } @@ -163,6 +166,11 @@ pub fn init(state: String) -> #(Model, Effect(Message)) { #(model, effect) } +@external(javascript, "./bowtie_client_ffi.mjs", "GetState") +fn get_state() -> String { + panic as "Tried getting the state from DOM on an Erlang target." +} + pub type Prerender { Prerendered(bodies: Dict(String, String)) RepoMissing diff --git a/frontend/client/src/bowtie_client_ffi.mjs b/frontend/client/src/bowtie_client_ffi.mjs new file mode 100644 index 0000000..4c1b457 --- /dev/null +++ b/frontend/client/src/bowtie_client_ffi.mjs @@ -0,0 +1,4 @@ +export function GetState() { + const bowtieState = document.getElementById("bowtie-state"); + return bowtieState ? bowtieState.textContent : ""; +} diff --git a/justfile b/justfile index cc9d0bc..3f698eb 100644 --- a/justfile +++ b/justfile @@ -35,7 +35,7 @@ prepare-build: vendor echo '}' >> ./frontend/client/build/dev/javascript/bowtie_client/oat.mjs # Patch SPA, Oat and Lustre Server Component runtime into a single web js bundle. - echo 'import { main } from "./bowtie_client.mjs"; import { ServerComponent } from "../lustre/priv/static/lustre-server-component.mjs"; import { oat } from "./oat.mjs"; document.addEventListener("DOMContentLoaded", () => { const bowtieState = document.getElementById("bowtie-state"); main(bowtieState ? bowtieState.textContent : ""); }); oat()' > "./frontend/client/build/dev/javascript/bowtie_client/client.mjs" + echo 'import { main } from "./bowtie_client.mjs"; import { ServerComponent } from "../lustre/priv/static/lustre-server-component.mjs"; import { oat } from "./oat.mjs"; document.addEventListener("DOMContentLoaded", () => main()); oat()' > "./frontend/client/build/dev/javascript/bowtie_client/client.mjs" deno bundle -o ./backend/priv/client.js --platform=browser ./frontend/client/build/dev/javascript/bowtie_client/client.mjs deno bundle -o ./backend/priv/client.min.js --platform=browser --minify ./frontend/client/build/dev/javascript/bowtie_client/client.mjs