Skip to content

Commit

Permalink
chore: get vendors on server component
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelovicentegc committed Nov 15, 2023
1 parent e7303ce commit 2403fef
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 53 deletions.
6 changes: 2 additions & 4 deletions app/api-management/page.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import ApiManagementContainer from "@/containers/api-management";
import { f } from "@/lib/fetch";
import { getApiKeys } from "@/lib/db/reads";

export default async function ApiManagementPage() {
const res = await f("/api/vendors");

const vendors = await res.json();
const vendors = await getApiKeys();

return <ApiManagementContainer vendors={vendors} />;
}
23 changes: 0 additions & 23 deletions app/api/vendors/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { NextResponse } from "next/server";
import { authOptions } from "../auth/[...nextauth]/route";
import { saveApiKeys } from "@/lib/db/writes";
import { encrypt } from "@/lib/encryption";
import { getApiKeys } from "@/lib/db/reads";
import { getServerSession } from "next-auth/next";

export async function POST(req) {
Expand Down Expand Up @@ -74,25 +73,3 @@ export async function POST(req) {
});
}
}

export async function GET() {
const session = await getServerSession(authOptions);

if (!session) {
return new NextResponse(JSON.stringify([]), { status: 401 });
}

try {
const vendors = await getApiKeys();

return new NextResponse(JSON.stringify(vendors), {
status: 200,
});
} catch (error) {
console.error(error);

return new NextResponse("Error", {
status: 500,
});
}
}
25 changes: 13 additions & 12 deletions containers/api-management.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import {
} from "grommet";
import { useState } from "react";
import { StatusGood, More } from "grommet-icons";
import { useRefreshData } from "@/lib/hooks";

export default function ApiManagementContainer(props) {
const { vendors } = props;
const { refresh } = useRefreshData();

const [open, setOpen] = useState(false);
const [submitting, setSubmitting] = useState(false);
Expand All @@ -27,11 +29,7 @@ export default function ApiManagementContainer(props) {
const onSubmit = async (event) => {
setSubmitting(true);
const {
value: {
name: vendorName,
url: vendorUrl,
callbackUrl: vendorCallbackUrl,
},
value: { vendorName, vendorUrl, vendorCallbackUrl },
} = event;

const formattedUrl = new URL(vendorUrl);
Expand All @@ -58,6 +56,8 @@ export default function ApiManagementContainer(props) {
so make sure you copy it and store it somewhere safe.`,
JSON.stringify(data),
);

refresh();
}

setSubmitting(false);
Expand Down Expand Up @@ -134,8 +134,7 @@ function Form(props) {
>
<FormField
label="Vendor Name"
aria-label="name"
name="name"
name="vendorName"
required
validate={[
{ regexp: /^[a-z]/i },
Expand All @@ -154,10 +153,12 @@ function Form(props) {
};
},
]}
/>
>
<TextInput name="vendorName" />
</FormField>
<FormField
label="Vendor URL"
name="url"
name="vendorUrl"
required
validate={[
(url) => {
Expand All @@ -180,11 +181,11 @@ function Form(props) {
},
]}
>
<TextInput name="url" aria-label="url" type="url" />
<TextInput name="vendorUrl" aria-label="url" type="url" />
</FormField>
<FormField
label="Callback URL"
name="url"
name="vendorCallbackUrl"
required
validate={[
(url) => {
Expand All @@ -207,7 +208,7 @@ function Form(props) {
},
]}
>
<TextInput name="callbackUrl" aria-label="url" type="url" />
<TextInput name="vendorCallbackUrl" aria-label="url" type="url" />
</FormField>
</FormPopUp>
);
Expand Down
14 changes: 0 additions & 14 deletions lib/fetch.js

This file was deleted.

19 changes: 19 additions & 0 deletions lib/hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useRouter } from "next/navigation";
import { useTransition } from "react";

/**
* Refresh the current route and fetch new data from the server without
* losing client-side browser or React state.
*/
export function useRefreshData() {
const router = useRouter();
const [_, startTransition] = useTransition();

const refresh = () => {
startTransition(() => {
router.refresh();
});
};

return { refresh };
}

0 comments on commit 2403fef

Please sign in to comment.