Skip to content

Commit

Permalink
Merge pull request #455 from geeksforsocialchange/feat/408-add-region…
Browse files Browse the repository at this point in the history
…al-selector

Feat/408 add regional selector
  • Loading branch information
katjam authored Nov 2, 2024
2 parents 474a8cd + d9e1428 commit 1cdd017
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 1 addition & 6 deletions app/Route/Events/Event_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down
7 changes: 1 addition & 6 deletions app/Route/Partners/Partner_.elm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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


Expand Down
3 changes: 2 additions & 1 deletion elm-constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"values": [
"CANONICAL_URL",
"PLACECAL_API",
"JOIN_US_FUNCTION_URL"
"JOIN_US_FUNCTION_URL",
"PARTNERSHIP_TAG_LIST"
]
}
43 changes: 30 additions & 13 deletions src/Data/PlaceCal/Events.elm
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -14,6 +14,7 @@ import Time

type alias Event =
{ id : String
, partnershipTagId : Int
, name : String
, summary : String
, description : String
Expand Down Expand Up @@ -58,6 +59,7 @@ type alias Geo =
emptyEvent : Event
emptyEvent =
{ id = ""
, partnershipTagId = 0
, name = ""
, summary = ""
, description = ""
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand Down
86 changes: 71 additions & 15 deletions src/Data/PlaceCal/Partners.elm
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,6 +12,7 @@ import Json.Encode

type alias Partner =
{ id : String
, partnershipTagId : Int
, name : String
, summary : String
, description : String
Expand Down Expand Up @@ -54,6 +56,7 @@ type alias ServiceArea =
emptyPartner : Partner
emptyPartner =
{ id = ""
, partnershipTagId = 0
, name = ""
, summary = ""
, description = ""
Expand All @@ -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
Expand All @@ -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 ""
Expand Down

0 comments on commit 1cdd017

Please sign in to comment.