Skip to content

Commit

Permalink
feat(client): you can now generate and start a APO dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivo committed Apr 15, 2023
1 parent 6e4f8f9 commit be9cb52
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 23 deletions.
1 change: 1 addition & 0 deletions apps/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@radix-ui/react-switch": "^1.0.2",
"@tailwindcss/forms": "^0.5.3",
"@tanstack/react-query": "^4.29.1",
"@tanstack/react-query-devtools": "^4.29.1",
"@vercel/og": "^0.4.1",
"axios": "^1.3.4",
"clsx": "^1.2.1",
Expand Down
4 changes: 2 additions & 2 deletions apps/client/src/components/Layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export function Header({ setTheme, theme }: MainNavProps) {
});

return (
<nav className="h-14 md:h-full md:w-60 bg-zinc-800/10 p-2">
<nav className="h-14 md:h-full md:w-80 bg-zinc-800/10 p-2">
<div className="flex flex-1 h-full justify-between md:hidden">
<Image
alt="Visual Dynamics"
Expand All @@ -121,7 +121,7 @@ export function Header({ setTheme, theme }: MainNavProps) {
<div className="hidden md:border-b md:border-b-zinc-400/50 md:pb-2 md:block md:sticky md:top-0">
<Image
alt="Visual Dynamics"
className="w-full"
className="w-2/3 mx-auto"
height={0}
src="/images/logo.svg"
width={0}
Expand Down
8 changes: 4 additions & 4 deletions apps/client/src/components/SelectTheme/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ export const SelectTheme: FC<SelectThemeProps> = ({ setTheme, theme }) => (
value={item}
>
<div
className={`rounded-full w-6 h-6 my-auto border-2 border-white transition-all ${bg[i]} ${hoverBg[i]} hover:border-zinc-200`}
className={`rounded-full w-6 h-6 my-auto border-2 border-white transition-all ${bg[i]} ${hoverBg[i]} flex items-center justify-center hover:border-zinc-200`}
>
<DropdownMenu.ItemIndicator className="flex w-full h-full items-center justify-center">
<Icons.Check />
<DropdownMenu.ItemIndicator className="flex items-center justify-center">
<Icons.Check className="mt-0.5 stroke-primary-100 stroke-[3] w-[75%] h-[75%]" />
</DropdownMenu.ItemIndicator>
</div>
</DropdownMenu.RadioItem>
))}
</DropdownMenu.RadioGroup>

<DropdownMenu.Arrow className="fill-primary-400/40" />
<DropdownMenu.Arrow className="fill-primary-950/60" />
</DropdownMenu.Content>
</DropdownMenu.Root>
);
2 changes: 1 addition & 1 deletion apps/client/src/lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios from "axios";

export const api = axios.create({
baseURL: "http://localhost:5000/api/v1"
baseURL: "http://127.0.0.1:5000/api/v1"
});
3 changes: 3 additions & 0 deletions apps/client/src/lib/query-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { QueryClient } from "@tanstack/react-query";

export const queryClient = new QueryClient();
28 changes: 18 additions & 10 deletions apps/client/src/pages/_app.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import React, { Suspense, useState } from "react";
import { QueryClientProvider } from "@tanstack/react-query";
import { ReactQueryDevtools } from "@tanstack/react-query-devtools";
import { AppProps } from "next/app";
import { Inter } from "next/font/google";
import Script from "next/script";

import { Footer } from "@app/components/Layout/Footer";
import { Header } from "@app/components/Layout/Header";
import { queryClient } from "@app/lib/query-client";

import "@app/styles/globals.css";

