Current section
Files
Jump to
Current section
Files
src/illustrious@internal@state_priv.erl
-module(illustrious@internal@state_priv).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([wrap_decoder/1, wrap_updater/2]).
-spec wrap_decoder(
fun((gleam@dynamic:dynamic_()) -> {ok,
illustrious@state:illustrious_action(OMH)} |
{error, list(gleam@dynamic:decode_error())})
) -> fun((binary()) -> illustrious@state:illustrious_action(OMH)).
wrap_decoder(Decoder) ->
fun(Json) -> case gleam@json:decode(Json, gleam@dynamic:list(Decoder)) of
{ok, Actions} ->
{all, Actions};
_ ->
_pipe = gleam@json:decode(Json, Decoder),
gleam@result:unwrap(_pipe, noop)
end end.
-spec wrap_updater(
fun((OMK, OML) -> {OMK,
lustre@effect:effect(illustrious@state:illustrious_action(OML))}),
fun((gleam@dynamic:dynamic_()) -> {ok,
illustrious@state:illustrious_action(OML)} |
{error, list(gleam@dynamic:decode_error())})
) -> fun((illustrious@state:illustrious_model(OMK), illustrious@state:illustrious_action(OML)) -> {illustrious@state:illustrious_model(OMK),
lustre@effect:effect(illustrious@state:illustrious_action(OML))}).
wrap_updater(Update, Decoder) ->
Wrapped_decoder = wrap_decoder(Decoder),
Loader = fun(Split_path) ->
Path = gleam@list:fold(
Split_path,
<<""/utf8>>,
fun(Full_path, Part) ->
<<<<Full_path/binary, "/"/utf8>>/binary, Part/binary>>
end
),
lustre_http:post(
illustrious@internal@api_helpers:get_illustrious_api(),
gleam@json:object([{<<"path"/utf8>>, gleam@json:string(Path)}]),
lustre_http:expect_text(fun(Result) -> case Result of
{ok, Json} ->
Wrapped_decoder(Json);
_ ->
noop
end end)
)
end,
fun(Illustrious_model, Action) -> case Action of
{perform, Sub_action} ->
Orig_update = Update(
erlang:element(3, Illustrious_model),
Sub_action
),
{erlang:setelement(
3,
Illustrious_model,
erlang:element(1, Orig_update)
),
erlang:element(2, Orig_update)};
{go_to, Path@1} ->
Split_path@1 = illustrious@internal@router:parse_path(Path@1),
case erlang:element(2, Illustrious_model) =:= Split_path@1 of
true ->
{Illustrious_model, lustre@effect:none()};
false ->
illustrious@internal@router:push_path(Path@1),
{erlang:setelement(2, Illustrious_model, Split_path@1),
Loader(Split_path@1)}
end;
{redirect, Path@2} ->
Split_path@2 = illustrious@internal@router:parse_path(Path@2),
case erlang:element(2, Illustrious_model) =:= Split_path@2 of
true ->
{Illustrious_model, lustre@effect:none()};
false ->
illustrious@internal@router:replace_path(Path@2),
{erlang:setelement(2, Illustrious_model, Split_path@2),
Loader(Split_path@2)}
end;
back ->
illustrious@internal@router:back(),
{Illustrious_model, lustre@effect:none()};
forward ->
illustrious@internal@router:forward(),
{Illustrious_model, lustre@effect:none()};
{set_path, Split_path@3} ->
case erlang:element(2, Illustrious_model) =:= Split_path@3 of
true ->
{Illustrious_model, lustre@effect:none()};
false ->
{erlang:setelement(2, Illustrious_model, Split_path@3),
Loader(Split_path@3)}
end;
noop ->
{Illustrious_model, lustre@effect:none()};
{all, Actions} ->
{Illustrious_model,
lustre@effect:from(
fun(Dispatch) -> gleam@list:each(Actions, Dispatch) end
)}
end end.