namespace logic
This commit is contained in:
parent
60ba131b81
commit
be1be20864
|
|
@ -2,11 +2,13 @@ import { z } from "zod";
|
||||||
import { chainSchema } from "../schema";
|
import { chainSchema } from "../schema";
|
||||||
import { getAxios } from "@/shared/lib/getAxios";
|
import { getAxios } from "@/shared/lib/getAxios";
|
||||||
|
|
||||||
export async function getChainList() {
|
export async function getChainList(namespaceId: string) {
|
||||||
const axios = getAxios();
|
const axios = getAxios();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await axios.get("/chain/list/");
|
const response = await axios.get("/chain/list/", {
|
||||||
|
params: { namespace_id: namespaceId },
|
||||||
|
});
|
||||||
return z.array(chainSchema).parse(response.data);
|
return z.array(chainSchema).parse(response.data);
|
||||||
} catch {
|
} catch {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import { actionSchema } from "../action/schema";
|
||||||
|
|
||||||
export const chainSchema = z.object({
|
export const chainSchema = z.object({
|
||||||
_id: z.string().uuid().optional(),
|
_id: z.string().uuid().optional(),
|
||||||
|
namespaceId: z.string().uuid(),
|
||||||
name: z.string().nullable(),
|
name: z.string().nullable(),
|
||||||
actions: z.array(actionSchema).optional(),
|
actions: z.array(actionSchema).optional(),
|
||||||
lastModified: z.string().optional(),
|
lastModified: z.string().optional(),
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ import { useNavigate } from "@tanstack/react-router";
|
||||||
import { chainSchema } from "@/entities/chain/schema";
|
import { chainSchema } from "@/entities/chain/schema";
|
||||||
import { upsertChain } from "@/entities/chain/api/upsert";
|
import { upsertChain } from "@/entities/chain/api/upsert";
|
||||||
import { useChainState } from "@/entities/chain/model";
|
import { useChainState } from "@/entities/chain/model";
|
||||||
|
import { useParams } from "@tanstack/react-router";
|
||||||
|
|
||||||
import Dialog from "@mui/material/Dialog";
|
import Dialog from "@mui/material/Dialog";
|
||||||
import DialogActions from "@mui/material/DialogActions";
|
import DialogActions from "@mui/material/DialogActions";
|
||||||
|
|
@ -19,6 +20,7 @@ export default function AddChainButton({ children }: AddChainButtonProps) {
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [isError, setIsError] = useState(false);
|
const [isError, setIsError] = useState(false);
|
||||||
|
const { namespace } = useParams({ from: "/$namespace/$chainId" });
|
||||||
|
|
||||||
const navigate = useNavigate({ from: "/$namespace/$chainId" });
|
const navigate = useNavigate({ from: "/$namespace/$chainId" });
|
||||||
const addChain = useChainState((state) => state.addChain);
|
const addChain = useChainState((state) => state.addChain);
|
||||||
|
|
@ -28,7 +30,10 @@ export default function AddChainButton({ children }: AddChainButtonProps) {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
const formData = new FormData(event.currentTarget);
|
const formData = new FormData(event.currentTarget);
|
||||||
const validatedData = chainSchema.parse(Object.fromEntries(formData));
|
const validatedData = chainSchema.parse({
|
||||||
|
...Object.fromEntries(formData),
|
||||||
|
namespaceId: namespace,
|
||||||
|
});
|
||||||
|
|
||||||
const createdChain = await upsertChain(validatedData);
|
const createdChain = await upsertChain(validatedData);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ function ChainPage() {
|
||||||
export const Route = createFileRoute("/$namespace/$chainId")({
|
export const Route = createFileRoute("/$namespace/$chainId")({
|
||||||
component: ChainPage,
|
component: ChainPage,
|
||||||
loader: async ({ params }) => {
|
loader: async ({ params }) => {
|
||||||
await requireChains();
|
await requireChains(params.namespace);
|
||||||
const chainState = useChainState.getState();
|
const chainState = useChainState.getState();
|
||||||
const chains = chainState.chains;
|
const chains = chainState.chains;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,7 @@ function IndexPage() {
|
||||||
export const Route = createFileRoute("/$namespace/")({
|
export const Route = createFileRoute("/$namespace/")({
|
||||||
component: IndexPage,
|
component: IndexPage,
|
||||||
loader: async ({ params }) => {
|
loader: async ({ params }) => {
|
||||||
await requireChains();
|
await requireChains(params.namespace);
|
||||||
const chains = useChainState.getState().chains;
|
const chains = useChainState.getState().chains;
|
||||||
|
|
||||||
if (typeof chains === "undefined") throw redirect({ to: "/" });
|
if (typeof chains === "undefined") throw redirect({ to: "/" });
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user