summaryrefslogtreecommitdiff
path: root/elm/UList/Opt.elm
blob: 5a8aaa1edefd67dd63fa2fadde1b821369453158 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
port module UList.Opt exposing (main)

import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import Json.Encode as JE
import Task
import Process
import Browser
import Date
import Dict exposing (Dict)
import Lib.Util exposing (..)
import Lib.Html exposing (..)
import Lib.Api as Api
import Lib.RDate as RDate
import Lib.DropDown as DD
import Gen.Types as T
import Gen.Api as GApi
import Gen.UListVNOpt as GVO
import Gen.UListVNNotes as GVN
import Gen.UListDel as GDE
import Gen.UListRStatus as GRS

main : Program GVO.Recv Model Msg
main = Browser.element
  { init = \f -> (init f, Date.today |> Task.perform Today)
  , subscriptions = \model -> Sub.batch (List.map (\r -> DD.sub r.dd) <| model.rels)
  , view = view
  , update = update
  }

port ulistVNDeleted : Bool -> Cmd msg
port ulistNotesChanged : String -> Cmd msg
port ulistRelChanged : (Int, Int) -> Cmd msg

type alias Rel =
  { id     : Int
  , status : Int -- Special value -1 means 'delete this release from my list'
  , state  : Api.State
  , dd     : DD.Config Msg
  }

newrel : Int -> Int -> Int -> Rel
newrel rid vid st =
  { id     = rid
  , status = st
  , state  = Api.Normal
  , dd     = DD.init ("ulist_reldd" ++ String.fromInt vid ++ "_" ++ String.fromInt rid) (RelOpen rid)
  }

type alias Model =
  { flags      : GVO.Recv
  , today      : Date.Date
  , del        : Bool
  , delState   : Api.State
  , notes      : String
  , notesRev   : Int
  , notesState : Api.State
  , rels       : List Rel
  , relNfo     : Dict Int GApi.ApiReleases
  , relOptions : Maybe (List (Int, String))
  , relState   : Api.State
  }

init : GVO.Recv -> Model
init f =
  { flags      = f
  , today      = Date.fromOrdinalDate 2100 1
  , del        = False
  , delState   = Api.Normal
  , notes      = f.notes
  , notesRev   = 0
  , notesState = Api.Normal
  , rels       = List.map2 (\st nfo -> newrel nfo.id f.vid st) f.relstatus f.rels
  , relNfo     = Dict.fromList <| List.map (\r -> (r.id, r)) f.rels
  , relOptions = Nothing
  , relState   = Api.Normal
  }

type Msg
  = Today Date.Date
  | Del Bool
  | Delete
  | Deleted GApi.Response
  | Notes String
  | NotesSave Int
  | NotesSaved Int GApi.Response
  | RelOpen Int Bool
  | RelSet Int Int Bool
  | RelSaved Int Int GApi.Response
  | RelLoad
  | RelLoaded GApi.Response
  | RelAdd Int


modrel : Int -> (Rel -> Rel) -> List Rel -> List Rel
modrel rid f = List.map (\r -> if r.id == rid then f r else r)


showrel : GApi.ApiReleases -> String
showrel r = "[" ++ (RDate.format (RDate.expand r.released)) ++ " " ++ (String.join "," r.lang) ++ "] " ++ r.title ++ " (r" ++ String.fromInt r.id ++ ")"


update : Msg -> Model -> (Model, Cmd Msg)
update msg model =
  case msg of
    Today d -> ({ model | today = d }, Cmd.none)

    Del b -> ({ model | del = b }, Cmd.none)
    Delete ->
      ( { model | delState = Api.Loading }
      , Api.post "/u/ulist/del.json" (GDE.encode { uid = model.flags.uid, vid = model.flags.vid }) Deleted)
    Deleted GApi.Success -> (model, ulistVNDeleted True)
    Deleted e -> ({ model | delState = Api.Error e }, Cmd.none)

    Notes s ->
      ( { model | notes = s, notesRev = model.notesRev + 1 }
      , Task.perform (\_ -> NotesSave (model.notesRev+1)) <| Process.sleep 1000)
    NotesSave rev ->
      if rev /= model.notesRev || model.notes == model.flags.notes
      then (model, Cmd.none)
      else ( { model | notesState = Api.Loading }
           , Api.post "/u/ulist/setnote.json" (GVN.encode { uid = model.flags.uid, vid = model.flags.vid, notes = model.notes }) (NotesSaved rev))
    NotesSaved rev GApi.Success ->
      let f = model.flags
          nf = { f | notes = model.notes }
       in if model.notesRev /= rev
          then (model, Cmd.none)
          else ({model | flags = nf, notesState = Api.Normal }, ulistNotesChanged model.notes)
    NotesSaved _ e -> ({ model | notesState = Api.Error e }, Cmd.none)

    RelOpen rid b -> ({ model | rels = modrel rid (\r -> { r | dd = DD.toggle r.dd b }) model.rels }, Cmd.none)
    RelSet rid st _ ->
      ( { model | rels = modrel rid (\r -> { r | dd = DD.toggle r.dd False, status = st, state = Api.Loading }) model.rels }
      , Api.post "/u/ulist/rstatus.json" (GRS.encode { uid = model.flags.uid, rid = rid, status = st }) (RelSaved rid st) )
    RelSaved rid st GApi.Success ->
      let nr = if st == -1 then List.filter (\r -> r.id /= rid) model.rels
                           else modrel rid (\r -> { r | state = Api.Normal }) model.rels
      in ( { model | rels = nr }
         , ulistRelChanged (List.length <| List.filter (\r -> r.status == 2) nr, List.length nr) )
    RelSaved rid _ e -> ({ model | rels = modrel rid (\r -> { r | state = Api.Error e }) model.rels }, Cmd.none)

    RelLoad ->
      ( { model | relState = Api.Loading }
      , Api.post "/r/get.json" (JE.object [("vid", JE.int model.flags.vid)]) RelLoaded )
    RelLoaded (GApi.Releases rels) ->
      ( { model
        | relState = Api.Normal
        , relNfo = Dict.union (Dict.fromList <| List.map (\r -> (r.id, r)) rels) model.relNfo
        , relOptions = Just <| List.map (\r -> (r.id, showrel r)) rels
        }, Cmd.none)
    RelLoaded e -> ({ model | relState = Api.Error e }, Cmd.none)
    RelAdd rid ->
      ( { model | rels = model.rels ++ (if rid == 0 then [] else [newrel rid model.flags.vid 2]) }
      , Task.perform (RelSet rid 2) <| Task.succeed True)


