Skip to content

Commit

Permalink
Merge pull request #490 from mozilla/qa-stage
Browse files Browse the repository at this point in the history
April Hubs Cloud Release [Themes Update]
  • Loading branch information
brianpeiris authored Apr 8, 2021
2 parents 3efc570 + a4ccb49 commit 8119ec2
Showing 1 changed file with 44 additions and 1 deletion.
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

0 comments on commit 8119ec2

Please sign in to comment.