1module Common exposing (ServiceWorkerStatus(..), Switch(..), backToIndex, boolFromString, boolToString, queryString, translateHttpResponse, urlOrigin)
2
3import Http
4import Tuple.Ext as Tuple
5import Url exposing (Protocol(..), Url)
6import Url.Builder as Url
7
8
9
10-- ⛩
11
12
13backToIndex : String
14backToIndex =
15 "Back to tracks"
16
17
18
19-- 🌳
20
21
22type ServiceWorkerStatus
23 = InstallingInitial
24 | InstallingNew
25 | WaitingForActivation
26 | Activated
27
28
29type Switch
30 = On
31 | Off
32
33
34
35-- 🔱
36
37
38boolFromString : String -> Bool
39boolFromString string =
40 case string of
41 "t" ->
42 True
43
44 _ ->
45 False
46
47
48boolToString : Bool -> String
49boolToString bool =
50 if bool then
51 "t"
52
53 else
54 "f"
55
56
57queryString : List ( String, String ) -> String
58queryString =
59 List.map (Tuple.uncurry Url.string) >> Url.toQuery
60
61
62translateHttpResponse : Http.Response String -> Result Http.Error String
63translateHttpResponse response =
64 case response of
65 Http.BadUrl_ u ->
66 Err (Http.BadUrl u)
67
68 Http.Timeout_ ->
69 Err Http.Timeout
70
71 Http.NetworkError_ ->
72 Err Http.NetworkError
73
74 Http.BadStatus_ _ body ->
75 Err (Http.BadBody body)
76
77 Http.GoodStatus_ _ body ->
78 Ok body
79
80
81urlOrigin : Url -> String
82urlOrigin { host, port_, path, protocol } =
83 let
84 scheme =
85 case protocol of
86 Http ->
87 "http://"
88
89 Https ->
90 "https://"
91
92 thePort =
93 port_
94 |> Maybe.map (String.fromInt >> (++) ":")
95 |> Maybe.withDefault ""
96 in
97 scheme ++ host ++ thePort ++ path