Skip to content

Commit

Permalink
Merge pull request #1094 from alleslabs/fix/add-route-zod-validation
Browse files Browse the repository at this point in the history
fix(components): add zod validation to pages
  • Loading branch information
songwongtp authored Sep 16, 2024
2 parents 7223a3a + 17c9f9a commit 12f097f
Show file tree
Hide file tree
Showing 46 changed files with 602 additions and 410 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Improvements

- [#1094](https://github.com/alleslabs/celatone-frontend/pull/1094) Replace getFirstQueryParam with zod validation

### Bug fixes

- [#1150](https://github.com/alleslabs/celatone-frontend/pull/1150) Fix camel case on message
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/button/InstantiateButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export const InstantiateButton = ({
const { address, isWalletConnected } = useCurrentChain();
const navigate = useInternalNavigate();
const goToInstantiate = () =>
navigate({ pathname: "/instantiate", query: { "code-id": codeId } });
navigate({ pathname: "/instantiate", query: { codeId } });

const isAllowed = resolvePermission(
address,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const SchemaInputNotExist = ({
if (verifiedSchema)
return (
<Text color="text.main" fontWeight={700} variant="body1">
`Verified JSON Schema doesn’t have ${prettyType}Msg`
Verified JSON Schema doesn’t have {prettyType}Msg
</Text>
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const WasmVerifySubmitForm = ({
}),
commit: z
.string()
.min(1, { message: "Commit hash is requried" })
.min(1, { message: "Commit hash is required" })
.regex(
/^[0-9a-fA-F]+$/,
"Only hexadecimal digits are allowed, such as 0-9 and A-F"
Expand Down
16 changes: 8 additions & 8 deletions src/lib/components/select-code/CodeSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,33 @@ import type { Code } from "lib/services/types";
import { useDerivedWasmVerifyInfo } from "lib/services/verification/wasm";
import { useCodeLcd } from "lib/services/wasm/code";
import { AccessConfigPermission } from "lib/types";
import { getWasmVerifyStatus, isId } from "lib/utils";
import { getWasmVerifyStatus } from "lib/utils";

import { CodeSelectDrawerButton } from "./CodeSelectDrawerButton";

interface CodeSelectProps extends Omit<FlexProps, "onSelect"> {
onCodeSelect: (code: string) => void;
codeId: number;
onCodeSelect: (code: number) => void;
setCodeHash: (data: Code) => void;
codeId: string;
status: FormStatus;
}

export const CodeSelect = ({
codeId,
onCodeSelect,
setCodeHash,
codeId,
status,
...componentProps
}: CodeSelectProps) => {
const { getCodeLocalInfo } = useCodeStore();
const name = getCodeLocalInfo(Number(codeId))?.name;
const { data } = useCodeLcd(Number(codeId), {
enabled: isId(codeId),
const name = getCodeLocalInfo(codeId)?.name;
const { data } = useCodeLcd(codeId, {
enabled: !!codeId,
onSuccess: setCodeHash,
});

const { data: wasmDerivedVerifyInfos } = useDerivedWasmVerifyInfo(
Number(codeId),
codeId,
data?.hash
);

Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/select-code/CodeSelectDrawerButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ interface CodeFilterState {
}

interface CodeSelectDrawerButtonProps {
onCodeSelect: (code: string) => void;
onCodeSelect: (code: number) => void;
buttonText: string;
}

Expand Down Expand Up @@ -65,7 +65,7 @@ export const CodeSelectDrawerButton = ({
} = useMyCodesData(keyword, permissionValue);

const handleSelect = (codeId: number) => {
onCodeSelect(codeId.toString());
onCodeSelect(codeId);
onClose();
};

Expand Down
6 changes: 3 additions & 3 deletions src/lib/components/select-code/CodeSelectSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import type { Option } from "lib/types";
import { CodeSelect } from "./CodeSelect";

interface CodeSelectSectionProps<T extends FieldValues> {
codeId: string;
codeId: number;
name: FieldPath<T>;
control: Control<T>;
error: Option<string>;
onCodeSelect: (codeId: string) => void;
onCodeSelect: (codeId: number) => void;
setCodeHash: (data: Code) => void;
status: FormStatus;
}
Expand Down Expand Up @@ -61,9 +61,9 @@ export const CodeSelectSection = <T extends FieldValues>({
<CodeSelect
mt={4}
mb={8}
codeId={codeId}
onCodeSelect={onCodeSelect}
setCodeHash={setCodeHash}
codeId={codeId}
status={status}
/>
) : (
Expand Down
2 changes: 1 addition & 1 deletion src/lib/components/tx/modal/ButtonSection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const ButtonSection = ({
const codeId = receipts.find((r) => r.title === "Code ID")?.value;
navigate({
pathname: "/migrate",
query: { contract: router.query.contract, "code-id": codeId },
query: { contract: router.query.contract, codeId },
});
onClose?.();
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
Flex,
Text,
} from "@chakra-ui/react";
import { useRouter } from "next/router";
import { useCallback, useMemo, useState } from "react";

import { AmpEvent, track, trackUseExpand } from "lib/amplitude";
Expand All @@ -16,40 +15,34 @@ import InputWithIcon from "lib/components/InputWithIcon";
import { ResourceCard } from "lib/components/resource";
import type {
BechAddr,
Option,
ResourceGroup,
ResourceGroupByAccount,
} from "lib/types";
import { getFirstQueryParam, truncate } from "lib/utils";
import { truncate } from "lib/utils";

interface ResourceSectionBodyProps {
address: BechAddr;
resourcesByOwner: ResourceGroupByAccount[];
selectedAccount: Option<string>;
selectedGroupName: Option<string>;
}

export const ResourceLeftPanel = ({
address,
resourcesByOwner,
selectedAccount,
selectedGroupName,
}: ResourceSectionBodyProps) => {
const router = useRouter();
const navigate = useInternalNavigate();
const [keyword, setKeyword] = useState("");

const selectedAccountParam = getFirstQueryParam(
router.query.account,
resourcesByOwner?.[0]?.owner
);
const selectedNameParam = getFirstQueryParam(
router.query.selected,
resourcesByOwner?.[0]?.resources[0]?.group
);

const handleSelectResource = useCallback(
(
account: ResourceGroupByAccount["owner"],
resource: ResourceGroup["group"]
) => {
if (account === selectedAccountParam && resource === selectedNameParam)
return;
if (account === selectedAccount && resource === selectedGroupName) return;
navigate({
pathname: `/accounts/[accountAddress]/[tab]`,
query: {
Expand All @@ -64,7 +57,7 @@ export const ResourceLeftPanel = ({
},
});
},
[selectedNameParam, selectedAccountParam, address, navigate]
[selectedGroupName, selectedAccount, address, navigate]
);

const filteredResourcesByOwner = useMemo(() => {
Expand All @@ -81,11 +74,11 @@ export const ResourceLeftPanel = ({
}, [keyword, resourcesByOwner]);

const selectedIndex = filteredResourcesByOwner.findIndex(
(item) => item.owner === selectedAccountParam
(item) => item.owner === selectedAccount
);
const selectedGroup = filteredResourcesByOwner[selectedIndex];
const selectedResources = selectedGroup?.resources.find(
(item) => item.group === selectedNameParam
(item) => item.group === selectedGroupName
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ interface ResourceSectionProps {
totalCount: Option<number>;
resourcesByOwner: Option<ResourceGroupByAccount[]>;
isLoading: boolean;
selectedAccountParam: Option<string>;
selectedGroupNameParam: Option<string>;
}

export const ResourceSection = ({
address,
totalCount,
resourcesByOwner,
isLoading,
selectedAccountParam,
selectedGroupNameParam,
}: ResourceSectionProps) => (
<Flex direction="column" mt={8}>
<TableTitle
Expand All @@ -28,6 +32,8 @@ export const ResourceSection = ({
address={address}
resourcesByOwner={resourcesByOwner}
isLoading={isLoading}
selectedAccountParam={selectedAccountParam}
selectedGroupNameParam={selectedGroupNameParam}
/>
</Flex>
);
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
GridItem,
Heading,
} from "@chakra-ui/react";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";

import { trackUseExpandAll } from "lib/amplitude";
Expand All @@ -18,38 +17,35 @@ import { Loading } from "lib/components/Loading";
import { ResourceDetailCard } from "lib/components/resource";
import { EmptyState, ErrorFetching } from "lib/components/state";
import type { BechAddr, Option, ResourceGroupByAccount } from "lib/types";
import { getFirstQueryParam } from "lib/utils";

import { ResourceLeftPanel } from "./ResourceLeftPanel";

interface ResourceSectionBodyProps {
address: BechAddr;
resourcesByOwner: Option<ResourceGroupByAccount[]>;
isLoading: boolean;
selectedAccountParam: Option<string>;
selectedGroupNameParam: Option<string>;
}

export const ResourceSectionBody = ({
address,
resourcesByOwner,
isLoading,
selectedAccountParam,
selectedGroupNameParam,
}: ResourceSectionBodyProps) => {
const router = useRouter();
const isMobile = useMobile();

const [expandedIndexes, setExpandedIndexes] = useState<number[]>([]);

const selectedAccountParam = getFirstQueryParam(
router.query.account,
resourcesByOwner?.[0]?.owner
);
const selectedNameParam = getFirstQueryParam(
router.query.selected,
resourcesByOwner?.[0]?.resources[0]?.group
);
const selectedAccount = selectedAccountParam ?? resourcesByOwner?.[0]?.owner;
const selectedGroupName =
selectedGroupNameParam ?? resourcesByOwner?.[0]?.resources[0]?.group;

const selectedResource = resourcesByOwner
?.find((resource) => resource.owner === selectedAccountParam)
?.resources?.find((resource) => resource.group === selectedNameParam);
?.find((resource) => resource.owner === selectedAccount)
?.resources?.find((resource) => resource.group === selectedGroupName);

useEffect(() => {
setExpandedIndexes(selectedResource?.items.length === 1 ? [0] : []);
Expand All @@ -65,6 +61,8 @@ export const ResourceSectionBody = ({
<ResourceLeftPanel
address={address}
resourcesByOwner={resourcesByOwner}
selectedAccount={selectedAccount}
selectedGroupName={selectedGroupName}
/>
</GridItem>
{selectedResource && (
Expand Down
10 changes: 9 additions & 1 deletion src/lib/pages/account-details/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ import { TabIndex, zAccountDetailsQueryParams } from "./types";

const tableHeaderId = "accountDetailsTab";

export interface AccountDetailsBodyProps {
interface AccountDetailsBodyProps {
accountAddressParam: Addr;
tabParam: TabIndex;
resourceSelectedAccountParam: Option<string>;
resourceSelectedGroupNameParam: Option<string>;
}

const getAddressOnPath = (hexAddress: HexAddr, accountAddress: BechAddr) =>
Expand All @@ -75,6 +77,8 @@ const InvalidAccount = () => <InvalidState title="Account does not exist" />;
const AccountDetailsBody = ({
accountAddressParam,
tabParam,
resourceSelectedAccountParam,
resourceSelectedGroupNameParam,
// eslint-disable-next-line sonarjs/cognitive-complexity
}: AccountDetailsBodyProps) => {
// ------------------------------------------//
Expand Down Expand Up @@ -556,6 +560,8 @@ const AccountDetailsBody = ({
totalCount={resourcesData?.totalCount}
resourcesByOwner={resourcesData?.groupedByOwner}
isLoading={isResourceLoading}
selectedAccountParam={resourceSelectedAccountParam}
selectedGroupNameParam={resourceSelectedGroupNameParam}
/>
<UserDocsLink
title="What is resources?"
Expand Down Expand Up @@ -616,6 +622,8 @@ const AccountDetails = () => {
<AccountDetailsBody
accountAddressParam={validated.data.accountAddress}
tabParam={validated.data.tab}
resourceSelectedAccountParam={validated.data.account}
resourceSelectedGroupNameParam={validated.data.selected}
/>
)}
</PageContainer>
Expand Down
3 changes: 3 additions & 0 deletions src/lib/pages/account-details/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ export const zAccountDetailsQueryParams = z.object({
.optional()
.transform(() => TabIndex.Overview),
]),
// for resource tab
account: z.string().optional(),
selected: z.string().optional(),
});
Loading

0 comments on commit 12f097f

Please sign in to comment.