Browse Source

Delete templates with remove button in view

master
Immanuel Onyeka 1 year ago
parent
commit
bb1f7eaa70
2 changed files with 47 additions and 13 deletions
  1. +17
    -11
      components/estimates.vue
  2. +30
    -2
      skouter.go

+ 17
- 11
components/estimates.vue View File

@@ -57,7 +57,7 @@ ${{(template.estimate.price / 100).toLocaleString()}}
<label>Cash to close: ${{format(l.result.cashToClose)}}</label>
</div>
<button @click="() => download(template.estimate)">Generate PDF</button>
<button @click="() => deleting = true">Remove</button>
<button @click="() => deletingt = true">Remove</button>
<button @click="() => template = null">Cancel</button>
</div>
</section>
@@ -103,9 +103,15 @@ ${{(estimate.price / 100).toLocaleString()}}

<Dialog v-if="deleting" @close="() => deleting = false">
<h3>Are you sure you want to delete this estimate?</h3>
<button @click="() => del(estimate ?? template.estimate)">Confirm</button>
<button @click="() => del(estimate)">Confirm</button>
</Dialog>

<Dialog v-if="deletingt" @close="() => deletingt = false">
<h3>Are you sure you want to delete this template?</h3>
<button @click="() => delt(template)">Confirm</button>
</Dialog>


</div>
</template>

@@ -124,7 +130,8 @@ let templates = ref([])
let estimate = ref()
let template = ref()
let dlink = ref("")
let deleting = ref()
let deleting = ref(false)
let deletingt = ref(false)

function newFee(fee, isDebit) {
if (!isDebit) {
@@ -188,7 +195,7 @@ function getEstimates() {
}

function getTemplates() {
fetch(`/api/estimate/templates`,
fetch(`/api/templates`,
{method: 'GET',
headers: {
"Accept": "application/json",
@@ -201,12 +208,11 @@ function getTemplates() {
}).then (result => {
if (!result || !result.length) return // Exit if token is invalid or no fees are saved
templates.value = result
console.log(result)
})
}

function saveTemplate(e) {
fetch(`/api/estimate/templates`,
fetch(`/api/templates`,
{method: 'POST',
body: JSON.stringify(e),
headers: {
@@ -235,8 +241,8 @@ function del(e) {
})
}

function remove(t) {
fetch(`/api/estimate/template/remove`,
function delt(t) {
fetch(`/api/templates`,
{method: 'DELETE',
body: JSON.stringify(t),
headers: {
@@ -245,9 +251,9 @@ function remove(t) {
},
}).then(response => {
if (response.ok) {
estimates.value = estimates.value.filter(e => e.id != estimate.value.id)
estimate.value = null
deleting.value = false
templates.value = templates.value.filter(t => t.id != template.value.id)
template.value = null
deletingt.value = false
}
})
}


+ 30
- 2
skouter.go View File

@@ -593,6 +593,18 @@ func deleteEstimate(w http.ResponseWriter, db *sql.DB, r *http.Request) {
if err != nil { http.Error(w, err.Error(), 500); return }
}

func deleteET(w http.ResponseWriter, db *sql.DB, r *http.Request) {
var et ETemplate
var err error
err = json.NewDecoder(r.Body).Decode(&et)
if err != nil { w.WriteHeader(422); return }
claims, err := getClaims(r)
err = et.del(db, claims.Id)
if err != nil { http.Error(w, err.Error(), 500); return }
}


func getMi(db *sql.DB, loan int) (MI, error) {
var mi MI
@@ -2113,6 +2125,18 @@ func (estimate *Estimate) del(db *sql.DB, user int) (error) {
return nil
}

func (et *ETemplate) del(db *sql.DB, user int) (error) {
var query string
var err error

query = `DELETE FROM estimate_template WHERE id = ? AND user_id = ?`
_, err = db.Exec(query, et.Id, user)
if err != nil { return err }

return nil
}

func (eTemplate *ETemplate) insert(db *sql.DB) (error) {
var query string
var row *sql.Row
@@ -2554,14 +2578,18 @@ func api(w http.ResponseWriter, r *http.Request) {
r.Method == http.MethodPost &&
guard(r, 1):
summarize(w, db, r)
case match(p, "/api/estimate/templates", &args) &&
case match(p, "/api/templates", &args) &&
r.Method == http.MethodGet &&
guard(r, 1):
getETemplates(w, db, r)
case match(p, "/api/estimate/templates", &args) &&
case match(p, "/api/templates", &args) &&
r.Method == http.MethodPost &&
guard(r, 1):
createETemplate(w, db, r)
case match(p, "/api/templates", &args) &&
r.Method == http.MethodDelete &&
guard(r, 1):
deleteET(w, db, r)
case match(p, "/api/pdf", &args) &&
r.Method == http.MethodPost &&
guard(r, 1):


Loading…
Cancel
Save