Skip to content

Commit ba0a909

Browse files
authored
Merge pull request #861 from subquery/fix/query-rewards
feat: set as recommend
2 parents 5404fe0 + c14c0fe commit ba0a909

6 files changed

Lines changed: 53 additions & 15 deletions

File tree

src/components/ProjectDeployments/ProjectDeployments.tsx

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ import * as React from 'react';
55
import { useTranslation } from 'react-i18next';
66
import Copy from '@components/Copy';
77
import Expand from '@components/Expand/Expand';
8+
import { useWeb3 } from '@containers';
89
import { useCreateDeployment } from '@hooks';
9-
import { Markdown, Modal, openNotification, TableTitle, Typography } from '@subql/components';
10-
import { parseError } from '@utils';
10+
import { useWaitTransactionhandled } from '@hooks/useWaitTransactionHandled';
11+
import { Markdown, Modal, openNotification, Spinner, TableTitle, Typography } from '@subql/components';
12+
import { cidToBytes32, parseError } from '@utils';
1113
import { useUpdate } from 'ahooks';
12-
import { Form, Radio, Table } from 'antd';
14+
import { Checkbox, Form, Radio, Table } from 'antd';
1315
import { useForm } from 'antd/es/form/Form';
1416
import dayjs from 'dayjs';
1517

18+
import { useWeb3Store } from 'src/stores';
19+
1620
import { NewDeployment } from '../../models';
1721
import styles from './ProjectDeployments.module.less';
1822

@@ -27,7 +31,10 @@ type Props = {
2731

2832
const ProjectDeployments: React.FC<Props> = ({ deployments, projectId, currentDeploymentCid, onRefresh }) => {
2933
const { t } = useTranslation();
34+
const { contracts } = useWeb3Store();
3035
const updateDeployment = useCreateDeployment(projectId);
36+
const [loading, setLoading] = React.useState<boolean>(false);
37+
const waitTransactionHandled = useWaitTransactionhandled();
3138
const [deploymentModal, setDeploymentModal] = React.useState<boolean>(false);
3239
const [form] = useForm();
3340
const [currentDeployment, setCurrentDeployment] = React.useState<Deployment>();
@@ -80,7 +87,6 @@ const ProjectDeployments: React.FC<Props> = ({ deployments, projectId, currentDe
8087
rules={[{ required: true }]}
8188
required
8289
help={ruleTips}
83-
validateStatus="error"
8490
>
8591
<div className={styles.markdownWrapper}>
8692
<Markdown
@@ -116,11 +122,36 @@ const ProjectDeployments: React.FC<Props> = ({ deployments, projectId, currentDe
116122
dataIndex: 'deploymentId',
117123
key: 'deploymentId',
118124
title: <TableTitle>RECOMMENDED</TableTitle>,
119-
render: (val) => (
120-
<p className={styles.value}>
121-
{currentDeploymentCid === val ? <Radio checked={currentDeploymentCid === val}>RECOMMENDED</Radio> : ''}
122-
</p>
123-
),
125+
render: (val) =>
126+
loading ? (
127+
<Spinner size={10}></Spinner>
128+
) : (
129+
<p className={styles.value}>
130+
<Radio
131+
checked={currentDeploymentCid === val}
132+
onClick={async () => {
133+
if (currentDeploymentCid !== val) {
134+
try {
135+
const res = await contracts?.projectRegistry.setProjectLatestDeployment(
136+
projectId,
137+
cidToBytes32(val),
138+
);
139+
setLoading(true);
140+
141+
const receipt = await res?.wait(10);
142+
await waitTransactionHandled(receipt?.blockNumber);
143+
144+
await onRefresh();
145+
} finally {
146+
setLoading(false);
147+
}
148+
}
149+
}}
150+
>
151+
RECOMMENDED
152+
</Radio>
153+
</p>
154+
),
124155
},
125156
{
126157
dataIndex: 'deploymentId',
@@ -162,6 +193,7 @@ const ProjectDeployments: React.FC<Props> = ({ deployments, projectId, currentDe
162193
setCurrentDeployment(record);
163194
setDeploymentModal(true);
164195
form.setFieldValue('description', record.description);
196+
form.setFieldValue('recommended', currentDeploymentCid === record.deploymentId);
165197
}}
166198
>
167199
Edit

src/containers/ProjectMetadata.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ function useProjectMetadataImpl() {
3434
return result.cid.toV0().toString();
3535
};
3636

37-
const uploadVersionMetadata = async (version: Omit<NewDeployment, 'deploymentId'>): Promise<string> => {
37+
const uploadVersionMetadata = async (version: NewDeployment & { recommended: boolean }): Promise<string> => {
3838
const result = await ipfs.add(JSON.stringify(version), { pin: true });
3939

4040
return result.cid.toV0().toString();
4141
};
4242

43-
const getVersionMetadata = (cid: string) => fetchIpfsMetadata<Omit<NewDeployment, 'deploymentId'>>(catSingle, cid);
43+
const getVersionMetadata = (cid: string) =>
44+
fetchIpfsMetadata<NewDeployment & { recommended: boolean }>(catSingle, cid);
4445

4546
return {
4647
getMetadataForProject,

src/containers/ProjectRegistry.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ function useProjectRegistryImpl(logger: Logger) {
134134
getQuery,
135135
updateQueryMetadata,
136136
updateDeployment,
137+
clearCache: () => {
138+
projectCache.current = {};
139+
},
137140
};
138141
}
139142

src/hooks/useCreateDeployment.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function useCreateDeployment(
1717
const versionCid = await uploadVersionMetadata({
1818
version: deploymentDetails.version,
1919
description: deploymentDetails.description,
20-
});
20+
} as NewDeployment & { recommended: boolean });
2121

2222
const tx = await projectRegistry.updateDeployment(
2323
projectId,

src/hooks/useProject.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,23 @@ import { AsyncData } from '../utils';
1111
import { useAsyncMemo } from '.';
1212

1313
export function useProject(id: string): AsyncData<ProjectDetails | undefined> {
14-
const { getQuery } = useProjectRegistry();
14+
const { getQuery, clearCache } = useProjectRegistry();
1515
const { catSingle } = useIPFS();
1616
const { getMetadataFromCid } = useProjectMetadata();
1717

1818
return useAsyncMemo(async () => {
1919
if (!id) {
2020
return undefined;
2121
}
22+
clearCache();
2223

2324
const query = await getQuery(id);
24-
console.warn(query);
2525
if (!query) {
2626
return undefined;
2727
}
2828

2929
const metadata = await getMetadataFromCid(query.metadata);
30+
3031
return {
3132
id,
3233
owner: query.owner,

src/pages/projects/Project/Deployments.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,12 @@ const DeploymentsTab = forwardRef<DeploymendRef, Props>(({ projectId, currentDep
8888
return (
8989
<ProjectDeployments
9090
currentDeploymentCid={currentDeployment?.deployment}
91+
// @ts-ignore
9192
deployments={sortedDeployments}
9293
projectId={projectId}
9394
onRefresh={async () => {
94-
await asyncDeployments.refetch();
9595
await onRefresh?.();
96+
await asyncDeployments.refetch();
9697
}}
9798
/>
9899
);

0 commit comments

Comments
 (0)