forked from YJU-OKURA/project_minori-next-deployment-repo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaterialContainer.tsx
109 lines (103 loc) · 3.1 KB
/
MaterialContainer.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {ChangeEvent, useState, KeyboardEvent} from 'react';
import InfiniteScroll from 'react-infinite-scroller';
import Image from 'next/image';
import MaterialList from './MaterialList';
import getMaterial from '@/src/api/material/getMaterial';
import searchMaterial from '@/src/api/material/searchMaterial';
import {Material, ParamsProps} from '@/src/interfaces/navbar';
import icons from '@/public/svgs/navbar';
const MaterialContainer = ({
params,
cId,
}: {
params: ParamsProps;
cId: string | null;
}) => {
const [materials, setMaterials] = useState<Material[]>([]);
const [searchMaterials, setSearchMaterials] = useState<Material[]>([]);
const [keyWord, setKeyWord] = useState<string>('');
const [boardPage, setBoardPage] = useState<number>(1);
const [hasMore, setHasMore] = useState<boolean>(true);
const onLoadMore = () => {
setHasMore(false);
getMaterial(4, boardPage, 8).then(res => {
if (res.length === 0) {
setHasMore(false);
} else {
setMaterials(prevMaterials => [
...(prevMaterials ? prevMaterials : []),
...res,
]);
setBoardPage(prevBoardPage => prevBoardPage + 1);
setHasMore(true);
}
});
};
const handleInputText = (e: ChangeEvent<HTMLInputElement>) => {
if (e.target.value) {
setKeyWord(e.target.value);
}
if (e.target.value === '') {
setSearchMaterials([]);
setKeyWord('');
}
};
const handleKeyEnter = (e: KeyboardEvent<HTMLInputElement>) => {
if (e.key === 'Enter')
searchMaterial(1, keyWord, 1, 5).then(res => {
console.log(res);
setSearchMaterials(res);
});
};
return (
<div className="">
{/* prompt - search */}
<div className="w-full flex bg-white items-center mb-3 px-1">
<Image
src={icons.search}
alt="icon"
width={20}
height={20}
className="w-5 h-5 opacity-50"
/>
<input
type="text"
className="w-full p-1 border-0 outline-none"
placeholder="Search"
onChange={handleInputText}
onKeyDown={handleKeyEnter}
/>
</div>
{/* Prompt - list */}
<div className="h-[350px] overflow-auto">
<InfiniteScroll
pageStart={0}
loadMore={onLoadMore}
hasMore={hasMore}
loader={
<div key="unique" className="flex justify-center items-center">
<div className="w-[80px] h-[80px] border border-4 border-dashed border-gray-500 rounded-full text-sm flex justify-center items-center">
Loading
</div>
</div>
}
useWindow={false}
threshold={20}
>
{materials ? (
keyWord ? (
<MaterialList
materials={searchMaterials}
params={params}
cId={cId}
/>
) : (
<MaterialList materials={materials} params={params} cId={cId} />
)
) : null}
</InfiniteScroll>
</div>
</div>
);
};
export default MaterialContainer;