From 84d6807b94cb9db881acd5cdf1f270fdbbf43d20 Mon Sep 17 00:00:00 2001
From: Immanuel Onyeka <immanuel@debian-BULLSEYE-live-builder-AMD64>
Date: Wed, 21 Jun 2023 21:11:13 -0400
Subject: [PATCH] Create sample custom MI field

---
 components/new.vue | 28 ++++++++++++++++++++--------
 skouter.go         | 14 ++++++++++++++
 2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/components/new.vue b/components/new.vue
index dc4fc3a..45c1419 100644
--- a/components/new.vue
+++ b/components/new.vue
@@ -159,14 +159,13 @@ v-model="estimate.transaction">
 
 <section class="form radios">
 <h3>Mortgage Insurance</h3>
-<input type="radio" name="transaction_type" value="transaction"
-@input="e => estimate.transaction = 0"
-selected="estimate.transaction == 0">
-<label>1.43% - National MI</label>
-<input type="radio" name="transaction_type" value="refinance"
-@input="e => estimate.transaction = 1"
-selected="estimate.transaction == 1">
-<label>0.73% - MGIC</label>
+<input type="radio">
+<span>
+    <label>Custom %</label>
+    <input type="text" :value="estimate.loans[sel].mi"
+    @input="e => estimate.loans[sel].mi = strip(e)"
+    selected="estimate.transaction == 0">
+</span>
 </section>
 
 <section class="form inputs">
@@ -207,6 +206,7 @@ const example = {
 		pud: true, // Property under development
 		zip: '',
 		fees: [],
+		mi: 0
 }
 
 // The default loans on a new estimate
@@ -331,6 +331,18 @@ function setHousingDti(e) {
 
 function generate() {
 	this.errors = this.validate()
+	if (this.errors.length) return
+	
+	 fetch(`/api/estimate`,
+		{
+		    method: 'POST',
+		    body: JSON.stringify( this.estimate ),
+		    headers: {
+			    "Accept": "application/json",
+		        "Authorization": `Bearer ${token}`,
+        	},
+    	}
+    )
 }
 
 function validate() {
diff --git a/skouter.go b/skouter.go
index a5688c6..82f3d6f 100644
--- a/skouter.go
+++ b/skouter.go
@@ -863,6 +863,16 @@ func createUser(w http.ResponseWriter, db *sql.DB, r *http.Request) {
 	json.NewEncoder(w).Encode(user)
 }
 
+func createEstimate(w http.ResponseWriter, db *sql.DB, r *http.Request) {
+	var estimate Estimate
+	err := json.NewDecoder(r.Body).Decode(&estimate)
+	if err != nil { http.Error(w, "Invalid fields.", 422); return }
+
+	if err != nil { http.Error(w, "Error creating user.", 422); return }
+
+	json.NewEncoder(w).Encode(estimate)
+}
+
 func api(w http.ResponseWriter, r *http.Request) {
 	var args []string
 
@@ -914,6 +924,10 @@ func api(w http.ResponseWriter, r *http.Request) {
 	r.Method == http.MethodGet &&
 	guard(r, 1):
 		getFeesTemp(w, db, r)
+	case match(p, "/api/estimate", &args) &&
+	r.Method == http.MethodPost &&
+	guard(r, 1):
+		createEstimate(w, db, r)
 	}
 
 	db.Close()