Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
hdJerry committed Aug 3, 2023
1 parent 7f26b24 commit deccd45
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 218 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,115 +11,114 @@ import React, { useEffect, useState } from 'react';

import type { PRTabValues } from '../types';
import {
OPEN_PULLS_URL,
CLOSED_PULLS_URL,
OPEN_PULLS_URL,
CLOSED_PULLS_URL,
} from '../../../constants/url.constants';
import type { PullRequest, PullRequests } from './PullRequest.type';
import PullRequestView from './PullRequest.view';
import { fromFetchWithAuth } from '../../../hooks/auth/from-fetch-with-auth';
import { useParams } from 'react-router-dom';

export default function PullRequestCtrl() {
const [activeTab, setActiveTab] = useState<PRTabValues>('open');
const [openPRPage, setOpenPRPage] = useState(1);
const [closedPRPage, setClosedPRPage] = useState(1);
const setPRPage = activeTab === 'open' ? setOpenPRPage : setClosedPRPage;
const [openPullRequests, setOpenPullRequests] = useState<PullRequests>({
total_count: 0,
items: [],
});
const [closedPullRequests, setClosedPullRequests] = useState<PullRequests>({
total_count: 0,
items: [],
});
const [activeTab, setActiveTab] = useState<PRTabValues>('open');
const [openPRPage, setOpenPRPage] = useState(1);
const [closedPRPage, setClosedPRPage] = useState(1);
const setPRPage = activeTab === 'open' ? setOpenPRPage : setClosedPRPage;
const [openPullRequests, setOpenPullRequests] = useState<PullRequests>({
total_count: 0,
items: [],
});
const [closedPullRequests, setClosedPullRequests] = useState<PullRequests>({
total_count: 0,
items: [],
});

const { username, repo } = useParams();
const { username, repo } = useParams();

useEffect(() => {
if (username && repo) {
const openPRSubscription: Subscription = fromFetchWithAuth<PullRequests>(
OPEN_PULLS_URL(username, repo, openPRPage),
{
selector: async (response: Response) => {
const res = await response.json();
return res as any;
},
}
)
.pipe(
filter((pulls) => !!pulls.total_count),
switchMap((pulls: PullRequests) => {
const requests = pulls.items.map(createCommentCountRequest);
return zip(...requests).pipe(
map(mergePullRequestsWithCommentCount(pulls))
);
}),
tap(setOpenPullRequests)
)
.subscribe();
useEffect(() => {
if (username && repo) {
const openPRSubscription: Subscription = fromFetchWithAuth<PullRequests>(
OPEN_PULLS_URL(username, repo, openPRPage),
{
selector: async (response: Response) => {
const res = await response.json();
return res as any;
},
}
)
.pipe(
filter((pulls) => !!pulls.total_count),
switchMap((pulls: PullRequests) => {
const requests = pulls.items.map(createCommentCountRequest);
return zip(...requests).pipe(
map(mergePullRequestsWithCommentCount(pulls))
);
}),
tap(setOpenPullRequests)
)
.subscribe();

const closedPRSubscription: Subscription =
fromFetchWithAuth<PullRequests>(
CLOSED_PULLS_URL(username, repo, closedPRPage),
{
selector: async (response: Response) => {
const res = await response.json();
return res as any;
},
}
)
.pipe(
filter((pulls) => !!pulls.total_count),
switchMap((pulls: PullRequests) => {
const requests = pulls.items.map(createCommentCountRequest);
return zip(...requests).pipe(
map(mergePullRequestsWithCommentCount(pulls))
);
}),
tap(setClosedPullRequests)
)
.subscribe();
const closedPRSubscription: Subscription = fromFetchWithAuth<PullRequests>(
CLOSED_PULLS_URL(username, repo, closedPRPage),
{
selector: async (response: Response) => {
const res = await response.json();
return res as any;
},
}
)
.pipe(
filter((pulls) => !!pulls.total_count),
switchMap((pulls: PullRequests) => {
const requests = pulls.items.map(createCommentCountRequest);
return zip(...requests).pipe(
map(mergePullRequestsWithCommentCount(pulls))
);
}),
tap(setClosedPullRequests)
)
.subscribe();

return () => {
openPRSubscription.unsubscribe();
closedPRSubscription.unsubscribe();
};
}
}, [username, repo, openPRPage, closedPRPage]);
return () => {
openPRSubscription.unsubscribe();
closedPRSubscription.unsubscribe();
};
}
}, [username, repo, openPRPage, closedPRPage]);

const PRS =
activeTab === 'open' ? openPullRequests.items : closedPullRequests.items;
const PRS =
activeTab === 'open' ? openPullRequests.items : closedPullRequests.items;

return (
<PullRequestView
pullRequests={PRS}
openPRCount={openPullRequests.total_count}
closedPRCount={closedPullRequests.total_count}
activeTab={activeTab}
changeActiveTab={setActiveTab}
setPRPage={setPRPage}
/>
);
return (
<PullRequestView
pullRequests={PRS}
openPRCount={openPullRequests.total_count}
closedPRCount={closedPullRequests.total_count}
activeTab={activeTab}
changeActiveTab={setActiveTab}
setPRPage={setPRPage}
/>
);
}

