diff --git a/.env.example b/.env.example index 911f4c3..82bea08 100644 --- a/.env.example +++ b/.env.example @@ -4,4 +4,6 @@ CANONICAL_URL=http://localhost:3030/ PLACECAL_API=https://placecal.org/api/v1/graphql +# comma seperated list of id|name for partnerships to include +PARTNERSHIP_TAG_LIST=3|London,30|Manchester JOIN_US_FUNCTION_URL=https://formspree.io/f/xeqdljwl diff --git a/app/Route/Events/Event_.elm b/app/Route/Events/Event_.elm index d747c9e..69b5c35 100644 --- a/app/Route/Events/Event_.elm +++ b/app/Route/Events/Event_.elm @@ -9,7 +9,6 @@ module Route.Events.Event_ exposing (Model, Msg, RouteParams, route, Data, Actio import BackendTask import Copy.Keys exposing (Key(..)) import Copy.Text exposing (t) -import Data.PlaceCal.Api import Data.PlaceCal.Events import FatalError import Head @@ -61,11 +60,7 @@ pages = eventData.allEvents |> List.map (\event -> { event = event.id }) ) - (Data.PlaceCal.Api.fetchAndCachePlaceCalData - "events" - Data.PlaceCal.Events.allEventsQuery - Data.PlaceCal.Events.eventsDecoder - ) + Data.PlaceCal.Events.eventsData |> BackendTask.allowFatal diff --git a/app/Route/Partners/Partner_.elm b/app/Route/Partners/Partner_.elm index 75f2988..09e2bfa 100644 --- a/app/Route/Partners/Partner_.elm +++ b/app/Route/Partners/Partner_.elm @@ -10,7 +10,6 @@ import BackendTask import Browser.Dom import Copy.Keys exposing (Key(..)) import Copy.Text exposing (t) -import Data.PlaceCal.Api import Data.PlaceCal.Events import Data.PlaceCal.Partners import Effect @@ -240,11 +239,7 @@ pages = partnerData.allPartners |> List.map (\partner -> { partner = partner.id }) ) - (Data.PlaceCal.Api.fetchAndCachePlaceCalData - "partners" - Data.PlaceCal.Partners.allPartnersQuery - Data.PlaceCal.Partners.partnersDecoder - ) + Data.PlaceCal.Partners.partnersData |> BackendTask.allowFatal diff --git a/elm-constants.json b/elm-constants.json index 683fb9a..355d190 100644 --- a/elm-constants.json +++ b/elm-constants.json @@ -4,6 +4,7 @@ "values": [ "CANONICAL_URL", "PLACECAL_API", - "JOIN_US_FUNCTION_URL" + "JOIN_US_FUNCTION_URL", + "PARTNERSHIP_TAG_LIST" ] } diff --git a/src/Data/PlaceCal/Events.elm b/src/Data/PlaceCal/Events.elm index 3119c7b..51f8a8b 100644 --- a/src/Data/PlaceCal/Events.elm +++ b/src/Data/PlaceCal/Events.elm @@ -1,4 +1,4 @@ -module Data.PlaceCal.Events exposing (Event, EventPartner, afterDate, allEventsQuery, eventFromSlug, eventPartnerFromId, eventsData, eventsDecoder, eventsFromDate, eventsWithPartners, next4Events, onOrBeforeDate) +module Data.PlaceCal.Events exposing (Event, EventPartner, afterDate, eventFromSlug, eventPartnerFromId, eventsData, eventsFromDate, eventsWithPartners, next4Events, onOrBeforeDate) import BackendTask import BackendTask.Custom @@ -14,6 +14,7 @@ import Time type alias Event = { id : String + , partnershipTagId : Int , name : String , summary : String , description : String @@ -58,6 +59,7 @@ type alias Geo = emptyEvent : Event emptyEvent = { id = "" + , partnershipTagId = 0 , name = "" , summary = "" , description = "" @@ -149,18 +151,31 @@ eventsWithPartners eventList partnerList = eventsData : BackendTask.BackendTask { fatal : FatalError.FatalError, recoverable : BackendTask.Custom.Error } AllEventsResponse eventsData = - Data.PlaceCal.Api.fetchAndCachePlaceCalData "events" - allEventsQuery - eventsDecoder + BackendTask.combine + (List.map + (\partnershipTagInt -> + Data.PlaceCal.Api.fetchAndCachePlaceCalData + ("events-" ++ String.fromInt partnershipTagInt) + (allEventsQuery (String.fromInt partnershipTagInt)) + (eventsDecoder partnershipTagInt) + ) + Data.PlaceCal.Partners.partnershipTagIdList + ) + |> BackendTask.map (List.map .allEvents) + |> BackendTask.map List.concat + |> BackendTask.map (\eventList -> { allEvents = eventList }) -allEventsQuery : Json.Encode.Value -allEventsQuery = +allEventsQuery : String -> Json.Encode.Value +allEventsQuery partnershipTag = Json.Encode.object [ ( "query" -- Note hardcoded to load events from 2022-09-01 - , Json.Encode.string """ - query { eventsByFilter(tagId: 3, fromDate: "2024-01-01 00:00", toDate: "2025-06-15 00:00") { + , Json.Encode.string + ("query { eventsByFilter(tagId: " + ++ partnershipTag + ++ """ + , fromDate: "2024-01-01 00:00", toDate: "2025-06-15 00:00") { id name summary @@ -172,21 +187,23 @@ allEventsQuery = organizer { id } } } """ + ) ) ] -eventsDecoder : Json.Decode.Decoder AllEventsResponse -eventsDecoder = +eventsDecoder : Int -> Json.Decode.Decoder AllEventsResponse +eventsDecoder partnershipTagInt = Json.Decode.succeed AllEventsResponse - |> Json.Decode.Pipeline.requiredAt [ "data", "eventsByFilter" ] (Json.Decode.list decode) + |> Json.Decode.Pipeline.requiredAt [ "data", "eventsByFilter" ] (Json.Decode.list (decodeEvent partnershipTagInt)) -decode : Json.Decode.Decoder Event -decode = +decodeEvent : Int -> Json.Decode.Decoder Event +decodeEvent partnershipTagInt = Json.Decode.succeed Event |> Json.Decode.Pipeline.required "id" Json.Decode.string + |> Json.Decode.Pipeline.optional "partnershipTagId" (Json.Decode.succeed partnershipTagInt) 0 |> Json.Decode.Pipeline.required "name" Json.Decode.string |> Json.Decode.Pipeline.optional "summary" diff --git a/src/Data/PlaceCal/Partners.elm b/src/Data/PlaceCal/Partners.elm index e0e58f2..a21370e 100644 --- a/src/Data/PlaceCal/Partners.elm +++ b/src/Data/PlaceCal/Partners.elm @@ -1,7 +1,8 @@ -module Data.PlaceCal.Partners exposing (Address, Contact, Partner, ServiceArea, allPartnersQuery, partnerFromSlug, partnerNamesFromIds, partnersData, partnersDecoder) +module Data.PlaceCal.Partners exposing (Address, Contact, Partner, ServiceArea, partnerFromSlug, partnerNamesFromIds, partnersData, partnershipTagIdList) import BackendTask import BackendTask.Custom +import Constants import Data.PlaceCal.Api import FatalError import Json.Decode @@ -11,6 +12,7 @@ import Json.Encode type alias Partner = { id : String + , partnershipTagId : Int , name : String , summary : String , description : String @@ -54,6 +56,7 @@ type alias ServiceArea = emptyPartner : Partner emptyPartner = { id = "" + , partnershipTagId = 0 , name = "" , summary = "" , description = "" @@ -76,19 +79,70 @@ type alias AllPartnersResponse = { allPartners : List Partner } -partnersData : BackendTask.BackendTask { fatal : FatalError.FatalError, recoverable : BackendTask.Custom.Error } AllPartnersResponse -partnersData = - Data.PlaceCal.Api.fetchAndCachePlaceCalData "partners" - allPartnersQuery - partnersDecoder +type alias PartnershipTag = + { id : Int + , name : String + } + + +partnershipTagList : List PartnershipTag +partnershipTagList = + String.split "," Constants.partnershipTagList + |> List.map + (\tagInfo -> + { id = partnershipTagId tagInfo + , name = partnershipTagName tagInfo + } + ) + +partnershipTagIdList : List Int +partnershipTagIdList = + partnershipTagList + |> List.map .id -allPartnersQuery : Json.Encode.Value -allPartnersQuery = + +partnershipTagId : String -> Int +partnershipTagId tagInfo = + List.head (String.split "|" tagInfo) + |> Maybe.withDefault "" + |> String.toInt + |> Maybe.withDefault 0 + + +partnershipTagName : String -> String +partnershipTagName tagInfo = + List.head + (List.reverse (String.split "|" tagInfo)) + |> Maybe.withDefault "" + + +partnersData : BackendTask.BackendTask { fatal : FatalError.FatalError, recoverable : BackendTask.Custom.Error } AllPartnersResponse +partnersData = + BackendTask.combine + (List.map + (\partnershipTagInt -> + Data.PlaceCal.Api.fetchAndCachePlaceCalData + ("partners-" ++ String.fromInt partnershipTagInt) + (allPartnersQuery (String.fromInt partnershipTagInt)) + (partnersDecoder partnershipTagInt) + ) + partnershipTagIdList + ) + |> BackendTask.map (List.map .allPartners) + |> BackendTask.map List.concat + |> BackendTask.map (\partnerList -> { allPartners = partnerList }) + + +allPartnersQuery : String -> Json.Encode.Value +allPartnersQuery partnershipTag = Json.Encode.object [ ( "query" - , Json.Encode.string """ - query { partnersByTag(tagId: 3) { + , Json.Encode.string + ("query { partnersByTag(tagId: " + ++ partnershipTag + ++ """ + ) { id name description @@ -100,20 +154,22 @@ allPartnersQuery = logo } } """ + ) ) ] -partnersDecoder : Json.Decode.Decoder AllPartnersResponse -partnersDecoder = +partnersDecoder : Int -> Json.Decode.Decoder AllPartnersResponse +partnersDecoder partnershipTagInt = Json.Decode.succeed AllPartnersResponse - |> Json.Decode.Pipeline.requiredAt [ "data", "partnersByTag" ] (Json.Decode.list decodePartner) + |> Json.Decode.Pipeline.requiredAt [ "data", "partnersByTag" ] (Json.Decode.list (decodePartner partnershipTagInt)) -decodePartner : Json.Decode.Decoder Partner -decodePartner = +decodePartner : Int -> Json.Decode.Decoder Partner +decodePartner partnershipTagInt = Json.Decode.succeed Partner |> Json.Decode.Pipeline.required "id" Json.Decode.string + |> Json.Decode.Pipeline.optional "partnershipTagId" (Json.Decode.succeed partnershipTagInt) 0 |> Json.Decode.Pipeline.required "name" Json.Decode.string |> Json.Decode.Pipeline.optional "summary" Json.Decode.string "" |> Json.Decode.Pipeline.optional "description" Json.Decode.string ""