Skip to content

Commit 35bbdea

Browse files
committed
refactor(tsx): convert OpenPushRequests view
1 parent e602895 commit 35bbdea

File tree

3 files changed

+129
-88
lines changed

3 files changed

+129
-88
lines changed

src/ui/views/OpenPushRequests/OpenPushRequests.jsx

Lines changed: 0 additions & 50 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import React from 'react';
2+
import GridItem from '../../components/Grid/GridItem';
3+
import GridContainer from '../../components/Grid/GridContainer';
4+
import PushesTable from './components/PushesTable';
5+
import CustomTabs from '../../components/CustomTabs/CustomTabs';
6+
import { Visibility, CheckCircle, Cancel, Block } from '@material-ui/icons';
7+
import { SvgIconProps } from '@material-ui/core';
8+
9+
interface TabConfig {
10+
tabName: string;
11+
tabIcon: React.ComponentType<SvgIconProps>;
12+
tabContent: React.ReactNode;
13+
}
14+
15+
const Dashboard: React.FC = () => {
16+
const tabs: TabConfig[] = [
17+
{
18+
tabName: 'Pending',
19+
tabIcon: Visibility,
20+
tabContent: (
21+
<PushesTable
22+
blocked={true}
23+
authorised={false}
24+
rejected={false}
25+
canceled={false}
26+
/>
27+
),
28+
},
29+
{
30+
tabName: 'Approved',
31+
tabIcon: CheckCircle,
32+
tabContent: <PushesTable authorised={true} />,
33+
},
34+
{
35+
tabName: 'Canceled',
36+
tabIcon: Cancel,
37+
tabContent: <PushesTable authorised={false} rejected={false} canceled={true} />,
38+
},
39+
{
40+
tabName: 'Rejected',
41+
tabIcon: Block,
42+
tabContent: <PushesTable authorised={false} rejected={true} canceled={false} />,
43+
},
44+
];
45+
46+
return (
47+
<div>
48+
<GridContainer>
49+
<GridItem xs={12} sm={12} md={12}>
50+
<CustomTabs
51+
headerColor="primary"
52+
tabs={tabs}
53+
/>
54+
</GridItem>
55+
</GridContainer>
56+
</div>
57+
);
58+
};
59+
60+
export default Dashboard;

src/ui/views/OpenPushRequests/components/PushesTable.jsx renamed to src/ui/views/OpenPushRequests/components/PushesTable.tsx

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,50 @@ import { KeyboardArrowRight } from '@material-ui/icons';
1616
import Search from '../../../components/Search/Search';
1717
import Pagination from '../../../components/Pagination/Pagination';
1818

19-
export default function PushesTable(props) {
20-
const useStyles = makeStyles(styles);
19+
interface CommitData {
20+
commitTs?: number;
21+
commitTimestamp?: number;
22+
message: string;
23+
committer: string;
24+
author: string;
25+
authorEmail?: string;
26+
}
27+
28+
interface PushData {
29+
id: string;
30+
repo: string;
31+
branch: string;
32+
commitTo: string;
33+
commitData: CommitData[];
34+
}
35+
36+
interface PushesTableProps {
37+
[key: string]: any;
38+
}
39+
40+
const useStyles = makeStyles(styles as any);
41+
42+
const PushesTable: React.FC<PushesTableProps> = (props) => {
2143
const classes = useStyles();
22-
const [data, setData] = useState([]);
23-
const [filteredData, setFilteredData] = useState([]);
44+
const [data, setData] = useState<PushData[]>([]);
45+
const [filteredData, setFilteredData] = useState<PushData[]>([]);
2446
const [isLoading, setIsLoading] = useState(false);
2547
const [isError, setIsError] = useState(false);
2648
const navigate = useNavigate();
2749
const [, setAuth] = useState(true);
2850
const [currentPage, setCurrentPage] = useState(1);
2951
const itemsPerPage = 5;
3052
const [searchTerm, setSearchTerm] = useState('');
31-
const openPush = (push) => navigate(`/dashboard/push/${push}`, { replace: true });
53+
54+
const openPush = (pushId: string) => navigate(`/dashboard/push/${pushId}`, { replace: true });
3255

3356
useEffect(() => {
34-
const query = {};
35-
for (const k in props) {
36-
if (k) query[k] = props[k];
37-
}
57+
const query = {
58+
blocked: props.blocked ?? false,
59+
canceled: props.canceled ?? false,
60+
authorised: props.authorised ?? false,
61+
rejected: props.rejected ?? false,
62+
};
3863
getPushes(setIsLoading, setData, setAuth, setIsError, query);
3964
}, [props]);
4065

@@ -49,31 +74,29 @@ export default function PushesTable(props) {
4974
(item) =>
5075
item.repo.toLowerCase().includes(lowerCaseTerm) ||
5176
item.commitTo.toLowerCase().includes(lowerCaseTerm) ||
52-
item.commitData[0].message.toLowerCase().includes(lowerCaseTerm),
77+
item.commitData[0]?.message.toLowerCase().includes(lowerCaseTerm),
5378
)
5479
: data;
5580
setFilteredData(filtered);
5681
setCurrentPage(1);
5782
}, [searchTerm, data]);
5883

