Skip to content

Commit

Permalink
feat: Auto go to detail page after delist
Browse files Browse the repository at this point in the history
  • Loading branch information
ybgbob committed Jun 28, 2024
1 parent e6f2310 commit 4a95316
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 24 deletions.
2 changes: 2 additions & 0 deletions src/atoms/delistAtom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ interface IDelistAtom {
starting: boolean;
params: {
groupId: bigint;
objectId: bigint;
};
}

Expand All @@ -13,5 +14,6 @@ export const delistAtom = atomWithImmer<IDelistAtom>({
starting: false,
params: {
groupId: BigInt(0),
objectId: BigInt(0),
},
});
22 changes: 21 additions & 1 deletion src/components/modal/DelistModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ import { useDelist } from '../../hooks/seller/useDelist';
import { Loader } from '../Loader';
import { DelistIcon } from '../svgIcon/DelistIcon';
import { BlackSolidButton } from '../ui/buttons/BlackButton';
import { client } from '../../utils/gfSDK';
import { useNavigate } from 'react-router-dom';

export const DelistModal = () => {
const navigator = useNavigate();
const [delistInfo, setDelistInfo] = useImmerAtom(delistAtom);
const { doDelist } = useDelist();

Expand All @@ -26,6 +29,7 @@ export const DelistModal = () => {
onClose={() => {
setDelistInfo((draft) => {
draft.openDelist = false;
draft.starting = false;
});
}}
>
Expand All @@ -51,7 +55,23 @@ export const DelistModal = () => {
isLoading={delistInfo.starting}
fontWeight={900}
onClick={async () => {
await doDelist(delistInfo.params.groupId);
await doDelist({
groupId: delistInfo.params.groupId,
onSuccess: async (objectId) => {
const objRes = await client.object.headObjectById(
String(objectId),
);
console.log('objRes', objRes);
const oid = objRes.objectInfo?.id;
if (!objRes.objectInfo?.bucketName) return;
const bucketRes = await client.bucket.headBucket(
objRes.objectInfo.bucketName,
);
console.log('bucketRes', bucketRes);
const bid = bucketRes.bucketInfo?.id;
navigator(`detail?bid=${bid}&oid=${oid}`);
},
});
}}
_disabled={{
bg: '#AEB4BC',
Expand Down
11 changes: 5 additions & 6 deletions src/components/profile/MyCollectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ const MyCollectionList = ({ address }: ICollectionList) => {
const navigator = useNavigate();
const { data: endpoint } = useSelectEndpoint();
const bucketName = getSpaceName(address);
const { confirmDelist } = useDelist({
onSuccess: async () => {
await refetchList();
},
});
const { confirmDelist } = useDelist();

const { address: loginAddress } = useAccount();

Expand Down Expand Up @@ -181,7 +177,10 @@ const MyCollectionList = ({ address }: ICollectionList) => {
item.ObjectInfo.Id.toString(),
);
console.log('groupId', groupId);
confirmDelist(BigInt(groupId));
confirmDelist(
BigInt(groupId),
BigInt(item.ObjectInfo.Id),
);
}}
>
Delist
Expand Down
26 changes: 19 additions & 7 deletions src/hooks/seller/useDelist.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,31 @@ import { Tips } from '../../components/modal/Tips';
import { BSC_CHAIN, NEW_MARKETPLACE_CONTRACT_ADDRESS } from '../../env';

interface Params {
onSuccess?: () => Promise<void>;
onSuccess?: (objectId: bigint) => Promise<void>;
}

export const useDelist = ({ onSuccess }: Params = {}) => {
const [, setDelistInfo] = useImmerAtom(delistAtom);
export const useDelist = () => {
// const { onSuccess } = params;
// console.log('init onSuccess', onSuccess);
const [delistInfo, setDelistInfo] = useImmerAtom(delistAtom);
const { address } = useAccount();
const publicClient = usePublicClient({
chainId: BSC_CHAIN.id,
});
const { data: walletClient } = useWalletClient();

const doDelist = async (groupId: bigint) => {
const doDelist = async ({
groupId,
onSuccess,
}: {
groupId: bigint;
onSuccess?: (objectId: bigint) => Promise<void>;
}) => {
setDelistInfo((draft) => {
draft.starting = true;
});

try {
// await delisting(groupId, true);
const { request } = await publicClient.simulateContract({
account: address,
abi: MarketplaceAbi,
Expand All @@ -44,8 +51,9 @@ export const useDelist = ({ onSuccess }: Params = {}) => {
}

await succDelist();
await onSuccess?.();
await onSuccess?.(delistInfo.params.objectId);
} catch (err) {
console.log('err', err);
} finally {
setDelistInfo((draft) => {
draft.starting = false;
Expand All @@ -54,10 +62,11 @@ export const useDelist = ({ onSuccess }: Params = {}) => {
}
};

const confirmDelist = (groupId: bigint) => {
const confirmDelist = (groupId: bigint, objectId: bigint) => {
setDelistInfo((draft) => {
draft.openDelist = true;
draft.params.groupId = groupId;
draft.params.objectId = objectId;
});
};

Expand All @@ -80,6 +89,9 @@ export const useDelist = ({ onSuccess }: Params = {}) => {
</Stack>
),
buttonText: 'Got it',
buttonClick: async () => {
// ...
},
});
};

Expand Down
56 changes: 46 additions & 10 deletions src/pages/Resource.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { LinkArrowIcon } from '@totejs/icons';
import { Box, Button, Flex, Image, Link, Stack } from '@totejs/uikit';
import { useImmerAtom } from 'jotai-immer';
import { MetaMaskAvatar } from 'react-metamask-avatar';
import { useNavigate, useSearchParams } from 'react-router-dom';
import { useSearchParams } from 'react-router-dom';
import { formatEther } from 'viem';
import { useAccount } from 'wagmi';
import { buyAtom } from '../atoms/buyAtom';
Expand Down Expand Up @@ -37,7 +37,6 @@ import { getItemByGroupId, getItemByObjectId } from '../utils/apis';
* Can be queryed in API
*/
const Resource = () => {
const navigator = useNavigate();
const [p] = useSearchParams();
const groupId = p.get('gid') as string;

Expand All @@ -63,13 +62,50 @@ const Resource = () => {
Number(chainItemInfo?.categoryIds?.[0] || 100),
);

const { confirmDelist } = useDelist({
onSuccess: async () => {
navigator(`profile?tab=uploaded`);
// await refetchRelation();
// await refetchItem();
},
});
// useEffect(() => {
// async function xx() {
// console.log(
// 'chainItemInfo?.objectIds?.[0]',
// chainItemInfo?.objectIds?.[0],
// );
// if (!chainItemInfo?.objectIds?.[0]) return;

// const objRes = await client.object.headObjectById(
// String(chainItemInfo?.objectIds?.[0]) || '0',
// );
// console.log('objRes', objRes);

// const oid = objRes.objectInfo?.id;

// if (!objRes.objectInfo?.bucketName) return;

// const bucketRes = await client.bucket.headBucket(
// objRes.objectInfo.bucketName,
// );
// console.log('bucketRes', bucketRes);

// const bid = bucketRes.bucketInfo?.id;
// }

// xx();
// }, [chainItemInfo?.objectIds]);

const { confirmDelist } = useDelist();
// onSuccess: async (objectId) => {
// debugger;
// console.log('objectId', objectId);
// const objRes = await client.object.headObjectById(String(objectId));
// console.log('objRes', objRes);
// const oid = objRes.objectInfo?.id;
// if (!objRes.objectInfo?.bucketName) return;
// const bucketRes = await client.bucket.headBucket(
// objRes.objectInfo.bucketName,
// );
// console.log('bucketRes', bucketRes);
// const bid = bucketRes.bucketInfo?.id;
// navigator(`detail?bid=${bid}&oid=${oid}`);
// },

const { onOpen } = useWalletKitModal();

const { data: usdExchange } = useGetBnbUsdtExchangeRate();
Expand Down Expand Up @@ -250,7 +286,7 @@ const Resource = () => {
String(chainItemInfo.objectIds?.[0]),
);
console.log('groupId', groupId);
confirmDelist(BigInt(groupId));
confirmDelist(BigInt(groupId), chainItemInfo.objectIds?.[0]);
}}
>
Delist
Expand Down

0 comments on commit 4a95316

Please sign in to comment.