Skip to content

Commit

Permalink
Merge pull request #2593 from threefoldtech/development_2.5_publicips…
Browse files Browse the repository at this point in the history
…_table

Remove farms public IP delete button and create  delete all action with it's module and method
  • Loading branch information
samaradel authored May 13, 2024
2 parents 4364940 + 0a0b566 commit 4d7e369
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 43 deletions.
3 changes: 2 additions & 1 deletion packages/grid_client/src/clients/tf-grid/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ValidationError } from "@threefold/types";
import { KeypairType } from "../../zos/deployment";
import { TFBalances } from "./balances";
import { TFContracts } from "./contracts";
import { TFFarms } from "./farms";
import { TFKVStore } from "./kvstore";
import { TFTermsAndConditions } from "./terms_and_conditions";
import { TFTBridge } from "./tftBridge";
Expand All @@ -18,7 +19,7 @@ class TFClient extends Client {
kvStore: TFKVStore = new TFKVStore(this);
termsAndConditions: TFTermsAndConditions = new TFTermsAndConditions(this);
tftBridge: TFTBridge = new TFTBridge(this);

farms: TFFarms = new TFFarms(this);
constructor(
public url: string,
public mnemonic: string,
Expand Down
25 changes: 25 additions & 0 deletions packages/grid_client/src/clients/tf-grid/farms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { ExtrinsicResult, Farm, Farms } from "@threefold/tfchain_client";

import { RemoveFarmIPModel } from "../../modules";

/**
* `TFFarms` is a subclass of `Farms` that provides additional functionality.
*/
class TFFarms extends Farms {
/**
* Removes farm IPs
*
* @param {RemoveFarmIPModel[]} options - An array of options used to remove farm IPs
* @returns {Promise<void>} A promise that resolves when all extrinsics have been applied
* @see {applyAllExtrinsics} - The method used to apply all extrinsics
*/
async removeFarmIps(options: RemoveFarmIPModel[]) {
const extrinsics: ExtrinsicResult<Farm>[] = [];
for (const option of options) {
extrinsics.push(await this.removeFarmIp(option));
}
await this.client.applyAllExtrinsics(extrinsics);
}
}

export { TFFarms };
1 change: 1 addition & 0 deletions packages/grid_client/src/clients/tf-grid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from "./balances";
export * from "./tftPrice";
export * from "./terms_and_conditions";
export * from "./tftBridge";
export * from "./farms";
13 changes: 13 additions & 0 deletions packages/grid_client/src/modules/farms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,19 @@ class Farms {
return (await this.client.farms.removeFarmIp(options)).apply();
}

@expose
@validateInput
@checkBalance
async removeFarmIps(options: RemoveFarmIPModel[]) {
/**
* Removes farm IPs
*
* @param {RemoveFarmIPModel[]} options - An array of options used to remove farm IPs
* @returns {Promise<void>} A promise that resolves when all farm IPs have been removed
*/
return await this.client.farms.removeFarmIps(options);
}

@expose
@validateInput
@checkBalance
Expand Down
84 changes: 43 additions & 41 deletions packages/playground/src/dashboard/components/public_ips_table.vue
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
<template>
<div>
<v-data-table-server
<v-data-table
:loading="loadingIps"
loading-text="Loading farm IPs..."
:headers="headers"
:items="copyPublicIps"
:items-length="publicIps.length"
:items-per-page="size"
:items-per-page-options="[
{ value: 5, title: '5' },
{ value: 10, title: '10' },
{ value: 15, title: '15' },
{ value: 20, title: '20' },
{ value: 50, title: '50' },
]"
:page="page"
@update:items-per-page="size => updateIPPageSize(size)"
@update:page="page => updateIPPage(page)"
class="elevation-1 v-data-table-header"
density="compact"
:disable-sort="true"
hide-default-header
:hover="true"
hover
show-select
v-model="selectedItems"
>
<template v-slot:top>
<v-alert class="pa-5" style="height: 20px">
Expand All @@ -38,39 +32,42 @@
<template #[`item.contractId`]="{ item }">
{{ item.contractId ?? "-" }}
</template>
<template #[`item.actions`]="{ item, index }">
<v-btn
class="text-subtitle-2"
size="small"
color="error"
@click="
() => {
showDialogue = true;
itemToDelete = { ip: item.ip, index };
}
"
:disabled="loading"
:loading="loading"
>
Delete
</v-btn>
<template #bottom>
<div class="d-flex align-end justify-end">
<v-btn
class="ma-5"
color="error"
variant="outlined"
prepend-icon="mdi-delete"
:disabled="selectedItems.length === 0 || isRemoving"
@click="showDialogue = true"
>
Delete
</v-btn>
</div>
</template>
</v-data-table-server>
</v-data-table>
<v-dialog v-model="showDialogue" max-width="600">
<v-card>
<v-toolbar color="primary" class="custom-toolbar">
<p class="mb-5">Delete IP</p>
<p class="mb-5">Delete IPs</p>
</v-toolbar>
<v-card-text class="text-subtitle-1">Delete IP {{ itemToDelete?.ip }}? </v-card-text>
<v-card-title class="text-subtitle-1">
<strong>Delete the following IPs?</strong>
</v-card-title>
<v-card-text>
<v-chip class="ma-1" color="primary" v-for="item in selectedItems" :key="item">
{{ item.ip }}
</v-chip>
</v-card-text>
<v-card-actions class="justify-end px-5 pb-5 pt-0">
<v-btn @click="showDialogue = false" variant="outlined" color="anchor">Close</v-btn>
<v-btn
variant="outlined"
text="Confirm"
:text="isRemoving ? 'Deleting..' : 'Confirm'"
color="error"
:loading="isRemoving"
:disabled="isRemoving"
@click="removeFarmIp({ farmId: $props.farmId, ip: itemToDelete?.ip }, itemToDelete?.index)"
@click="removeFarmIps"
></v-btn>
</v-card-actions>
</v-card>
Expand Down Expand Up @@ -117,7 +114,6 @@ export default {
align: "center",
key: "contractId",
},
{ title: "Actions", align: "center", sortable: false, key: "actions" },
] as any;
const publicIps = ref<PublicIp[]>([]);
const copyPublicIps = ref<PublicIp[]>([]);
Expand All @@ -129,9 +125,10 @@ export default {
const toPublicIP = ref();
const gateway = ref();
const isRemoving = ref(false);
const itemToDelete = ref();
const size = ref(5);
const page = ref(1);
const selectedItems = ref<any[]>([]);
const items = ref<RemoveFarmIPModel[]>([]);
onMounted(async () => {
await getFarmByID(props.farmId);
Expand Down Expand Up @@ -163,19 +160,23 @@ export default {
}
loadingIps.value = false;
}
async function removeFarmIp(options: RemoveFarmIPModel, index: number) {
async function removeFarmIps() {
try {
isRemoving.value = true;
await gridStore.grid.farms.removeFarmIp({ ip: options.ip, farmId: options.farmId });
items.value = selectedItems.value.map(item => ({
ip: item.ip,
farmId: props.farmId,
}));
await gridStore.grid.farms.removeFarmIps(items.value);
createCustomToast("IP is deleted successfully!", ToastType.success);
publicIps.value.splice(index, 1);
await getFarmByID(props.farmId);
} catch (error) {
console.log(error);
createCustomToast("Failed to delete IP!", ToastType.danger);
} finally {
isRemoving.value = false;
showDialogue.value = false;
itemToDelete.value = undefined;
selectedItems.value = [];
}
}
watch(
Expand All @@ -185,6 +186,7 @@ export default {
},
{ deep: true },
);
return {
gridStore,
headers,
Expand All @@ -196,14 +198,14 @@ export default {
loading,
showDialogue,
isRemoving,
itemToDelete,
removeFarmIp,
removeFarmIps,
page,
size,
updateIPPageSize,
updateIPPage,
copyPublicIps,
selectedItems,
loadingIps,
updateIPPageSize,
};
},
};
Expand Down
3 changes: 2 additions & 1 deletion packages/tfchain_client/src/farms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Client, QueryClient } from "./client";
import type { ExtrinsicResult } from "./types";
import { checkConnection } from "./utils";

enum Certification {
Expand Down Expand Up @@ -89,7 +90,7 @@ class Farms extends QueryFarms {
@checkConnection
async removeFarmIp(options: RemoveFarmIPOptions) {
const extrinsic = this.client.api.tx.tfgridModule.removeFarmIp(options.farmId, options.ip);
return this.client.patchExtrinsic(extrinsic);
return this.client.patchExtrinsic<Farm>(extrinsic);
}

@checkConnection
Expand Down

0 comments on commit 4d7e369

Please sign in to comment.