view : Model -> Html Msg
view model =
  let
    opt =
      [ tr []
        [ td [ colspan 5 ]
          [ textarea ([ placeholder "Notes", rows 2, cols 80, onInput Notes, onBlur (NotesSave model.notesRev) ] ++ GVN.valNotes) [ text model.notes ]
          , div [ ]
            [ div [ class "spinner", classList [("invisible", model.notesState /= Api.Loading)] ] []
            , br [] []
            , case model.notesState of
                Api.Error e -> b [ class "standout" ] [ text <| Api.showResponse e ]
                _ -> text ""
            , br [] []
            , a [ href "#", onClickD (Del True) ] [ text "Remove VN" ]
            ]
          ]
        ]
      , tfoot []
        [ tr []
          [ td [ colspan 5 ] <|
            -- TODO: This <select> solution is ugly as hell, a Lib.DropDown-based solution would be nicer.
            -- Or just throw all releases in the table and use the status field for add stuff.
            case (model.relOptions, model.relState) of
              (Just opts, _)   -> [ inputSelect "" 0 RelAdd [ style "width" "500px" ]
                                    <| (0, "-- add release --") :: List.filter (\(rid,_) -> not <| List.any (\r -> r.id == rid) model.rels) opts ]
              (_, Api.Normal)  -> [ a [ href "#", onClickD RelLoad ] [ text "Add release" ] ]
              (_, Api.Loading) -> [ span [ class "spinner" ] [], text "Loading releases..." ]
              (_, Api.Error e) -> [ b [ class "standout" ] [ text <| Api.showResponse e ], text ". ", a [ href "#", onClickD RelLoad ] [ text "Try again" ] ]
          ]
        ]
      ]

    rel r =
      case Dict.get r.id model.relNfo of
        Nothing -> text ""
        Just nfo -> relnfo r nfo

    relnfo r nfo =
      let name = "ulist_relstatus" ++ String.fromInt model.flags.vid ++ "_" ++ String.fromInt nfo.id ++ "_"
      in
        tr []
        [ td [ class "tco1" ]
          [ DD.view r.dd r.state (text <| Maybe.withDefault "removing" <| lookup r.status T.rlistStatus)
            <| \_ ->
              [ ul [] <| List.map (\(n, status) ->
                  li [ class "linkradio" ]
                  [ inputCheck (name ++ String.fromInt n) (n == r.status) (RelSet r.id n)
                  , label [ for <| name ++ String.fromInt n ] [ text status ]
                  ]
                ) T.rlistStatus
                ++ [ li [] [ a [ href "#", onClickD (RelSet r.id -1 True) ] [ text "remove" ] ] ]
              ]
          ]
        , td [ class "tco2" ] [ RDate.display model.today nfo.released ]
        , td [ class "tco3" ] <| List.map langIcon nfo.lang ++ [ releaseTypeIcon nfo.rtype ]
        , td [ class "tco4" ] [ a [ href ("/r"++String.fromInt nfo.id), title nfo.original ] [ text nfo.title ] ]
        ]

    confirm =
      div []
      [ text "Are you sure you want to remove this visual novel from your list? "
      , a [ onClickD Delete ] [ text "Yes" ]
      , text " | "
      , a [ onClickD (Del False) ] [ text "Cancel" ]
      ]

  in case (model.del, model.delState) of
      (False, _) -> table [] <| (if model.flags.own then opt else []) ++ List.map rel model.rels
      (_, Api.Normal)  -> confirm
      (_, Api.Loading) -> div [ class "spinner" ] []
      (_, Api.Error e) -> b [ class "standout" ] [ text <| "Error removing item: " ++ Api.showResponse e ]