From f5ede9e020c47c49567ada2cfa52ca1b926b4c12 Mon Sep 17 00:00:00 2001 From: Yorhel Date: Sun, 10 Nov 2019 14:30:20 +0100 Subject: ulist: Add feature to add releases to the list --- elm/Lib/Api.elm | 1 + elm/UList/Opt.elm | 76 +++++++++++++++++++++++++++++++++++++++--------- lib/VNWeb/Elm.pm | 8 +++++ lib/VNWeb/Releases/JS.pm | 21 +++++++++++++ lib/VNWeb/User/Lists.pm | 10 ++++--- 5 files changed, 98 insertions(+), 18 deletions(-) create mode 100644 lib/VNWeb/Releases/JS.pm diff --git a/elm/Lib/Api.elm b/elm/Lib/Api.elm index 283cbe3c..1e028904 100644 --- a/elm/Lib/Api.elm +++ b/elm/Lib/Api.elm @@ -42,6 +42,7 @@ showResponse res = DoubleIP -> "You can only register one account from the same IP within 24 hours." BadCurPass -> "Current password is invalid." MailChange -> unexp + Releases _ -> unexp expectResponse : (Response -> msg) -> Http.Expect msg diff --git a/elm/UList/Opt.elm b/elm/UList/Opt.elm index 46ac6acd..5a8aaa1e 100644 --- a/elm/UList/Opt.elm +++ b/elm/UList/Opt.elm @@ -3,10 +3,12 @@ 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 @@ -32,12 +34,20 @@ port ulistNotesChanged : String -> Cmd msg port ulistRelChanged : (Int, Int) -> Cmd msg type alias Rel = - { nfo : GVO.RecvRels + { 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 @@ -47,6 +57,9 @@ type alias Model = , 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 @@ -58,10 +71,10 @@ init f = , notes = f.notes , notesRev = 0 , notesState = Api.Normal - , rels = List.map (\r -> - { nfo = r, status = r.status, state = Api.Normal - , dd = DD.init ("ulist_reldd" ++ String.fromInt f.vid ++ "_" ++ String.fromInt r.id) (RelOpen r.id) - } ) f.rels + , 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 @@ -75,10 +88,17 @@ type Msg | 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.nfo.id == rid then f r else r) +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) @@ -114,12 +134,26 @@ update msg model = ( { 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.nfo.id /= rid) model.rels + 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 = @@ -141,12 +175,26 @@ view model = ] , tfoot [] [ tr [] - [ td [ colspan 5 ] [ a [ href "#" ] [ text "Add release" ] ] ] + [ td [ colspan 5 ] <| + -- TODO: This