Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

April Hubs Cloud Release [Themes Update] #490

Merged
merged 8 commits into from
Apr 8, 2021
45 changes: 44 additions & 1 deletion lib/ret_web/controllers/page_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,51 @@ defmodule RetWeb.PageController do
Ret.AppConfig.get_config(!!module_config(:skip_cache)) |> generate_config("APP_CONFIG")
end

# We receive the themes array as stringified JSON. We must decode it here and
# then re-encode it so that it will be successfully parsed by the client.
#
# The data going into this function looks something like this:
# %{
# "theme" => %{
# "themes" => "[
# { \"id\": \"theme-1\", ...},
# { \"id\": \"theme-2\", ...},
# ...
# ]",
# "deprecated_color_property_1" => "#ffffff",
# "deprecated_color_property_n" => "#000000"
# }
# }
# The data returned from this function looks something like this:
# %{
# "theme" => %{
# "themes" => [
# %{ "id" => "theme-1", ...},
# %{ "id" => "theme-2", ...},
# ...
# ],
# "deprecated_color_property_1" => "#ffffff",
# "deprecated_color_property_n" => "#000000"
# }
# }
defp escape_themes(%{"theme" => %{"themes" => string} = category} = config) do
case Poison.decode(string || "") do
{:ok, array} ->
Map.put(config, "theme", Map.put(category, "themes", array))

_ ->
category = Map.put(category, "themes", [])
category = Map.put(category, "error", "Failed to parse custom theme JSON.")
Map.put(config, "theme", category)
end
end

defp escape_themes(config) do
config
end

defp generate_config(config, name) do
config_json = config |> Poison.encode!()
config_json = config |> escape_themes() |> Poison.encode!()
config_script = "window.#{name} = JSON.parse('#{config_json |> String.replace("'", "\\'")}')"
{config, config_script}
end
Expand Down