Expand All @@ -24,17 +27,22 @@ export default function App({ Component, pageProps }: AppProps) {
>
<div className="h-screen font-inter">
<Suspense fallback={<div>Loading...</div>}>
<div className="flex flex-col h-full gap-2 md:flex-row">
<Header
setTheme={setTheme}
theme={theme}
/>
<div className="flex flex-col flex-1 max-h-full">
{/* BREADCRUMB */}
<Component {...pageProps} />
<Footer />
<QueryClientProvider client={queryClient}>
<div className="flex flex-col h-full gap-2 md:flex-row">
<Header
setTheme={setTheme}
theme={theme}
/>
<div className="flex flex-col flex-1 max-h-full">
{/* BREADCRUMB */}
<Component {...pageProps} />
<Footer />
</div>
</div>
</div>
{process.env.NODE_ENV === "development" ? (
<ReactQueryDevtools />
) : null}
</QueryClientProvider>
</Suspense>
</div>

Expand Down
36 changes: 30 additions & 6 deletions apps/client/src/pages/dynamic/apo.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { SubmitHandler, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { GetServerSideProps } from "next";
import { useRouter } from "next/router";

import { Button } from "@app/components/Button";
import { Input } from "@app/components/Input";
import { PageLayout } from "@app/components/Layout/Page";
import { Select } from "@app/components/Select";
import { Switch } from "@app/components/Switch";
import { api } from "@app/lib/api";
import { getRunningDynamic } from "@app/queries/useRunningDynamic";
import {
APOFormSchema,
APOFormSchemaType
Expand All @@ -30,6 +33,7 @@ export default function APODynamic() {
double: false
}
});
const router = useRouter();

const handleSubmitDynamic: SubmitHandler<APOFormSchemaType> = async (
data
Expand All @@ -45,8 +49,7 @@ export default function APODynamic() {
formData.append("neutralize", data.neutralize ? "True" : "False");
formData.append("double", data.double ? "True" : "False");
formData.append("ignore", data.ignore ? "True" : "False");
formData.append("user_id", "IvoVieira");
formData.append("dynamic_id", "PrimeiraAPO");
formData.append("username", "IvoVieira1");

await api
.post("/apo", formData, {
Expand All @@ -55,21 +58,20 @@ export default function APODynamic() {
}
})
.then(async ({ data }) => {
console.log(data);
if (data.status === "generated") {
await api
.post(
"/run",
{
folder_run: data.folder
folder: data.folder
},
{
headers: {
"Content-Type": "multipart/form-data"
}
}
)
.then(() => alert("running"))
.then(() => router.push("/dynamic/running"))
.catch(() => alert("not running"));
}
})
Expand Down Expand Up @@ -158,8 +160,30 @@ export default function APODynamic() {
/>
</div>

<Button type="submit">Enviar</Button>
<Button
className="mt-4"
type="submit"
>
Enviar
</Button>
</form>
</PageLayout>
);
}

export const getServerSideProps: GetServerSideProps = async () => {
const data = await getRunningDynamic("IvoVieira1");

if (data?.status === "running") {
return {
redirect: {
destination: "/dynamic/running"
},
props: {}
};
}

return {
props: {}
};
};
103 changes: 103 additions & 0 deletions apps/client/src/pages/dynamic/running.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { ArrowRight } from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/router";

import { Button } from "@app/components/Button";
import { PageLayout } from "@app/components/Layout/Page";
import { useRunningDynamic } from "@app/queries/useRunningDynamic";

export default function Running() {
const { data, isRefetching } = useRunningDynamic("IvoVieira1");
const router = useRouter();

if (data?.status === "not-running") {
return (
<PageLayout title="features:dynamic.running.title">
<h1 className="text-primary-950 uppercase text-center font-bold text-2xl">
features:dynamic.running.not-running.title
</h1>
<p className="text-center">
features:dynamic.running.not-running.description
</p>

<div className="flex gap-x-2 flex-wrap mt-5 mx-auto">
<Link href="/dynamic/apo">
<Button RightIcon={ArrowRight}>features:dynamic.types.apo</Button>
</Link>
<Link href="/dynamic/acpype">
<Button RightIcon={ArrowRight}>
features:dynamic.types.acpype
</Button>
</Link>
</div>
</PageLayout>
);
}

return (
<PageLayout title="features:dynamic.running.title">
<h5>feature:dynamic.running.description</h5>
<div className="flex gap-x-2">
<p className="font-medium">features:dynamic.running.type:</p>
<p className="font-bold text-primary-950">{data?.data?.type}</p>
</div>
<div className="flex gap-x-2">
<p className="font-medium">features:dynamic.running.molecule:</p>
<p className="font-bold text-primary-950">{data?.data?.molecule}</p>
</div>
<div className="flex gap-x-2">
<p className="font-medium">features:dynamic.running.createdAt:</p>
<p className="font-bold text-primary-950">
{Intl.DateTimeFormat(router.locale, {
day: "2-digit",
month: "long",
year: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit"
}).format(new Date(data?.data?.timestamp ?? Date.now()))}
</p>
</div>
{data?.step}
<div className="flex gap-x-2 mt-5">
<h4 className="text-primary-950 uppercase font-bold">
features:dynamic.running.logs.title
</h4>
{isRefetching ? (
<div
className="my-auto"
role="status"
>
<svg
aria-hidden="true"
className="w-5 h-5 text-primary-100 animate-spin fill-primary-950"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
) : null}
</div>
<div className="border border-primary-500/60 bg-primary-400/25 pt-2 p-4 rounded-md h-full w-full overflow-y-auto">
{data?.log?.reverse()?.map((logLine) => (
<p
className="text-zinc-700 font-mono"
key={logLine}
>
{logLine}
</p>
))}
</div>
</PageLayout>
);
}
43 changes: 43 additions & 0 deletions apps/client/src/queries/useRunningDynamic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
useQuery,
UseQueryOptions,
UseQueryResult
} from "@tanstack/react-query";

import { api } from "@app/lib/api";

interface GetRunningDynamicResult {
data?: {
timestamp: string;
molecule: string;
type: "APO" | "ACPYPE";
};
step?: string;
log?: string[];
status: "running" | "not-running";
}

export async function getRunningDynamic(
username: string
): Promise<GetRunningDynamicResult> {
const { data } = await api.get<GetRunningDynamicResult>("/run", {
params: {
username
}
});

return data;
}

export function useRunningDynamic(
username: string,
options?: UseQueryOptions<GetRunningDynamicResult, unknown>
): UseQueryResult<GetRunningDynamicResult, unknown> {
return useQuery({
queryKey: ["RunningDynamic", username],
queryFn: () => getRunningDynamic(username),
staleTime: 1000 * 5,
refetchInterval: 1000 * 5,
...options
});
}

0 comments on commit be9cb52

Please sign in to comment.