Skip to content

Commit c72e594

Browse files
authored
Merge pull request #29 from TryShape/issue-19-sort-shapes
Issue 19 sort shapes
2 parents 02bcce9 + fc2bd55 commit c72e594

3 files changed

Lines changed: 50 additions & 15 deletions

File tree

components/core/App.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const App = (props) => {
1818
const [loading, setLoading] = useState(true); // shapes loading
1919

2020
const [searchTerm, setSearchTerm] = useState(""); // search
21+
const [sort, setSort] = useState("popularity"); // sort
2122

2223
const { user } = props;
2324

@@ -92,7 +93,9 @@ const App = (props) => {
9293
<>
9394
<Header {...props}
9495
searchTerm={searchTerm}
95-
setSearchTerm={setSearchTerm}/>
96+
setSearchTerm={setSearchTerm}
97+
sort={sort}
98+
setSort={setSort} />
9699
{loading ? (
97100
<Loader
98101
style={{margin: '20% auto auto 42%'}}
@@ -102,7 +105,7 @@ const App = (props) => {
102105
width={300}
103106
/>
104107
) : (
105-
<ShapeList {...props} data={ data } searchTerm={searchTerm} />
108+
<ShapeList {...props} data={ data } searchTerm={searchTerm} sort={sort} />
106109
)}
107110
</>
108111
);

components/utils/Header.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ const NavbarSearchInputControl = styled(FormControl)`
9191

9292
const CloseIcon = styled(FiX)`
9393
margin: 0.37rem;
94+
cursor: pointer;
9495
`;
9596

9697
const UserThumb = styled.div`
@@ -131,7 +132,9 @@ const Header = ({
131132
user,
132133
setUser,
133134
searchTerm,
134-
setSearchTerm
135+
setSearchTerm,
136+
sort,
137+
setSort
135138
}) => {
136139

137140
const [searchterm, setSearchterm] = useState('');
@@ -167,12 +170,27 @@ const Header = ({
167170
value={searchTerm}
168171
onChange={(e) => setSearchTerm(e.target.value)}
169172
/>
170-
<CloseIcon color='#ffffff' size='24px' />
173+
<CloseIcon title="Clear Search Query" color='#ffffff' size='24px' onClick={() => setSearchTerm('')}/>
171174
</NavbarSearchInput>
172-
<DropdownButton variant="outline-secondary" size="sm" id="dropdown-basic-button" title="View by Popularity" className="border-0">
173-
<Dropdown.Item href="#/action-1">Likes</Dropdown.Item>
174-
<Dropdown.Item href="#/action-2" active>Popularity</Dropdown.Item>
175-
<Dropdown.Item href="#/action-3">Newly Added</Dropdown.Item>
175+
<DropdownButton variant="outline-secondary" size="sm" id="dropdown-basic-button" title={`Sort by ${sort}`} className="border-0">
176+
<Dropdown.Item
177+
href="#"
178+
active={sort==='oldest'}
179+
onClick={() =>setSort('oldest')}>
180+
Oldest
181+
</Dropdown.Item>
182+
<Dropdown.Item
183+
href="#"
184+
active={sort==='popularity'}
185+
onClick={() =>setSort('popularity')}>
186+
Popularity
187+
</Dropdown.Item>
188+
<Dropdown.Item
189+
href="#"
190+
active={sort==='recent'}
191+
onClick={() =>setSort('recent')}>
192+
Recent
193+
</Dropdown.Item>
176194
</DropdownButton>
177195
</NavbarSearchInputContainer>
178196

components/utils/ShapeList.js

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,14 @@ const LikeFilledIcon = styled(BsFillHeartFill)`
191191
cursor: pointer;
192192
`;
193193

194-
const ShapeList = ({ setOpen, user, data, searchTerm }) => {
194+
const ShapeList = (
195+
{
196+
setOpen,
197+
user,
198+
data,
199+
searchTerm,
200+
sort
201+
}) => {
195202

196203
const filterShape = (shapes, searchTerm) => {
197204
if (!searchTerm) {
@@ -209,8 +216,16 @@ const ShapeList = ({ setOpen, user, data, searchTerm }) => {
209216
const [shapeToExport, setShapeToExport] = useState();
210217

211218
useEffect(() =>{
212-
setFilteredShape(filterShape(shapes, searchTerm));
213-
}, [searchTerm, shapes]);
219+
const copy = [...shapes];
220+
if(sort === 'recent') {
221+
copy.sort((a, b) => b.__createdtime__ - a.__createdtime__);
222+
} else if(sort === 'popularity') {
223+
copy.sort((a, b) => b.likes - a.likes);
224+
} else if(sort === 'oldest') {
225+
copy.sort((a, b) => a.__createdtime__ - b.__createdtime__);
226+
}
227+
setFilteredShape(filterShape(copy, searchTerm));
228+
}, [searchTerm, shapes, sort]);
214229

215230
const handleSwicth = (shapeName) => {
216231

@@ -359,19 +374,18 @@ const ShapeList = ({ setOpen, user, data, searchTerm }) => {
359374
/>
360375
<ShapeActions className="shape-actions">
361376
<ShapeActionsContainer>
362-
<span
363-
title="Like"
377+
<span
364378
onClick={(event, shapeId) => performLike(event, shape['shape_id'])}>
365379
{
366380
shape.liked ?
367381
(
368-
<Button variant="danger" className="btn-icon">
382+
<Button title="Remove Like" variant="danger" className="btn-icon">
369383
<LikeFilledIcon size={24} />
370384
</Button>
371385
)
372386
:
373387
(
374-
<Button variant="outline-light" className="btn-icon">
388+
<Button title="Add Like" variant="outline-light" className="btn-icon">
375389
<LikeIcon size={24} />
376390
</Button>
377391
)

0 commit comments

Comments
 (0)