Skip to content

Commit

Permalink
add network action if errors
Browse files Browse the repository at this point in the history
  • Loading branch information
sinamics committed Aug 9, 2023
1 parent 985edc6 commit 825b5b4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 30 deletions.
5 changes: 5 additions & 0 deletions src/components/modules/anotation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Input from "~/components/elements/input";
import { api } from "~/utils/api";
import { getRandomColor } from "~/utils/randomColor";
import { useTranslations } from "next-intl";
import { toast } from "react-hot-toast";

type IAnotationProps = {
name: string;
Expand Down Expand Up @@ -58,6 +59,10 @@ const Anotation = ({ nwid, nodeid }: IProps) => {
(anotation) => anotation.name === value,
);
if (isInList) {
if (!nodeid)
return toast.error(
"Member does not exist in DB, please Authorize first",
);
setAnotation(
{
name: value,
Expand Down
14 changes: 13 additions & 1 deletion src/components/modules/table/memberHeaderColumns.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,19 @@ export const MemberHeaderColumns = ({ nwid, central = false }: IProp) => {
),
id: "creationTime",
cell: (info) => {
return <TimeAgo date={info.getValue()} />;
const formatTime = (value: string, unit: string) => {
// Map full unit names to their abbreviations
const unitAbbreviations: { [key: string]: string } = {
second: "s ago",
minute: "m ago",
hour: "hr ago",
day: "dy ago",
};
const abbreviation = unitAbbreviations[unit] || unit;

return `${value} ${abbreviation}`;
};
return <TimeAgo date={info.getValue()} formatter={formatTime} />;
},
}),
columnHelper.accessor("peers", {
Expand Down
45 changes: 36 additions & 9 deletions src/pages/network/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,56 @@ const NetworkById = () => {
{ enabled: !!query.id, refetchInterval: 10000 },
);
const { network, members = [] } = networkById || {};
if (loadingNetwork) {
// add loading progress bar to center of page, vertially and horizontally

if (errorNetwork) {
return (
<div className="flex flex-col items-center justify-center">
<h1 className="text-center text-2xl font-semibold">
<progress className="progress progress-primary w-56" />
{errorNetwork.message}
</h1>
<ul className="list-disc">
<li>{t("errorSteps.step1")}</li>
<li>{t("errorSteps.step2")}</li>
</ul>
<div className="w-5/5 divider mx-auto flex px-4 py-4 text-sm sm:w-4/5 sm:px-10 md:text-base">
Network Actions
</div>
<div className="w-5/5 mx-auto px-4 py-4 text-sm sm:w-4/5 sm:px-10 md:flex-row md:text-base">
<div className="flex items-end md:justify-end">
<button
onClick={() =>
callModal({
title: `Delete network ${network?.name}`,
description:
"Are you sure you want to delete this network? This cannot be undone and all members will be deleted from this network",
yesAction: () => {
deleteNetwork(
{ nwid: query.id as string },
{ onSuccess: () => void router("/network") },
);
},
})
}
className="btn btn-error btn-outline btn-wide"
>
Delete network
</button>
</div>
</div>
</div>
);
}
if (errorNetwork) {
if (loadingNetwork) {
// add loading progress bar to center of page, vertially and horizontally
return (
<div className="flex flex-col items-center justify-center">
<h1 className="text-center text-2xl font-semibold">
{errorNetwork.message}
<progress className="progress progress-primary w-56" />
</h1>
<ul className="list-disc">
<li>{t("errorSteps.step1")}</li>
<li>{t("errorSteps.step2")}</li>
</ul>
</div>
);
}

return (
<div>
<div className="w-5/5 mx-auto flex flex-row flex-wrap justify-between space-y-10 p-4 text-sm sm:w-4/5 sm:p-10 md:text-base xl:space-y-0">
Expand Down
8 changes: 3 additions & 5 deletions src/server/api/routers/networkRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ export const networkRouter = createTRPCRouter({
.catch((err: APIError) => {
throwError(`${err.message}`);
});

// console.log(JSON.stringify(ztControllerResponse, null, 2));
if (!ztControllerResponse)
return throwError("Failed to get network details!");
Expand Down Expand Up @@ -214,10 +213,9 @@ export const networkRouter = createTRPCRouter({
.mutation(async ({ ctx, input }) => {
try {
// Delete ZT network
const createCentralNw = await ztController.network_delete(
input.nwid,
input.central,
);
const createCentralNw = await ztController
.network_delete(input.nwid, input.central)
.catch(() => []);

if (input.central) return createCentralNw;
// Delete networkMembers
Expand Down
22 changes: 7 additions & 15 deletions src/utils/ztApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,13 @@ export const network_members = async function (
) {
// get headers based on local or central api
const { headers, ztCentralApiUrl } = await getOptions(isCentral);
try {
const addr = isCentral
? `${ztCentralApiUrl}/network/${nwid}/member`
: `${LOCAL_ZT_ADDR}/controller/network/${nwid}/member`;

// fetch members
return await getData<MemberEntity[]>(addr, headers);
} catch (error: unknown) {
const prefix = isCentral ? "[CENTRAL] " : "";
const message = `${prefix}An error occurred while getting network_members`;
throw new APIError(message, error as AxiosError);
}
const addr = isCentral
? `${ztCentralApiUrl}/network/${nwid}/member`
: `${LOCAL_ZT_ADDR}/controller/network/${nwid}/member`;

// fetch members
return await getData<MemberEntity[]>(addr, headers);
};

export const local_network_detail = async function (
Expand All @@ -338,7 +333,6 @@ export const local_network_detail = async function (
try {
// get all members for a specific network
const members = await network_members(nwid);

const network = await getData<NetworkEntity>(
`${LOCAL_ZT_ADDR}/controller/network/${nwid}`,
headers,
Expand All @@ -358,9 +352,7 @@ export const local_network_detail = async function (
members: [...membersArr],
};
} catch (error) {
const message =
"An error occurred while getting data from network_details function";
throw new APIError(message, error as AxiosError);
throw new APIError(error, error as AxiosError);
}
};
// Get network details
Expand Down

0 comments on commit 825b5b4

Please sign in to comment.