Skip to content

Commit

Permalink
Fix: add address to scaphold when it is created
Browse files Browse the repository at this point in the history
This was causing an issue with sending photos because we were not able to create an address-photo connection without a valid address ID
  • Loading branch information
alexbrashear committed Dec 7, 2017
1 parent 535d6ff commit db0a039
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 8 deletions.
61 changes: 61 additions & 0 deletions Recap/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,67 @@ public final class UpdateAddressMutation: GraphQLMutation {
}
}

public final class CreateAddressMutation: GraphQLMutation {
public static let operationDefinition =
"mutation CreateAddress($input: CreateAddressInput!) {" +
" createAddress(input: $input) {" +
" __typename" +
" changedAddress {" +
" __typename" +
" ...completeAddress" +
" }" +
" }" +
"}"
public static let queryDocument = operationDefinition.appending(CompleteAddress.fragmentDefinition)

public let input: CreateAddressInput

public init(input: CreateAddressInput) {
self.input = input
}

public var variables: GraphQLMap? {
return ["input": input]
}

public struct Data: GraphQLMappable {
/// Create objects of type Address.
public let createAddress: CreateAddress?

public init(reader: GraphQLResultReader) throws {
createAddress = try reader.optionalValue(for: Field(responseName: "createAddress", arguments: ["input": reader.variables["input"]]))
}

public struct CreateAddress: GraphQLMappable {
public let __typename: String
/// The mutated Address.
public let changedAddress: ChangedAddress?

public init(reader: GraphQLResultReader) throws {
__typename = try reader.value(for: Field(responseName: "__typename"))
changedAddress = try reader.optionalValue(for: Field(responseName: "changedAddress"))
}

public struct ChangedAddress: GraphQLMappable {
public let __typename: String

public let fragments: Fragments

public init(reader: GraphQLResultReader) throws {
__typename = try reader.value(for: Field(responseName: "__typename"))

let completeAddress = try CompleteAddress(reader: reader)
fragments = Fragments(completeAddress: completeAddress)
}

public struct Fragments {
public let completeAddress: CompleteAddress
}
}
}
}
}

public final class UpdateUserMutation: GraphQLMutation {
public static let operationDefinition =
"mutation UpdateUser($input: UpdateUserInput!) {" +
Expand Down
20 changes: 18 additions & 2 deletions Recap/CameraFlow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,25 @@ extension RootFlowCoordinator {
}

private func configure(vc: EnterAddressController) {
let vm = EnterAddressNewFriendViewModel(friendsListProvider: friendsListProvider, backAction: { [weak vc] in
let backAction: () -> Void = { [weak vc] in
vc?.dismiss(animated: true, completion: nil)
})
}

let nextAction: NextAction = { [weak self, weak vc] address in
HUD.show(.progress)
self?.userController.createAddress(newAddress: address, callback: { result in
HUD.hide()
switch result {
case let .error(err):
vc?.present(err.alert, animated: true, completion: nil)
case let .success(newAddress):
self?.friendsListProvider.addFriend(Friend(name: address.name, address: newAddress))
backAction()
}
})
}

let vm = EnterAddressNewFriendViewModel(backAction: backAction, nextAction: nextAction)
vc.viewModel = vm
}
}
8 changes: 2 additions & 6 deletions Recap/EnterAddressViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ class EnterAddressSignupViewModel: EnterAddressViewModel {
}

class EnterAddressNewFriendViewModel: EnterAddressViewModel {
init(friendsListProvider: FriendsListProvider, backAction: @escaping () -> Void) {
let weakBackAction: (() -> Void)? = backAction
super.init(headingText: "Add a friend", subheadingText: "We'll send their recaps here", buttonText: "Save", backAction: backAction, nextAction: { [weak friendsListProvider] address in
friendsListProvider?.addFriend(Friend(name: address.name, address: address))
weakBackAction?()
})
init(backAction: @escaping () -> Void, nextAction: @escaping NextAction) {
super.init(headingText: "Add a friend", subheadingText: "We'll send their recaps here", buttonText: "Save", backAction: backAction, nextAction: nextAction)
}
}
8 changes: 8 additions & 0 deletions Recap/User.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ mutation UpdateAddress($input: UpdateAddressInput!) {
}
}

mutation CreateAddress($input: CreateAddressInput!) {
createAddress(input: $input) {
changedAddress {
...completeAddress
}
}
}

mutation UpdateUser($input: UpdateUserInput!) {
updateUser(input: $input) {
changedUser {
Expand Down
20 changes: 20 additions & 0 deletions Recap/UserController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typealias SocialLoginCallback = (Result<Void, SocialError>) -> ()
typealias PhotosCallback = (Result<[Photo], PhotoError>) -> ()
typealias PhotoErrorOnlyCallback = (Result<Void, PhotoError>) -> Void
typealias VerifyInviteCodeCallback = (Result<Void, UserError>) -> Void
typealias CreateAddressCallback = (Result<Address, UserError>) -> Void

struct UserNotification {
static let userDidUpdate = Notification.Name("userDidUpdate")
Expand Down Expand Up @@ -159,6 +160,20 @@ extension UserController {
updateUser(input: input, callback: callback)
}

func createAddress(newAddress: Address, callback: @escaping CreateAddressCallback) {
let input = CreateAddressInput(city: newAddress.city, secondaryLine: newAddress.secondaryLine, name: newAddress.name, primaryLine: newAddress.primaryLine, zipCode: newAddress.zip, state: newAddress.state)
let mut = CreateAddressMutation(input: input)
graphql.client.perform(mutation: mut) { result, error in
guard let completeAddress = result?.data?.createAddress?.changedAddress?.fragments.completeAddress,
error == nil
else {
callback(.error(.uploadAddressFailed)); return;
}
let address = Address(completeAddress: completeAddress)
callback(.success(address))
}
}

func updateAddress(newAddress: Address, callback: @escaping UserCallback) {
guard let user = self.user else { return }
let input = UpdateAddressInput(id: user.address.id, userId: user.id, city: newAddress.city, secondaryLine: newAddress.secondaryLine, name: newAddress.name, primaryLine: newAddress.primaryLine, zipCode: newAddress.zip, state: newAddress.state)
Expand Down Expand Up @@ -278,6 +293,7 @@ enum UserError: AlertableError {
case unknownFailure
case invalidInviteCode
case couldNotFetchFacebookAddresses
case uploadAddressFailed

var localizedTitle: String {
switch self {
Expand All @@ -295,6 +311,8 @@ enum UserError: AlertableError {
return "Invalid Invite Code"
case .couldNotFetchFacebookAddresses:
return "Where's that?"
case .uploadAddressFailed:
return "Storing your address failed"
}
}

Expand All @@ -314,6 +332,8 @@ enum UserError: AlertableError {
return "Check that you entered the right code and try again, or vist recap-app.com to learn more."
case .couldNotFetchFacebookAddresses:
return "We ran into an issue getting your facebook friends address. Please check your internet connection and try again or send us an email at [email protected] with details."
case .uploadAddressFailed:
return "The address checks out but there was an issue storing it. Please check your internet connection and try again or send us an email at [email protected] with details."
}
}
}
Expand Down

0 comments on commit db0a039

Please sign in to comment.