|
| 1 | +port module Wizard.Common.Driver exposing |
| 2 | + ( DriverOptionsStep |
| 3 | + , TourConfig |
| 4 | + , TourId |
| 5 | + , addModalDelay |
| 6 | + , addStep |
| 7 | + , init |
| 8 | + , onTourDone |
| 9 | + , tourConfig |
| 10 | + , tourId |
| 11 | + ) |
| 12 | + |
| 13 | +import Gettext exposing (gettext) |
| 14 | +import Json.Encode as E |
| 15 | +import Json.Encode.Extra as E |
| 16 | +import Shared.Auth.Session as Session |
| 17 | +import String.Format as String |
| 18 | +import Wizard.Common.AppState exposing (AppState) |
| 19 | + |
| 20 | + |
| 21 | +type TourId |
| 22 | + = TourId String |
| 23 | + |
| 24 | + |
| 25 | +tourId : String -> TourId |
| 26 | +tourId = |
| 27 | + TourId |
| 28 | + |
| 29 | + |
| 30 | +type TourConfig |
| 31 | + = TourConfig TourConfigData |
| 32 | + |
| 33 | + |
| 34 | +type alias TourConfigData = |
| 35 | + { tourId : String |
| 36 | + , loggedIn : Bool |
| 37 | + , completedTourIds : List String |
| 38 | + , locale : Gettext.Locale |
| 39 | + , steps : List DriverOptionsStep |
| 40 | + , delay : Int |
| 41 | + } |
| 42 | + |
| 43 | + |
| 44 | +tourConfig : TourId -> AppState -> TourConfig |
| 45 | +tourConfig (TourId id) appState = |
| 46 | + TourConfig |
| 47 | + { tourId = id |
| 48 | + , loggedIn = Session.exists appState.session |
| 49 | + , completedTourIds = appState.config.tours |
| 50 | + , locale = appState.locale |
| 51 | + , steps = [] |
| 52 | + , delay = 0 |
| 53 | + } |
| 54 | + |
| 55 | + |
| 56 | +addStep : DriverOptionsStep -> TourConfig -> TourConfig |
| 57 | +addStep step (TourConfig config) = |
| 58 | + TourConfig { config | steps = config.steps ++ [ step ] } |
| 59 | + |
| 60 | + |
| 61 | +addDelay : Int -> TourConfig -> TourConfig |
| 62 | +addDelay delay (TourConfig config) = |
| 63 | + TourConfig { config | delay = delay } |
| 64 | + |
| 65 | + |
| 66 | +addModalDelay : TourConfig -> TourConfig |
| 67 | +addModalDelay = |
| 68 | + addDelay 200 |
| 69 | + |
| 70 | + |
| 71 | +type alias DriverOptionsStep = |
| 72 | + { element : Maybe String |
| 73 | + , popover : |
| 74 | + { title : String |
| 75 | + , description : String |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | +skipTourStr : Gettext.Locale -> String |
| 81 | +skipTourStr = |
| 82 | + gettext "Are you sure you want to skip the tour?" |
| 83 | + |
| 84 | + |
| 85 | +skipTourHint : Gettext.Locale -> String |
| 86 | +skipTourHint = |
| 87 | + gettext "You can reset tours in user settings" |
| 88 | + |
| 89 | + |
| 90 | +encodeTour : TourConfigData -> E.Value |
| 91 | +encodeTour config = |
| 92 | + E.object |
| 93 | + [ ( "tourId", E.string config.tourId ) |
| 94 | + , ( "steps", E.list encodeStep config.steps ) |
| 95 | + , ( "skipTourStr", E.string (String.format "%s\n(%s)" [ skipTourStr config.locale, skipTourHint config.locale ]) ) |
| 96 | + , ( "delay", E.int config.delay ) |
| 97 | + ] |
| 98 | + |
| 99 | + |
| 100 | +encodeStep : DriverOptionsStep -> E.Value |
| 101 | +encodeStep step = |
| 102 | + E.object |
| 103 | + [ ( "element", E.maybe E.string step.element ) |
| 104 | + , ( "popover" |
| 105 | + , E.object |
| 106 | + [ ( "title", E.string step.popover.title ) |
| 107 | + , ( "description", E.string step.popover.description ) |
| 108 | + ] |
| 109 | + ) |
| 110 | + ] |
| 111 | + |
| 112 | + |
| 113 | +init : TourConfig -> Cmd msg |
| 114 | +init (TourConfig config) = |
| 115 | + if not config.loggedIn || List.member config.tourId config.completedTourIds then |
| 116 | + Cmd.none |
| 117 | + |
| 118 | + else |
| 119 | + drive (encodeTour config) |
| 120 | + |
| 121 | + |
| 122 | +port drive : E.Value -> Cmd msg |
| 123 | + |
| 124 | + |
| 125 | +port onTourDone : (String -> msg) -> Sub msg |
0 commit comments