view lazybear/Views/InsiderTransactions.swift @ 114:4c172ff9af20

Add optional values to all the models
author Dennis Concepción Martín <66180929+denniscm190@users.noreply.github.com>
date Thu, 04 Feb 2021 20:07:29 +0100
parents 16f8514cc5e6
children 2bc9e5c0a7c6
line wrap: on
line source

//
//  Insiders.swift
//  LazyBear
//
//  Created by Dennis Concepción Martín on 1/2/21.
//

import SwiftUI

struct InsiderTransactions: View {
    var symbol: String
    @EnvironmentObject var apiAccess: ApiAccess
    
    // <--------- API Job --------->
    @State private var url = String() {
        didSet { request(url: url, model: [InsiderTransactionModel].self) { self.data = $0 } }}
    
    @State private var data = [InsiderTransactionModel]()
    // <--------- API Job --------->
    
    var body: some View {
        VStack(alignment: .leading) {
            Text("Insider transactions")
                .font(.title)
                .fontWeight(.semibold)
                .padding([.leading, .bottom])
            
            ForEach(data, id: \.self) { data in
                TransactionRow(data: data)
            }
        }
        .onAppear { getUrl() }
    }
    
    private func getUrl() {
        // 1 -> Sandbox / 2 -> Production
        let baseUrl = apiAccess.results[1].url ?? ""
        let token = apiAccess.results[1].key ?? ""
        let path = "/stable/stock/\(symbol)/insider-transactions?token="
        
        self.url = baseUrl + path + token
    }
}

struct InsiderTransactions_Previews: PreviewProvider {
    static var previews: some View {
        InsiderTransactions(symbol: "aapl")
    }
}