@@ -51,14 +51,20 @@ public final class PledgeShippingLocationViewModel: PledgeShippingLocationViewMo
5151 let shippingShouldBeginLoading = project
5252 . mapConst ( true )
5353
54- let locations : Signal < [ Location ] , Never > = project. switchMap { p -> SignalProducer < [ Location ] , Never > in
55- shippingLocations ( forProject: p)
56- . demoteErrors ( replaceErrorWith: [ ] )
57- }
54+ let locations : Signal < [ Location ] ? , Never > = configData
55+ . ignoreValues ( )
56+ . switchMap { _ in
57+ shippableLocations ( )
58+ . wrapInOptional ( )
59+ . demoteErrors ( replaceErrorWith: nil )
60+ }
61+
62+ let loadedLocations = locations. skipNil ( )
63+ let erroredLocations = locations. filter { $0. isNil }
5864
5965 let shippingRulesLoadingCompleted = locations
66+ . demoteErrors ( replaceErrorWith: [ ] )
6067 . mapConst ( false )
61- . ksr_debounce ( . seconds( 1 ) , on: AppEnvironment . current. scheduler)
6268
6369 let isLoading = Signal . merge (
6470 shippingShouldBeginLoading,
@@ -70,21 +76,21 @@ public final class PledgeShippingLocationViewModel: PledgeShippingLocationViewMo
7076
7177 let initialShippingLocation = Signal . combineLatest (
7278 project,
73- locations ,
79+ loadedLocations ,
7480 selectedLocationId
7581 )
7682 . map ( determineShippingLocation)
7783
78- self . shippingRulesError = locations . map { $0 == [ ] ? " Shoot " : nil }
79- . skipNil ( )
84+ self . shippingRulesError = erroredLocations
85+ . mapConst ( Strings . We_were_unable_to_load_the_shipping_destinations ( ) )
8086
8187 self . notifyDelegateOfSelectedShippingLocation = Signal . merge (
8288 initialShippingLocation. skipNil ( ) ,
8389 self . shippingLocationUpdatedSignal
8490 )
8591
8692 self . presentShippingLocations = Signal . combineLatest (
87- locations ,
93+ loadedLocations ,
8894 self . notifyDelegateOfSelectedShippingLocation
8995 )
9096 . takeWhen ( self . shippingLocationButtonTappedSignal)
@@ -172,70 +178,12 @@ private func determineShippingLocation(
172178 return defaultShippingLocation ( fromLocations: locations)
173179}
174180
175- private func shippingLocations( forProject _: Project ) -> SignalProducer < [ Location ] , ErrorEnvelope > {
176- let query = GraphAPI . LocationsByTermQuery (
177- term: GraphQLNullable . some ( " America " ) ,
178- first: GraphQLNullable . some ( 25 )
179- )
181+ private func shippableLocations( ) -> SignalProducer < [ Location ] , ErrorEnvelope > {
182+ let query = GraphAPI . ShippableLocationsQuery ( )
180183 let signal = AppEnvironment . current. apiService. fetch ( query: query)
181184 . map { data in
182185 let locations = Location . locations ( from: data)
183186 return locations
184187 }
185188 return signal
186189}
187-
188- /*
189- Iterates through all reward shipping preferences to get all possible shipping rules.
190- */
191-
192- private func getShippingRulesForAllRewards( in project: Project ) -> SignalProducer < Signal <
193- [ ShippingRule ] ,
194- ErrorEnvelope
195- > . Event , Never > {
196- /// Get all of the reward IDs we'll need to fetch the Shipping Rules. See inner method logic for more details.
197- let rewardIDsToQuery : Set < Int > = getRewardIDsToQuery ( for: project)
198-
199- /// Initializing the result with a fetch using the first reward ID in the Set to avoid optional warnings
200- var queryResult : SignalProducer < Signal < [ ShippingRule ] , ErrorEnvelope > . Event , Never > ?
201-
202- /// Fetch the shipping rules for each reward and then consolidate each corresponding SignalProducer into the `queryResult` variable using .merge(with:).
203- rewardIDsToQuery. forEach { id in
204- let fetchResult = AppEnvironment . current. apiService. fetchRewardShippingRules (
205- projectId: project. id,
206- rewardId: id
207- )
208- . ksr_delay ( AppEnvironment . current. apiDelayInterval, on: AppEnvironment . current. scheduler)
209- . map ( ShippingRulesEnvelope . lens. shippingRules. view)
210- . retry ( upTo: 3 )
211- . materialize ( )
212-
213- queryResult = queryResult? . merge ( with: fetchResult) ?? fetchResult
214- }
215-
216- return queryResult ?? SignalProducer ( value: . value( [ ] ) )
217- }
218-
219- private func getRewardIDsToQuery( for project: Project ) -> Set < Int > {
220- /// Using a Set to avoid adding duplicate reward IDs. Some rewards may have the same shipping preferences.
221- var rewardIDsToQuery = Set < Int > ( )
222-
223- /// If project contains a reward with an `unrestricted` shipping preference, we can query just that reward. This will return ALL available locations.
224- if let reward = project. rewards
225- . first ( where: { $0. isUnRestrictedShippingPreference && $0. shipping. enabled } ) {
226- rewardIDsToQuery. insert ( reward. id)
227- }
228-
229- /// If project does not contain a reward with an `unrestricted` shipping preference, then we'll need to query all other rewards to capture all possible shipping locations.
230- if rewardIDsToQuery. isEmpty {
231- let restrictedRewards = project. rewards
232- . filter {
233- ( $0. isRestrictedShippingPreference || $0. hasNoShippingPreference)
234- && $0. shipping. enabled
235- }
236-
237- restrictedRewards. forEach { rewardIDsToQuery. insert ( $0. id) }
238- }
239-
240- return rewardIDsToQuery
241- }
0 commit comments