Procuret / procuret-swift

Procuret API Swift library
0 stars 1 forks source link

create `Human` test app #17

Closed kaythoyet closed 1 year ago

kaythoyet commented 2 years ago

Hey Hugh,

Leaving this here because I figured it might be easier while I sort out family stuff.

Code for content view looks like this. This part was simple and I think it looks pretty good. I didn't bring in a logo or anything yet but I figure I can add that later as I play with this a bit more.


//
//  ContentView.swift
//  Create Human
//
//  Created by Kayla Hoyet on 12/1/21.
//

import SwiftUI

struct ContentView: View {

    @State var firstName = ""
    @State var lastName = ""
    @State var emailAddress = ""
    @State var phone = ""

    var body: some View {
        NavigationView {
            VStack {
                Form {
                    Section {
                        TextField("First Name", text: $firstName)
                        TextField("Last Name", text: $lastName)
                        TextField("Email Address", text: $emailAddress)
                        TextField("Phone Number", text: $phone)
                    }

                }

                Button(action: {
                }, label: {
                    Text("Create Human")
                        .frame(
                            width: 200,
                            height: 50,
                            alignment: .center
                        )
                        .background(Color.green)
                        .foregroundColor(.white)
                        .cornerRadius(4.0)
                        .padding()
                })
                .padding()

            }
            .navigationTitle("Create a Human")
        }
    }

    struct ContentView_Previews: PreviewProvider {
        static var previews: some View {
            Group {
                ContentView()
                    .preferredColorScheme(.light)
            }
        }
    }
}

I watched this video when I was first getting started. It was great for learning about the stacks and seeing the previewer at work. Since I knew I needed text fields and that video didn't talk about how to implement those, I tried a few different things that I thought made sense and then did some googling. This is the reason I used binding for the text fields. Most other resources I found seemed to do it this way as well. This allowed me to be able to write in the text fields when I made the preview live:

Screen Shot 2021-12-02 at 2 26 19 PM

Text fields? Check. Button? Kinda check.

It's there, but it doesn't do anything yet, obviously. There's no action assigned to it. I got myself hung up on how to make it do what I want it to because I think I'll need to pull the Swift library in, and I'm not sure how to do that. In the example from the Apple Developer video, he simply does a drag and drop, but that gets complicated with this one because the compiler throws a bunch of errors because it can't find the other associated types. I tried to simply copy and add the associated Types, and there was a lot of them, but it did get rid of the errors. My issue now is figuring out how to properly implement everything so that the button works when you click it.

hwjeremy commented 2 years ago

@kaythoyet great stuff. Just go ahead and copy the entire Swift library into your project for now. In future you will be able to import it nicely.

Looking forwa

hwjeremy commented 2 years ago

... whoops, accidentally hit "close with comment".

Looking forward to more progress notes, it's great to see you playing with an actual UI like this.

kaythoyet commented 2 years ago

@hwjeremy

Update:

Currently in progress working on what we talked about yesterday. I adjust the DocumentRevision file in my version of the Swift repository so that it wasn't throwing errors when I copied the repository files into the project, but when I copied the files over, everything sort of exploded. This also led me to a question though, so figured I'd post this here. I may attempt this before I get a response from you anyway, just to see if it helps.

Do I need to also copy over the Human test files from the repository? They are separate from the source code, so they didn't copy over on the first attempt.

Investigating now. I was able to discard the changes and get it back to where I had it, but I am going to pull that video back up real quick and verify a couple things:

  1. Where the folder the demonstrator used that contained his code was pulled into inside the project, and
  2. How he made those files 'discoverable' so that he was able to pull the proper information into his app.

I think once I verify this I will be back in business. I am determined to move this forward before the holidays!

kaythoyet commented 2 years ago

@hwjeremy Test successful. :)

import SwiftUI

struct ContentView: View {

    @State var firstName = ""
    @State var lastName = ""
    @State var emailAddress = ""
    @State var phone = ""

    func provideTestSession() -> Session {

        do {
            return try Session.fromEnvironmentVariables()
        } catch {
            fatalError("Unable to initalise test session: \(error)")
        }
    }

    func generateHuman(error: Error?, human: Human?) {

        if (human == nil) {
            print("Human is nil.")
        }

        if (error != nil) {
            print("An error has occurred.")
        }

        return
    }

    var body: some View {
        NavigationView {
            VStack {
                Form {
                    Section {
                        TextField("First Name", text: $firstName)
                        TextField("Last Name", text: $lastName)
                        TextField("Email Address", text: $emailAddress)
                        TextField("Phone Number", text: $phone)
                    }
                }
                Button("Test", action: {
                    Human.create(
                        firstName: firstName,
                        lastName: lastName,
                        emailAddress: emailAddress,
                        phone: phone,
                        secret: "coffee phone table",
                        existingPhone: nil,
                        verifyPhone: false,
                        creationNote: nil,
                        session: provideTestSession(),
                        hasAgentSecret: false,
                        signupPerspective: Perspective.business,
                        callback: generateHuman
                    )
                })
                .frame(
                    width: 200,
                    height: 50,
                    alignment: .center
                )
                .background(Color.green)
                .foregroundColor(.white)
                .cornerRadius(12.0)
                .padding()
            }
            .navigationTitle("Create a Human")
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Code for reference.

Output of test:

--- Begin raw data returned by Procuret API ---
{
    "active_entity_id": null,
    "code": "126463",
    "created": "2021-12-22 17:05:55.243262",
    "disposition": {
        "count": 1,
        "limit": 1,
        "offset": 0,
        "sequence": 1
    },
    "documents": null,
    "email_address": {
        "confirmation_received": null,
        "confirmation_request_sent": null,
        "confirmation_required": true,
        "created": "2021-12-22 17:05:55.215215",
        "email_address": "kayla.h+0990@procuret.com",
        "indexid": 262
    },
    "first_name": "TestKayla",
    "identity": null,
    "indexid": 13717147914292286,
    "last_name": "Hoyet",
    "modified": "2021-12-22 17:05:55.243262",
    "other_name": null,
    "phone_number": {
        "confirmation_request_sent": "2021-12-22 17:05:56.322677",
        "confirmation_required": true,
        "confirmed": false,
        "digits": "+17654615534",
        "indexid": 312
    },
    "public_id": 13717147914292286,
    "reset_key": null
}
--- End raw data returned by Procuret API ---

Screenshot (with button formatting as seen in code above):

Screen Shot 2021-12-22 at 12 32 33 PM