function createCommentCountRequest(pr: PullRequest): Observable<number> {
const review_comments_url = `${pr.repository_url}/pulls/${pr.number}/comments`;
return fromFetchWithAuth<number>(review_comments_url, {
selector: (response: Response) => {
return response.json();
},
});
const review_comments_url = `${pr.repository_url}/pulls/${pr.number}/comments`;
return fromFetchWithAuth<number>(review_comments_url, {
selector: (response: Response) => {
return response.json();
},
});
}

function mergePullRequestsWithCommentCount(pulls: PullRequests) {
return (counts: number[]): PullRequests => {
const items = pulls.items.map((p: PullRequest, index: number) => ({
...p,
comments: counts[index],
}));
return {
total_count: pulls.total_count,
items,
};
};
return (counts: number[]): PullRequests => {
const items = pulls.items.map((p: PullRequest, index: number) => ({
...p,
comments: counts[index],
}));
return {
total_count: pulls.total_count,
items,
};
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,57 +13,57 @@ export const Content = styled.div`
border-radius: 6px;
`;
export const PaginationContainer = styled.div`
padding: 10px 0;
padding: 10px 0;
/* pagination */
.pagination {
display: flex;
padding: 0;
justify-content: center;
list-style: none;
cursor: pointer;
}
/* pagination */
.pagination {
display: flex;
padding: 0;
justify-content: center;
list-style: none;
cursor: pointer;
}
.pagination a {
padding: 1px;
border-radius: 5px;
color: var(--default-text-color);
font-size: 14px;
padding: 5px 10px;
margin: 0 5px;
}
.pagination a {
padding: 1px;
border-radius: 5px;
color: var(--default-text-color);
font-size: 14px;
padding: 5px 10px;
margin: 0 5px;
}
.pagination__link {
font-weight: bold;
}
.pagination__link {
font-weight: bold;
}
.pagination__item a:hover {
text-decoration: none;
border: 1px solid var(--text-muted);
}
.pagination__item a:hover {
text-decoration: none;
border: 1px solid var(--text-muted);
}
.pagination__link_end a {
color: #539bf5;
text-decoration: none;
padding: 7px 10px;
}
.pagination__link_end a:hover {
border: 1px solid var(--text-muted);
}
.pagination__link_end a {
color: #539bf5;
text-decoration: none;
padding: 7px 10px;
}
.pagination__link_end a:hover {
border: 1px solid var(--text-muted);
}
.pagination__link--active a {
color: #fff;
background: #316dca;
}
.pagination__link--active a:hover {
border: none !important;
}
.pagination__link--active a {
color: #fff;
background: #316dca;
}
.pagination__link--active a:hover {
border: none !important;
}
.pagination__link--disabled,
.pagination__link--disabled a {
color: #545d68 !important;
text-decoration: none !important;
border: none !important;
cursor: default;
}
.pagination__link--disabled,
.pagination__link--disabled a {
color: #545d68 !important;
text-decoration: none !important;
border: none !important;
cursor: default;
}
`;
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
export type PullRequest = {
title: string;
number: string;
created_at: string;
user: {
login: string;
};
state: 'open' | 'closed' | 'merged';
messageCount: number;
isMerged?: boolean;
merged_at: string | null;
repository_url: string;
comments: any;
title: string;
number: string;
created_at: string;
user: {
login: string;
};
state: 'open' | 'closed' | 'merged';
messageCount: number;
isMerged?: boolean;
merged_at: string | null;
repository_url: string;
comments: any;
};

export type PullRequests = {
total_count: number;
items: PullRequest[];
total_count: number;
items: PullRequest[];
};
Loading

0 comments on commit deccd45

Please sign in to comment.