Skip to content

Commit

Permalink
feat: Collection List Button
Browse files Browse the repository at this point in the history
  • Loading branch information
ybgbob committed Nov 17, 2023
1 parent 19af955 commit 4279232
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 53 deletions.
16 changes: 11 additions & 5 deletions src/components/resource/MyBreadcrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
useNavigate,
useSearchParams,
} from 'react-router-dom';
import { getItemByBucketId } from '../../utils/apis';
import { getCollectionInfoByName } from '../../utils/gfSDK';

interface Props {
root: {
Expand All @@ -23,7 +25,7 @@ export const MyBreadcrumb = (props: Props) => {
const bucketPath = path === '/' ? '' : path.split('/');
const breadItems = [root.bucketName].concat(bucketPath);

console.log('props', breadItems.length - 1);
// console.log('props', breadItems.length - 1);

return (
<CustomBreadcrumb>
Expand All @@ -36,15 +38,19 @@ export const MyBreadcrumb = (props: Props) => {
<BreadcrumbLink fontSize="16px" as="span">
<NavLink
as="span"
onClick={() => {
const params: Record<string, string> = {
id: id,
};
onClick={async () => {
const params: Record<string, string> = {};

if (item !== root.bucketName) {
params.path = item + '/';
}

const bucketData = await getCollectionInfoByName(item);
const ItemInfo = await getItemByBucketId(
bucketData.bucketInfo.id,
);
params.id = String(ItemInfo.id);

navigator({
pathname: '/resource',
search: `?${createSearchParams(params)}`,
Expand Down
89 changes: 77 additions & 12 deletions src/components/resource/collection/ActionButtonGroup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box } from '@totejs/uikit';
import { Box, Button } from '@totejs/uikit';
import { FILE_ITEM } from '../../../hooks/useGetObjectList';
import { useAccount } from 'wagmi';
import { useMemo } from 'react';
Expand All @@ -9,6 +9,10 @@ import {
useGetObject,
} from '../../../hooks/useGetBucketOrObj';
import { generateGroupName } from '../../../utils';
import { useGetItemById } from '../../../hooks/useGetItemById';
import { getItemById, getItemByObjectId } from '../../../utils/apis';
import { useGetDownloadUrl } from '../../../hooks/useGetDownloadUrl';
import { DownloadIcon } from '@totejs/icons';

interface Props {
fileInfo: FILE_ITEM;
Expand All @@ -24,24 +28,34 @@ export const ActionButtonGroup = (props: Props) => {

const { data: bucketData } = useGetBucketByName(BucketName);
const { data: objectData } = useGetObject(BucketName, ObjectName);

const { data: groupData } = useGetGroupByName(
generateGroupName(BucketName, ObjectName),
data.Owner,
);

const downloadUrl = useGetDownloadUrl({
bucketName: BucketName,
name: ObjectName,
});

return (
<Box>
{/* only owner can delist */}
{isListed && data.Owner === address && (
<Box
<Button
size={'sm'}
background="#665800"
color="#FFE900"
h="32px"
fontSize="14px"
p="8px 16px"
onClick={() => {
if (!objectData || !groupData) return;

console.log(
'generateGroupName(BucketName, ObjectName)',
generateGroupName(BucketName, ObjectName),
);
// console.log(
// 'generateGroupName(BucketName, ObjectName)',
// generateGroupName(BucketName, ObjectName),
// );

modalData.modalDispatch({
type: 'OPEN_DELIST',
Expand All @@ -59,12 +73,18 @@ export const ActionButtonGroup = (props: Props) => {
}}
>
delist
</Box>
</Button>
)}

{/* only owner can list */}
{!isListed && data.Owner === address && (
<Box
<Button
size={'sm'}
background="#665800"
color="#FFE900"
h="32px"
fontSize="14px"
p="8px 16px"
onClick={() => {
if (!bucketData || !objectData) return;

Expand All @@ -84,14 +104,59 @@ export const ActionButtonGroup = (props: Props) => {
}}
>
list
</Box>
</Button>
)}

{/* only buyer can download */}
{isPurchasedByMe && <Box>download</Box>}
{isListed && isPurchasedByMe && (
<Button
h="32px"
bg="none"
color="#F1F2F3"
border="1px solid #F1F2F3"
fontSize="14px"
p="8px 16px"
_hover={{
background: '#E1E2E5',
color: '#181A1E',
}}
onClick={() => {
window.open(downloadUrl);
}}
>
<DownloadIcon />
<Box as="span" ml="8px">
Download
</Box>
</Button>
)}

{/* haven't bought can buy excepy owner */}
{!isPurchasedByMe && data.Owner !== address && <Box>Buy</Box>}
{isListed && !isPurchasedByMe && data.Owner !== address && (
<Button
size={'sm'}
background="#665800"
color="#FFE900"
h="32px"
fontSize="14px"
p="8px 16px"
onClick={async () => {
// console.log('isPurchasedByMe', isPurchasedByMe);
if (!objectData) return;

const itemInfo = await getItemByObjectId(objectData.objectInfo.id);
modalData.modalDispatch({
type: 'OPEN_BUY',
buyData: itemInfo,
callBack: () => {
uploadFn();
},
});
}}
>
Only Buy This
</Button>
)}
</Box>
);
};
32 changes: 18 additions & 14 deletions src/components/resource/collection/CollectionList.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import styled from '@emotion/styled';
import { CardPocketIcon } from '@totejs/icons';
import { Box, ColumnDef, Flex, Table } from '@totejs/uikit';
import _ from 'lodash';
import { useCallback, useEffect, useState } from 'react';
import { useCallback } from 'react';
import {
createSearchParams,
useNavigate,
useSearchParams,
} from 'react-router-dom';
import { useAccount } from 'wagmi';
import { DEFAULT_ITEM } from '../../../hooks/useGetItemById';
import {
useGetItemByObjId,
useGetItemsByObjIds,
usePurchaseQueryByObjIds,
} from '../../../hooks/useGetItemByObjId';
Expand All @@ -21,18 +18,16 @@ import {
objListMergeListedAndPurchased,
useGetObjectList,
} from '../../../hooks/useGetObjectList';
import { useGlobal } from '../../../hooks/useGlobal';
import { useModal } from '../../../hooks/useModal';
import { usePagination } from '../../../hooks/usePagination';
import { useSalesVolume } from '../../../hooks/useSalesVolume';
import {
contentTypeToExtension,
defaultImg,
formatDateUTC,
generateGroupName,
parseFileSize,
trimLongStr,
} from '../../../utils';
import { getItemByObjectId } from '../../../utils/apis';
import { Item } from '../../../utils/apis/types';
import { QueryHeadBucketResponse } from '../../../utils/gfSDK';
import { Loader } from '../../Loader';
Expand Down Expand Up @@ -67,7 +62,6 @@ const CollectionList = (props: Props) => {
.map((item: any) => {
return item.data.Id as number;
});
// console.log('oidList', oidList);

const { data: objListedList, refetch: reloadListedList } =
useGetItemsByObjIds(oidList || []);
Expand All @@ -81,17 +75,18 @@ const CollectionList = (props: Props) => {
reloadPurchasedList();
}, [reloadListedList, reloadObjectList, reloadPurchasedList]);

console.log('objListedList', objListedList);
console.log('purchasedList', purchasedList);
// console.log('oidList', oidList);
// console.log('objListedList', objListedList);
// console.log('purchasedList', purchasedList);

const mergedList = objListMergeListedAndPurchased(
objectList,
objListedList,
purchasedList,
);

console.log('objectList', objectList);
console.log('mergedList', mergedList);
// console.log('objectList', objectList);
// console.log('mergedList', mergedList);

const { handlePageChange, page } = usePagination();

Expand Down Expand Up @@ -126,8 +121,17 @@ const CollectionList = (props: Props) => {
}

if (type === 'file') {
const { isListed } = data;
return (
<ImgContainer>
<ImgContainer
cursor={isListed ? 'pointer' : 'inherit'}
onClick={async () => {
if (isListed) {
const item = await getItemByObjectId(data.data.Id);
navigator(`/resource?id=${item.id}`);
}
}}
>
<ImgCon src={defaultImg(name, 40)}></ImgCon>
<Box title={name}>{trimLongStr(name, 15)}</Box>
</ImgContainer>
Expand Down Expand Up @@ -190,7 +194,7 @@ const CollectionList = (props: Props) => {
{
header: '',
cell: (data) => {
console.log('data', data);
// console.log('data', data);
return (
data.type === 'file' && (
<ActionButtonGroup fileInfo={data} uploadFn={reloadList} />
Expand Down
1 change: 1 addition & 0 deletions src/context/modal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const ModalReducer = (initialState: any, action: any) => {
...initialState,
openBuy: true,
buyData: action.buyData,
callBack: action.callBack,
};
case 'BUYING':
return {
Expand Down
5 changes: 0 additions & 5 deletions src/hooks/useGetBucketOrObj.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,6 @@ export const useGetBOInfoFromGroup = (groupName?: string) => {
// bucketName = _;
}

console.log({
type: rType,
bucketName,
objectName,
});
setBoInfo({
type: rType,
bucketName,
Expand Down
5 changes: 3 additions & 2 deletions src/hooks/useGetItemById.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const useGetItemById = (id: number) => {
const [itemData, setItemData] = useState<Item>();

const { data, isLoading, refetch } = useQuery({
enabled: !!id,
queryKey: ['GET_ITEM', id],
queryFn: () => getItemById(id),
gcTime: 20000,
Expand All @@ -38,10 +39,10 @@ export const useGetItemById = (id: number) => {
});

useEffect(() => {
if (isLoading || !data) return;
if (isLoading || !data || !id) return;

setItemData(data);
}, [data, isLoading]);
}, [data, id, isLoading]);

return {
data: itemData,
Expand Down
17 changes: 10 additions & 7 deletions src/hooks/useGetItemByObjId.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,21 +87,24 @@ export const usePurchaseQueryByObjIds = (
const res = await queryPurchase({
filter: {
address,
objectIds: ids.concat(178839),
objectIds: ids,
},
offset: 0,
limit: 10,
sort: 'CREATION_DESC',
});
console.log('res --->', res);

return res.purchases
.filter((item) => {
return ids.includes(item.resourceId);
const { purchases } = res;

const xx = purchases
.filter((p) => {
return ids.includes(p.item.resourceId);
})
.map((item) => {
return item.resourceId;
.map((p) => {
return p.item.resourceId;
});

return xx;
},
});
};
Expand Down
6 changes: 0 additions & 6 deletions src/hooks/useGetObjectList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,6 @@ export const objListMergeListedAndPurchased = (
listedList: number[] | undefined,
purchasedList: number[] | undefined,
) => {
console.log(
'objListMergeListedAndPurchased objectList',
objectList,
listedList,
purchasedList,
);
if (!objectList) return [];
if (!listedList || !purchasedList) return objectList;

Expand Down
16 changes: 14 additions & 2 deletions src/utils/apis/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,13 @@ export interface SearchPurchaseRequest {
limit: number;
}
export interface SearchPurchaseResponse {
purchases: Item[];
purchases: {
id: number;
buyerAddress: string;
price: number;
createdAt: number;
item: Item;
}[];
total: number;
}

Expand All @@ -74,6 +80,12 @@ export interface QueryPurchaseRequest {
limit: number;
}
export interface QueryPurchaseResponse {
purchases: Item[];
purchases: {
id: number;
buyerAddress: string;
price: number;
createdAt: number;
item: Item;
}[];
total: number;
}

0 comments on commit 4279232

Please sign in to comment.