Edit prober name action

This commit is contained in:
Cristian Ditaputratama 2024-05-07 23:23:31 +07:00
parent 09490a8250
commit 50588da322
Signed by: ditatompel
GPG key ID: 31D3D06D77950979
5 changed files with 92 additions and 8 deletions

View file

@ -1,7 +1,7 @@
<script>
import { DataHandler } from '@vincjo/datatables/remote';
import { format, formatDistance } from 'date-fns';
import { loadData, deleteData } from './api-handler';
import { loadData, deleteData, editProber } from './api-handler';
import { onMount, onDestroy } from 'svelte';
import { getModalStore, getToastStore } from '@skeletonlabs/skeleton';
import {
@ -14,6 +14,40 @@
const modalStore = getModalStore();
const toastStore = getToastStore();
/**
* @param {string} proberId
* @param {string} proberName
*/
function showEditModal(proberId, proberName) {
/** @type {import('@skeletonlabs/skeleton').ModalSettings} */
const modal = {
type: 'prompt',
// Data
title: 'Enter Name',
body: 'Enter a new name for the prober',
value: proberName,
valueAttr: { type: 'text', minlength: 3, maxlength: 50, required: true },
response: (r) => {
editProber(proberId, r)
.then((res) => {
if (res.status !== 'ok') {
toastStore.trigger({ message: 'Failed to edit prober' });
} else {
toastStore.trigger({
message: 'Prober edited',
background: 'variant-filled-success'
});
handler.invalidate();
}
})
.catch(() => {
toastStore.trigger({ message: 'Failed to edit prober' });
});
}
};
modalStore.trigger(modal);
}
/** @param {number} id */
const handleDelete = (id) => {
modalStore.trigger({
@ -143,7 +177,11 @@
<td>
{row.id}
<button
class="variant-filled-error btn btn-sm mr-1"
class="variant-filled-secondary btn btn-sm mr-1"
on:click={() => showEditModal(row.id, row.name)}>Edit</button
>
<button
class="variant-filled-error btn btn-sm"
name="delete_{row.id}"
on:click={() => {
handleDelete(row.id);

View file

@ -17,6 +17,19 @@ export const deleteData = async (id) => {
return json;
};
export const editProber = async (id, name) => {
const response = await fetch(apiUri(`/api/v1/prober/${id}`), {
method: 'PATCH',
credentials: 'include',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ name })
});
const json = await response.json();
return json;
};
const getParams = ({ pageNumber, rowsPerPage, sort, filters }) => {
let params = `page=${pageNumber}&limit=${rowsPerPage}`;

View file

@ -104,6 +104,31 @@ func Prober(c *fiber.Ctx) error {
"message": "Success",
"data": nil,
})
} else if c.Method() == "PATCH" {
payload := repo.Prober{}
if err := c.BodyParser(&payload); err != nil {
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
if payload.Name == "" {
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
"status": "error",
"message": "Please fill prober name",
"data": nil,
})
}
id, _ := strconv.Atoi(c.Params("id"))
err := proberRepo.Update(id, payload.Name)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
}
query := repo.ProbersQueryParams{

View file

@ -14,6 +14,7 @@ func V1Api(app *fiber.App) {
v1.Get("/prober", Prober)
v1.Post("/prober", Prober)
v1.Patch("/prober/:id", CookieProtected, Prober)
v1.Delete("/prober/:id", CookieProtected, Prober)
v1.Get("/nodes", MoneroNodes)
v1.Post("/nodes", AddNode)

View file

@ -12,6 +12,7 @@ import (
type ProberRepository interface {
AddProber(name string) error
Update(id int, name string) error
Probers(q ProbersQueryParams) (Probers, error)
CheckApi(key string) (Prober, error)
Delete(id int) error
@ -59,6 +60,18 @@ func (repo *ProberRepo) AddProber(name string) error {
return nil
}
func (repo *ProberRepo) Update(id int, name string) error {
query := `UPDATE tbl_prober SET name = ? WHERE id = ?`
_, err := repo.db.Exec(query, name, id)
return err
}
func (repo *ProberRepo) Delete(id int) error {
query := `DELETE FROM tbl_prober WHERE id = ?`
_, err := repo.db.Exec(query, id)
return err
}
func (repo *ProberRepo) Probers(q ProbersQueryParams) (Probers, error) {
queryParams := []interface{}{}
whereQueries := []string{}
@ -125,9 +138,3 @@ func (repo *ProberRepo) CheckApi(key string) (Prober, error) {
err := repo.db.QueryRow(query, key).Scan(&prober.Id, &prober.Name, &prober.ApiKey, &prober.LastSubmitTs)
return prober, err
}
func (repo *ProberRepo) Delete(id int) error {
query := `DELETE FROM tbl_prober WHERE id = ?`
_, err := repo.db.Exec(query, id)
return err
}