|
3 | 3 | * |
4 | 4 | * Page for the list of teams. |
5 | 5 | */ |
6 | | -import React from "react"; |
| 6 | +import React, { useCallback, useState, useEffect } from "react"; |
| 7 | +import _ from "lodash"; |
7 | 8 | import LayoutContainer from "components/LayoutContainer"; |
8 | 9 | import PageHeader from "components/PageHeader"; |
9 | | -import { useData } from "../../hooks/useData"; |
| 10 | +import Input from "components/Input"; |
| 11 | +import Pagination from "components/Pagination"; |
10 | 12 | import { getMyTeams } from "../../services/teams"; |
11 | 13 | import TeamCard from "./components/TeamCard"; |
12 | 14 | import TeamCardGrid from "./components/TeamCardGrid"; |
13 | 15 | import LoadingIndicator from "../../components/LoadingIndicator"; |
14 | | -import { useAsync } from "react-use"; |
15 | | -import { |
16 | | - getAuthUserTokens, |
17 | | -} from "@topcoder/micro-frontends-navbar-app"; |
| 16 | +import { useDebounce } from "react-use"; |
| 17 | +import { TEAMS_PER_PAGE } from "constants"; |
| 18 | +import "./styles.module.scss"; |
18 | 19 |
|
19 | 20 | const MyTeamsList = () => { |
20 | | - const authUserTokens = useAsync(getAuthUserTokens); |
21 | | - const tokenV3 = authUserTokens.value ? authUserTokens.value.tokenV3 : null; |
22 | | - const [myTeams, loadingError] = useData(getMyTeams, tokenV3); |
| 21 | + let [myTeams, setMyTeams] = useState(null); |
| 22 | + const [filter, setFilter] = useState(""); |
| 23 | + const [tempFilter, setTempFilter] = React.useState(''); |
| 24 | + const [loadingError, setLoadingError] = useState(false); |
| 25 | + const [page, setPage] = useState(1); |
| 26 | + const [total, setTotal] = useState(0); |
| 27 | + const onFilterChange = (evt) => { |
| 28 | + setTempFilter(evt.target.value) |
| 29 | + } |
| 30 | + |
| 31 | + useDebounce((value) => { |
| 32 | + console.log('xxxx', value) |
| 33 | + setFilter(tempFilter); |
| 34 | + setPage(1); |
| 35 | + }, 200, [tempFilter]); |
| 36 | + |
| 37 | + useEffect(() => { |
| 38 | + setMyTeams(null); |
| 39 | + getMyTeams(filter, page, TEAMS_PER_PAGE) |
| 40 | + .then((response) => { |
| 41 | + setMyTeams(response.data); |
| 42 | + setTotal(response.headers["x-total"]); |
| 43 | + }) |
| 44 | + .catch((responseError) => { |
| 45 | + setLoadingError(responseError); |
| 46 | + }); |
| 47 | + }, [filter, page]); |
| 48 | + |
| 49 | + const onPageClick = useCallback( |
| 50 | + (newPage) => { |
| 51 | + setPage(newPage); |
| 52 | + }, |
| 53 | + [setPage] |
| 54 | + ); |
23 | 55 |
|
24 | 56 | return ( |
25 | 57 | <LayoutContainer> |
26 | | - <PageHeader title="My Teams" /> |
| 58 | + <PageHeader |
| 59 | + title="My Teams" |
| 60 | + aside={ |
| 61 | + <Input |
| 62 | + placeholder="Filter by team name" |
| 63 | + styleName="filter-input" |
| 64 | + onChange={onFilterChange} |
| 65 | + /> |
| 66 | + } |
| 67 | + /> |
| 68 | + {myTeams && myTeams.length === 0 && (<div styleName="empty">No teams found</div>)} |
27 | 69 | {!myTeams ? ( |
28 | 70 | <LoadingIndicator error={loadingError && loadingError.toString()} /> |
29 | 71 | ) : ( |
30 | | - <TeamCardGrid> |
31 | | - {myTeams.map((team) => ( |
32 | | - <TeamCard key={team.id} team={team} /> |
33 | | - ))} |
34 | | - </TeamCardGrid> |
| 72 | + <> |
| 73 | + <TeamCardGrid> |
| 74 | + {myTeams.map((team) => ( |
| 75 | + <TeamCard key={team.id} team={team} /> |
| 76 | + ))} |
| 77 | + </TeamCardGrid> |
| 78 | + {myTeams.length > 0 && ( |
| 79 | + <div styleName="pagination-wrapper"> |
| 80 | + <Pagination |
| 81 | + total={total} |
| 82 | + currentPage={page} |
| 83 | + perPage={TEAMS_PER_PAGE} |
| 84 | + onPageClick={onPageClick} |
| 85 | + /> |
| 86 | + </div> |
| 87 | + )} |
| 88 | + </> |
35 | 89 | )} |
36 | 90 | </LayoutContainer> |
37 | 91 | ); |
|
0 commit comments