59-
const handleSearch = (term) => setSearchTerm(term.trim());
84+
const handleSearch = (term: string) => setSearchTerm(term.trim());
6085

61-
const handlePageChange = (page) => {
86+
const handlePageChange = (page: number) => {
6287
setCurrentPage(page);
6388
};
6489

6590
const indexOfLastItem = currentPage * itemsPerPage;
6691
const indexOfFirstItem = indexOfLastItem - itemsPerPage;
6792
const currentItems = filteredData.slice(indexOfFirstItem, indexOfLastItem);
6893

69-
const paginate = (pageNumber) => setCurrentPage(pageNumber);
70-
7194
if (isLoading) return <div>Loading...</div>;
7295
if (isError) return <div>Something went wrong ...</div>;
7396

7497
return (
7598
<div>
76-
<Search onSearch={handleSearch} /> {}
99+
<Search onSearch={handleSearch} />
77100
<TableContainer component={Paper}>
78101
<Table className={classes.table} aria-label='simple table'>
79102
<TableHead>
@@ -91,16 +114,16 @@ export default function PushesTable(props) {
91114
</TableRow>
92115
</TableHead>
93116
<TableBody>
94-
{currentItems.reverse().map((row) => {
117+
{[...currentItems].reverse().map((row) => {
95118
const repoFullName = row.repo.replace('.git', '');
96119
const repoBranch = row.branch.replace('refs/heads/', '');
120+
const commitTimestamp =
121+
row.commitData[0]?.commitTs || row.commitData[0]?.commitTimestamp;
97122

98123
return (
99124
<TableRow key={row.id}>
100125
<TableCell align='left'>
101-
{moment
102-
.unix(row.commitData[0].commitTs || row.commitData[0].commitTimestamp)
103-
.toString()}
126+
{commitTimestamp ? moment.unix(commitTimestamp).toString() : 'N/A'}
104127
</TableCell>
105128
<TableCell align='left'>
106129
<a href={`https://github.com/${row.repo}`} rel='noreferrer' target='_blank'>
@@ -126,33 +149,41 @@ export default function PushesTable(props) {
126149
</a>
127150
</TableCell>
128151
<TableCell align='left'>
129-
<a
130-
href={`https://github.com/${row.commitData[0].committer}`}
131-
rel='noreferrer'
132-
target='_blank'
133-
>
134-
{row.commitData[0].committer}
135-
</a>
152+
{row.commitData[0]?.committer ? (
153+
<a
154+
href={`https://github.com/${row.commitData[0].committer}`}
155+
rel='noreferrer'
156+
target='_blank'
157+
>
158+
{row.commitData[0].committer}
159+
</a>
160+
) : (
161+
'N/A'
162+
)}
136163
</TableCell>
137164
<TableCell align='left'>
138-
<a
139-
href={`https://github.com/${row.commitData[0].author}`}
140-
rel='noreferrer'
141-
target='_blank'
142-
>
143-
{row.commitData[0].author}
144-
</a>
165+
{row.commitData[0]?.author ? (
166+
<a
167+
href={`https://github.com/${row.commitData[0].author}`}
168+
rel='noreferrer'
169+
target='_blank'
170+
>
171+
{row.commitData[0].author}
172+
</a>
173+
) : (
174+
'N/A'
175+
)}
145176
</TableCell>
146177
<TableCell align='left'>
147-
{row.commitData[0].authorEmail ? (
178+
{row.commitData[0]?.authorEmail ? (
148179
<a href={`mailto:${row.commitData[0].authorEmail}`}>
149180
{row.commitData[0].authorEmail}
150181
</a>
151182
) : (
152183
'No data...'
153184
)}
154185
</TableCell>
155-
<TableCell align='left'>{row.commitData[0].message}</TableCell>
186+
<TableCell align='left'>{row.commitData[0]?.message || 'N/A'}</TableCell>
156187
<TableCell align='left'>{row.commitData.length}</TableCell>
157188
<TableCell component='th' scope='row'>
158189
<Button variant='contained' color='primary' onClick={() => openPush(row.id)}>
@@ -165,14 +196,14 @@ export default function PushesTable(props) {
165196
</TableBody>
166197
</Table>
167198
</TableContainer>
168-
{/* Pagination Component */}
169199
<Pagination
170200
itemsPerPage={itemsPerPage}
171201
totalItems={filteredData.length}
172-
paginate={paginate}
173202
currentPage={currentPage}
174203
onPageChange={handlePageChange}
175204
/>
176205
</div>
177206
);
178-
}
207+
};
208+
209+
export default PushesTable;

0 commit comments

Comments
 (0)