-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
218 lines (196 loc) · 7.87 KB
/
App.js
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import './App.css';
import { useState } from 'react';
// Component imports
import SearchBar from './components/SearchBar';
import GitHubResults from './components/GitHubResults';
import Header from './components/Header';
import GitLabResults from './components/GitLabResults';
// Boostrap imports
import ListGroup from 'react-bootstrap/ListGroup';
import ListGroupItem from 'react-bootstrap/cjs/ListGroupItem';
function App() {
// --------------- Set State ---------------
// GitHub and GitLab user profiles (all data from server).
const [userProfiles, setUserProfiles] = useState();
// GitHub profile and repo data.
const [gitHubAvatar, setGitHubAvatar] = useState();
const [gitHubName, setGitHubName] = useState();
const [gitHubProfileLink, setGitHubProfileLink] = useState();
const [gitHubBio, setGitHubBio] = useState();
const [gitHubRepoData, setGitHubRepoData] = useState();
// GitLab profile and repo data.
const [gitLabAvatar, setGitLabAvatar] = useState();
const [gitLabName, setGitLabName] = useState();
const [gitLabProfileLink, setGitLabProfileLink] = useState();
const [gitLabRepoData, setGitLabRepoData] = useState();
// General states.
const [searchName, setSearchName] = useState("");
const [option, setOption] = useState();
const [loading, setLoading] = useState(false);
const [loadingRepos, setLoadingRepos] = useState(false);
// --------------- Get User Profiles ---------------
async function getUser() {
setLoading(true);
// Get all GitHub and GitLab profiles data for inputted username.
let response = await fetch(`/searchUser/${searchName}`);
let profilesData = await response.json();
// Set userProfiles state to store GitHub and GitLab profiles data.
setUserProfiles(profilesData.data);
// set GitHub profile details.
setGitHubName(profilesData.data[0].gitHubUserName);
setGitHubProfileLink(profilesData.data[0].gitHubProfileLink);
setGitHubAvatar(profilesData.data[0].gitHubAvatar);
setGitHubBio(profilesData.data[0].gitHubBio);
// set GitLab profile details.
setGitLabName(profilesData.data[1].gitLabUserName);
setGitLabProfileLink(profilesData.data[1].gitLabProfileLink);
setGitLabAvatar(profilesData.data[1].gitLabAvatar);
setLoading(false);
// Call function to get repos for the inputted username.
getRepos(searchName);
}
// --------------- Get User Repos ---------------
async function getRepos(searchName) {
setLoadingRepos(true);
// Get all GitHub and GitLab repo data for inputted username.
let response = await fetch(`/searchUser/${searchName}/repos`);
let repoData = await response.json();
// create seperate repoLists to store data for GitHub and GitLab repos.
let gitHubRepoList = repoData.data[0].gitHubProjects;
let gitLabRepoList = repoData.data[1].gitLabProjects;
// Loop through gitHubRepoList:
for (let i = 0; i < gitHubRepoList.length; i++) {
// If the gitHubRepoList item's repoFound value is true:
if (gitHubRepoList[i].repoFound === true) {
// Map through gitHubRepoList and the commitsMessages for that repo and return relevant details.
let gitHubRepoInfo = gitHubRepoList.map((repo, index) => {
return (
<ListGroup key={index} className="text-start">
<ListGroup.Item className="repoList">
<div className="repoTitle">{repo.repoName}</div>
<div className="pb-2">Created on: {repo.dateCreated}</div>
<div className="repoDescription pb-2">
Description:
<br></br>
{repo.repoDescription}
</div>
<div>
<span className="latestCommitsHeading">Latest Commit Messages:</span>
<br></br>
{repo.commitMessages.map((message, index) => {
return (
<ListGroup.Item className="repoCommits" key={index}>
{message.commitDate}:
<br></br>
{message.commitMsg}
</ListGroup.Item>
)
})}
</div>
</ListGroup.Item>
</ListGroup>
)
})
// Set gitHubRepoData to value of gitHubRepoInfo.
setGitHubRepoData(gitHubRepoInfo);
} else {
// Else, create noReposMsg React element and set gitHubRepoData to this value.
let noReposMsg = (
<ListGroup>
<ListGroupItem>No GitHub repos found for this user </ListGroupItem>
</ListGroup>
);
setGitHubRepoData(noReposMsg);
}
}
// Loop through gitLabRepoList:
for (let i = 0; i < gitLabRepoList.length; i++) {
// If the gitLabProjects' repoFound value is true:
if (gitLabRepoList[i].repoFound === true) {
// Map through gitLabRepoList and the commitsMessages for that repo and return relevant details.
let gitLabRepoInfo = gitLabRepoList.map((repo, index) => {
return (
<ListGroup key={index} className="text-start">
<ListGroup.Item className="repoList">
<div className="repoTitle">{repo.repoName}</div>
<div className="pb-2">Created on: {repo.dateCreated}</div>
<div className="repoDescription pb-2">
Description:
<br></br>
{repo.repoDescription}
</div>
<div>
<span className="latestCommitsHeading">Latest Commit Messages:</span>
<br></br>
{repo.commitMessages.map((message, index) => {
return (
<ListGroup.Item className="repoCommits" key={index}>
{message.commitDate}:
<br></br>
{message.commitMsg}
</ListGroup.Item>
)
})}
</div>
</ListGroup.Item>
</ListGroup>
)
})
// Set gitLabRepoData to value of gitLabRepoInfo.
setGitLabRepoData(gitLabRepoInfo);
} else {
// Else, create noReposMsg React element and set gitHubRepoData to this value.
let noReposMsg = (
<ListGroup>
<ListGroupItem>No GitLab repos found for this user</ListGroupItem>
</ListGroup>
);
setGitLabRepoData(noReposMsg);
}
}
// setLoading(false);
setLoadingRepos(false);
}
// Handle change function for SearchBar component's onChange value.
const handleChange = (e) => {
setSearchName(e.target.value);
}
// Handle option function for SearchBar component's onClick value.
const handleOption = (e) => {
setOption(e);
}
return (
<div className="App">
<Header />
<div className="App justify-content-center align-items-center d-flex flex-column">
<SearchBar
getUser = {getUser}
handleChange = {handleChange}
searchName = {searchName}
option = {option}
handleOption = {handleOption}
/>
{ loading ? <p>Loading...</p> : option ? <GitLabResults
userProfiles = {userProfiles}
gitLabAvatar = {gitLabAvatar}
gitLabName = {gitLabName}
gitLabProfileLink = {gitLabProfileLink}
searchName = {searchName}
gitLabRepoData = {gitLabRepoData}
loadingRepos = {loadingRepos}
/> :
<GitHubResults
userProfiles = {userProfiles}
gitHubAvatar = {gitHubAvatar}
gitHubName = {gitHubName}
gitHubProfileLink = {gitHubProfileLink}
gitHubBio = {gitHubBio}
searchName = {searchName}
gitHubRepoData = {gitHubRepoData}
loadingRepos = {loadingRepos}
/> }
</div>
</div>
);
}
export default App;