Commit e6cd85d8 authored by tom's avatar tom

change types according new model

parent 0064819c
export const apiKey = [
{
token: '6fd12fe0-841c-4abf-ac2a-8c1b08dadf8e',
name: 'zapper.fi',
},
{
token: '057085a1-d2eb-4d8d-8b89-1dd9fba32071',
name: 'TenderlyBlaBlaName',
},
{
token: '057085a1-d2eb-4d8d-8b89-1dd9fba32071',
name: 'Application name',
},
];
export type TApiKey = Array<TApiKeyItem>
export type TApiKeyItem = {
token: string;
name: string;
}
...@@ -10,8 +10,8 @@ export interface AddressTag { ...@@ -10,8 +10,8 @@ export interface AddressTag {
export type AddressTags = Array<AddressTag> export type AddressTags = Array<AddressTag>
export interface ApiKey { export interface ApiKey {
apiKey: string; api_key: string;
apiKeyName: string; name: string;
} }
export type ApiKeys = Array<ApiKey> export type ApiKeys = Array<ApiKey>
......
...@@ -9,10 +9,10 @@ import React, { useCallback, useEffect } from 'react'; ...@@ -9,10 +9,10 @@ import React, { useCallback, useEffect } from 'react';
import type { SubmitHandler, ControllerRenderProps } from 'react-hook-form'; import type { SubmitHandler, ControllerRenderProps } from 'react-hook-form';
import { useForm, Controller } from 'react-hook-form'; import { useForm, Controller } from 'react-hook-form';
import type { TApiKeyItem } from 'data/apiKey'; import type { ApiKey } from 'pages/api/types/account';
type Props = { type Props = {
data?: TApiKeyItem; data?: ApiKey;
} }
type Inputs = { type Inputs = {
...@@ -27,7 +27,7 @@ const ApiKeyForm: React.FC<Props> = ({ data }) => { ...@@ -27,7 +27,7 @@ const ApiKeyForm: React.FC<Props> = ({ data }) => {
const { control, handleSubmit, formState: { errors }, setValue } = useForm<Inputs>(); const { control, handleSubmit, formState: { errors }, setValue } = useForm<Inputs>();
useEffect(() => { useEffect(() => {
setValue('token', data?.token || ''); setValue('token', data?.api_key || '');
setValue('name', data?.name || ''); setValue('name', data?.name || '');
}, [ setValue, data ]); }, [ setValue, data ]);
......
import React, { useCallback } from 'react'; import React, { useCallback } from 'react';
import type { TApiKeyItem } from 'data/apiKey'; import type { ApiKey } from 'pages/api/types/account';
import FormModal from 'ui/shared/FormModal'; import FormModal from 'ui/shared/FormModal';
import ApiKeyForm from './ApiKeyForm'; import ApiKeyForm from './ApiKeyForm';
...@@ -8,7 +9,7 @@ import ApiKeyForm from './ApiKeyForm'; ...@@ -8,7 +9,7 @@ import ApiKeyForm from './ApiKeyForm';
type Props = { type Props = {
isOpen: boolean; isOpen: boolean;
onClose: () => void; onClose: () => void;
data?: TApiKeyItem; data?: ApiKey;
} }
const AddressModal: React.FC<Props> = ({ isOpen, onClose, data }) => { const AddressModal: React.FC<Props> = ({ isOpen, onClose, data }) => {
...@@ -19,7 +20,7 @@ const AddressModal: React.FC<Props> = ({ isOpen, onClose, data }) => { ...@@ -19,7 +20,7 @@ const AddressModal: React.FC<Props> = ({ isOpen, onClose, data }) => {
return <ApiKeyForm data={ data }/>; return <ApiKeyForm data={ data }/>;
}, [ data ]); }, [ data ]);
return ( return (
<FormModal<TApiKeyItem> <FormModal<ApiKey>
isOpen={ isOpen } isOpen={ isOpen }
onClose={ onClose } onClose={ onClose }
title={ title } title={ title }
......
...@@ -8,14 +8,14 @@ import { ...@@ -8,14 +8,14 @@ import {
} from '@chakra-ui/react'; } from '@chakra-ui/react';
import React from 'react'; import React from 'react';
import type { TApiKey, TApiKeyItem } from 'data/apiKey'; import type { ApiKeys, ApiKey } from 'pages/api/types/account';
import ApiKeyTableItem from './ApiKeyTableItem'; import ApiKeyTableItem from './ApiKeyTableItem';
interface Props { interface Props {
data: TApiKey; data: ApiKeys;
onEditClick: (data: TApiKeyItem) => void; onEditClick: (item: ApiKey) => void;
onDeleteClick: (data: TApiKeyItem) => void; onDeleteClick: (item: ApiKey) => void;
limit: number; limit: number;
} }
...@@ -33,7 +33,7 @@ const ApiKeyTable = ({ data, onDeleteClick, onEditClick, limit }: Props) => { ...@@ -33,7 +33,7 @@ const ApiKeyTable = ({ data, onDeleteClick, onEditClick, limit }: Props) => {
{ data.map((item) => ( { data.map((item) => (
<ApiKeyTableItem <ApiKeyTableItem
item={ item } item={ item }
key={ item.token } key={ item.api_key }
onDeleteClick={ onDeleteClick } onDeleteClick={ onDeleteClick }
onEditClick={ onEditClick } onEditClick={ onEditClick }
/> />
......
...@@ -6,15 +6,16 @@ import { ...@@ -6,15 +6,16 @@ import {
} from '@chakra-ui/react'; } from '@chakra-ui/react';
import React, { useCallback } from 'react'; import React, { useCallback } from 'react';
import type { TApiKeyItem } from 'data/apiKey'; import type { ApiKey } from 'pages/api/types/account';
import CopyToClipboard from 'ui/shared/CopyToClipboard'; import CopyToClipboard from 'ui/shared/CopyToClipboard';
import DeleteButton from 'ui/shared/DeleteButton'; import DeleteButton from 'ui/shared/DeleteButton';
import EditButton from 'ui/shared/EditButton'; import EditButton from 'ui/shared/EditButton';
interface Props { interface Props {
item: TApiKeyItem; item: ApiKey;
onEditClick: (data: TApiKeyItem) => void; onEditClick: (item: ApiKey) => void;
onDeleteClick: (data: TApiKeyItem) => void; onDeleteClick: (item: ApiKey) => void;
} }
const WatchlistTableItem = ({ item, onEditClick, onDeleteClick }: Props) => { const WatchlistTableItem = ({ item, onEditClick, onDeleteClick }: Props) => {
...@@ -28,11 +29,11 @@ const WatchlistTableItem = ({ item, onEditClick, onDeleteClick }: Props) => { ...@@ -28,11 +29,11 @@ const WatchlistTableItem = ({ item, onEditClick, onDeleteClick }: Props) => {
}, [ item, onDeleteClick ]); }, [ item, onDeleteClick ]);
return ( return (
<Tr alignItems="top" key={ item.token }> <Tr alignItems="top" key={ item.api_key }>
<Td> <Td>
<HStack> <HStack>
<Text fontSize="md" fontWeight={ 600 }>{ item.token }</Text> <Text fontSize="md" fontWeight={ 600 }>{ item.api_key }</Text>
<CopyToClipboard text={ item.token }/> <CopyToClipboard text={ item.api_key }/>
</HStack> </HStack>
<Text fontSize="sm" marginTop={ 0.5 } variant="secondary">{ item.name }</Text> <Text fontSize="sm" marginTop={ 0.5 } variant="secondary">{ item.name }</Text>
</Td> </Td>
......
import { Box, Button, HStack, Link, Text, useDisclosure } from '@chakra-ui/react'; import { Box, Button, HStack, Link, Text, Spinner, useDisclosure } from '@chakra-ui/react';
import { useQuery } from '@tanstack/react-query'; import { useQuery } from '@tanstack/react-query';
import React, { useCallback, useState } from 'react'; import React, { useCallback, useState } from 'react';
import type { TApiKeyItem } from 'data/apiKey'; import type { ApiKey, ApiKeys } from 'pages/api/types/account';
import { apiKey } from 'data/apiKey';
import { space } from 'lib/html-entities'; import { space } from 'lib/html-entities';
import ApiKeyModal from 'ui/apiKey/ApiKeyModal/ApiKeyModal'; import ApiKeyModal from 'ui/apiKey/ApiKeyModal/ApiKeyModal';
import ApiKeyTable from 'ui/apiKey/ApiKeyTable/ApiKeyTable'; import ApiKeyTable from 'ui/apiKey/ApiKeyTable/ApiKeyTable';
...@@ -13,14 +13,14 @@ import Page from 'ui/shared/Page/Page'; ...@@ -13,14 +13,14 @@ import Page from 'ui/shared/Page/Page';
const DATA_LIMIT = 3; const DATA_LIMIT = 3;
const ApiKeys: React.FC = () => { const ApiKeysPage: React.FC = () => {
const apiKeyModalProps = useDisclosure(); const apiKeyModalProps = useDisclosure();
const deleteModalProps = useDisclosure(); const deleteModalProps = useDisclosure();
const [ apiKeyModalData, setApiKeyModalData ] = useState<TApiKeyItem>(); const [ apiKeyModalData, setApiKeyModalData ] = useState<ApiKey>();
const [ deleteModalData, setDeleteModalData ] = useState<string>(); const [ deleteModalData, setDeleteModalData ] = useState<string>();
useQuery([ 'api-keys' ], async() => { const { data, isLoading, isError } = useQuery<unknown, unknown, ApiKeys>([ 'api-keys' ], async() => {
const response = await fetch('/api/account/api-keys'); const response = await fetch('/api/account/api-keys');
if (!response.ok) { if (!response.ok) {
throw new Error('Network response was not ok'); throw new Error('Network response was not ok');
...@@ -28,7 +28,7 @@ const ApiKeys: React.FC = () => { ...@@ -28,7 +28,7 @@ const ApiKeys: React.FC = () => {
return response.json(); return response.json();
}); });
const onEditClick = useCallback((data: TApiKeyItem) => { const onEditClick = useCallback((data: ApiKey) => {
setApiKeyModalData(data); setApiKeyModalData(data);
apiKeyModalProps.onOpen(); apiKeyModalProps.onOpen();
}, [ apiKeyModalProps ]); }, [ apiKeyModalProps ]);
...@@ -38,7 +38,7 @@ const ApiKeys: React.FC = () => { ...@@ -38,7 +38,7 @@ const ApiKeys: React.FC = () => {
apiKeyModalProps.onClose(); apiKeyModalProps.onClose();
}, [ apiKeyModalProps ]); }, [ apiKeyModalProps ]);
const onDeleteClick = useCallback((data: TApiKeyItem) => { const onDeleteClick = useCallback((data: ApiKey) => {
setDeleteModalData(data.name); setDeleteModalData(data.name);
deleteModalProps.onOpen(); deleteModalProps.onOpen();
}, [ deleteModalProps ]); }, [ deleteModalProps ]);
...@@ -48,19 +48,17 @@ const ApiKeys: React.FC = () => { ...@@ -48,19 +48,17 @@ const ApiKeys: React.FC = () => {
deleteModalProps.onClose(); deleteModalProps.onClose();
}, [ deleteModalProps ]); }, [ deleteModalProps ]);
const canAdd = apiKey.length < DATA_LIMIT; const content = (() => {
if (isLoading || isError) {
return <Spinner/>;
}
return ( const canAdd = data.length < DATA_LIMIT;
<Page> return (
<Box h="100%"> <>
<AccountPageHeader text="API keys"/> { data.length > 0 && (
<Text marginBottom={ 12 }>
Create API keys to use for your RPC and EthRPC API requests. For more information, see { space }
<Link href="#">“How to use a Blockscout API key”</Link>.
</Text>
{ Boolean(apiKey.length) && (
<ApiKeyTable <ApiKeyTable
data={ apiKey } data={ data }
onDeleteClick={ onDeleteClick } onDeleteClick={ onDeleteClick }
onEditClick={ onEditClick } onEditClick={ onEditClick }
limit={ DATA_LIMIT } limit={ DATA_LIMIT }
...@@ -81,6 +79,19 @@ const ApiKeys: React.FC = () => { ...@@ -81,6 +79,19 @@ const ApiKeys: React.FC = () => {
</Text> </Text>
) } ) }
</HStack> </HStack>
</>
);
})();
return (
<Page>
<Box h="100%">
<AccountPageHeader text="API keys"/>
<Text marginBottom={ 12 }>
Create API keys to use for your RPC and EthRPC API requests. For more information, see { space }
<Link href="#">“How to use a Blockscout API key”</Link>.
</Text>
{ content }
</Box> </Box>
<ApiKeyModal { ...apiKeyModalProps } onClose={ onApiKeyModalClose } data={ apiKeyModalData }/> <ApiKeyModal { ...apiKeyModalProps } onClose={ onApiKeyModalClose } data={ apiKeyModalData }/>
<DeleteApiKeyModal { ...deleteModalProps } onClose={ onDeleteModalClose } name={ deleteModalData }/> <DeleteApiKeyModal { ...deleteModalProps } onClose={ onDeleteModalClose } name={ deleteModalData }/>
...@@ -88,4 +99,4 @@ const ApiKeys: React.FC = () => { ...@@ -88,4 +99,4 @@ const ApiKeys: React.FC = () => {
); );
}; };
export default ApiKeys; export default ApiKeysPage;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment