|
|
@@ -1,4 +1,4 @@
|
|
|
-import React, { useState, useContext, useEffect } from "react";
|
|
|
+import React, { useState, useContext, useEffect, useRef } from "react";
|
|
|
import styled from "styled-components";
|
|
|
import github from "assets/github.png";
|
|
|
|
|
|
@@ -8,6 +8,7 @@ import { Context } from "shared/Context";
|
|
|
|
|
|
import Loading from "../Loading";
|
|
|
import Button from "../Button";
|
|
|
+import { AxiosResponse } from "axios";
|
|
|
|
|
|
type Props = {
|
|
|
actionConfig: ActionConfigType | null;
|
|
|
@@ -26,122 +27,137 @@ const RepoList = ({
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
const [error, setError] = useState(false);
|
|
|
const [searchFilter, setSearchFilter] = useState("");
|
|
|
+ const gitReposIDs = useRef<[number]>(null);
|
|
|
const { currentProject } = useContext(Context);
|
|
|
|
|
|
// TODO: Try to unhook before unmount
|
|
|
useEffect(() => {
|
|
|
- // Get repos
|
|
|
+ // load git repo ids, only need to do this once through component lifecycle
|
|
|
+
|
|
|
if (!userId && userId !== 0) {
|
|
|
api
|
|
|
.getGitRepos("<token>", {}, { project_id: currentProject.id })
|
|
|
.then(async (res) => {
|
|
|
- if (res.data.length == 0) {
|
|
|
- setLoading(false);
|
|
|
- setError(false);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- var allRepos: any = [];
|
|
|
- var errors: any = [];
|
|
|
-
|
|
|
- console.log(res);
|
|
|
-
|
|
|
- var promises = res.data.map((gitrepo: any, id: number) => {
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
- api
|
|
|
- .getGitRepoList(
|
|
|
- "<token>",
|
|
|
- {},
|
|
|
- { project_id: currentProject.id, git_repo_id: gitrepo.id }
|
|
|
- )
|
|
|
- .then((res) => {
|
|
|
- res.data.forEach((repo: any, id: number) => {
|
|
|
- repo.GHRepoID = gitrepo.id;
|
|
|
- });
|
|
|
-
|
|
|
- resolve(res.data);
|
|
|
- })
|
|
|
- .catch((err) => {
|
|
|
- errors.push(err);
|
|
|
- resolve([]);
|
|
|
- });
|
|
|
- });
|
|
|
- });
|
|
|
-
|
|
|
- var sepRepos = await Promise.all(promises);
|
|
|
-
|
|
|
- allRepos = [].concat.apply([], sepRepos);
|
|
|
-
|
|
|
- // remove duplicates based on name
|
|
|
- allRepos = allRepos.filter((repo: any, index: number, self: any) => {
|
|
|
- var keep =
|
|
|
- index ===
|
|
|
- self.findIndex((_repo: any) => {
|
|
|
- return repo.FullName === _repo.FullName;
|
|
|
- });
|
|
|
-
|
|
|
- return keep;
|
|
|
- });
|
|
|
-
|
|
|
- // sort repos based on name
|
|
|
- allRepos.sort((a: any, b: any) => {
|
|
|
- if (a.FullName < b.FullName) {
|
|
|
- return -1;
|
|
|
- } else if (a.FullName > b.FullName) {
|
|
|
- return 1;
|
|
|
- } else {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- });
|
|
|
-
|
|
|
- if (allRepos.length == 0 && errors.length > 0) {
|
|
|
- setLoading(false);
|
|
|
- setError(true);
|
|
|
- } else {
|
|
|
- setRepos(allRepos);
|
|
|
- setLoading(false);
|
|
|
- setError(false);
|
|
|
- }
|
|
|
+ gitReposIDs.current = res.data.map((gitrepo: any) => gitrepo.id);
|
|
|
})
|
|
|
.catch((_) => {
|
|
|
setLoading(false);
|
|
|
setError(true);
|
|
|
});
|
|
|
} else {
|
|
|
- // ??? wouldn't this always be an undefined request?
|
|
|
- let grid = userId;
|
|
|
-
|
|
|
- api
|
|
|
- .getGitRepoList(
|
|
|
- "<token>",
|
|
|
- {},
|
|
|
- { project_id: currentProject.id, git_repo_id: grid }
|
|
|
- )
|
|
|
- .then((res) => {
|
|
|
- var repos: any = res.data;
|
|
|
-
|
|
|
- repos.forEach((repo: any, id: number) => {
|
|
|
- repo.GHRepoID = grid;
|
|
|
- });
|
|
|
-
|
|
|
- repos.sort((a: any, b: any) => {
|
|
|
- if (a.FullName < b.FullName) {
|
|
|
- return -1;
|
|
|
- } else if (a.FullName > b.FullName) {
|
|
|
- return 1;
|
|
|
- } else {
|
|
|
- return 0;
|
|
|
- }
|
|
|
- });
|
|
|
- setRepos(repos);
|
|
|
- setLoading(false);
|
|
|
- setError(false);
|
|
|
- })
|
|
|
- .catch((_) => {
|
|
|
- setLoading(false);
|
|
|
- setError(true);
|
|
|
- });
|
|
|
+ gitReposIDs.current = [userId];
|
|
|
}
|
|
|
+
|
|
|
+ // if (!userId && userId !== 0) {
|
|
|
+ // api
|
|
|
+ // .getGitRepos("<token>", {}, { project_id: currentProject.id })
|
|
|
+ // .then(async (res) => {
|
|
|
+ // if (res.data.length == 0) {
|
|
|
+ // setLoading(false);
|
|
|
+ // setError(false);
|
|
|
+ // return;
|
|
|
+ // }
|
|
|
+ //
|
|
|
+ // var allRepos: any = [];
|
|
|
+ // var errors: any = [];
|
|
|
+ //
|
|
|
+ // console.log(res);
|
|
|
+ //
|
|
|
+ // var promises = res.data.map((gitrepo: any, id: number) => {
|
|
|
+ // return new Promise((resolve, reject) => {
|
|
|
+ // api
|
|
|
+ // .getGitRepoList(
|
|
|
+ // "<token>",
|
|
|
+ // {},
|
|
|
+ // { project_id: currentProject.id, git_repo_id: gitrepo.id }
|
|
|
+ // )
|
|
|
+ // .then((res) => {
|
|
|
+ // res.data.forEach((repo: any, id: number) => {
|
|
|
+ // repo.GHRepoID = gitrepo.id;
|
|
|
+ // });
|
|
|
+ //
|
|
|
+ // resolve(res.data);
|
|
|
+ // })
|
|
|
+ // .catch((err) => {
|
|
|
+ // errors.push(err);
|
|
|
+ // resolve([]);
|
|
|
+ // });
|
|
|
+ // });
|
|
|
+ // });
|
|
|
+ //
|
|
|
+ // var sepRepos = await Promise.all(promises);
|
|
|
+ //
|
|
|
+ // allRepos = [].concat.apply([], sepRepos);
|
|
|
+ //
|
|
|
+ // // remove duplicates based on name
|
|
|
+ // allRepos = allRepos.filter((repo: any, index: number, self: any) => {
|
|
|
+ // var keep =
|
|
|
+ // index ===
|
|
|
+ // self.findIndex((_repo: any) => {
|
|
|
+ // return repo.FullName === _repo.FullName;
|
|
|
+ // });
|
|
|
+ //
|
|
|
+ // return keep;
|
|
|
+ // });
|
|
|
+ //
|
|
|
+ // // sort repos based on name
|
|
|
+ // allRepos.sort((a: any, b: any) => {
|
|
|
+ // if (a.FullName < b.FullName) {
|
|
|
+ // return -1;
|
|
|
+ // } else if (a.FullName > b.FullName) {
|
|
|
+ // return 1;
|
|
|
+ // } else {
|
|
|
+ // return 0;
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ //
|
|
|
+ // if (allRepos.length == 0 && errors.length > 0) {
|
|
|
+ // setLoading(false);
|
|
|
+ // setError(true);
|
|
|
+ // } else {
|
|
|
+ // setRepos(allRepos);
|
|
|
+ // setLoading(false);
|
|
|
+ // setError(false);
|
|
|
+ // }
|
|
|
+ // })
|
|
|
+ // .catch((_) => {
|
|
|
+ // setLoading(false);
|
|
|
+ // setError(true);
|
|
|
+ // });
|
|
|
+ // } else {
|
|
|
+ // let grid = userId;
|
|
|
+ //
|
|
|
+ // api
|
|
|
+ // .getGitRepoList(
|
|
|
+ // "<token>",
|
|
|
+ // {},
|
|
|
+ // { project_id: currentProject.id, git_repo_id: grid }
|
|
|
+ // )
|
|
|
+ // .then((res) => {
|
|
|
+ // var repos: any = res.data;
|
|
|
+ //
|
|
|
+ // repos.forEach((repo: any, id: number) => {
|
|
|
+ // repo.GHRepoID = grid;
|
|
|
+ // });
|
|
|
+ //
|
|
|
+ // repos.sort((a: any, b: any) => {
|
|
|
+ // if (a.FullName < b.FullName) {
|
|
|
+ // return -1;
|
|
|
+ // } else if (a.FullName > b.FullName) {
|
|
|
+ // return 1;
|
|
|
+ // } else {
|
|
|
+ // return 0;
|
|
|
+ // }
|
|
|
+ // });
|
|
|
+ // setRepos(repos);
|
|
|
+ // setLoading(false);
|
|
|
+ // setError(false);
|
|
|
+ // })
|
|
|
+ // .catch((_) => {
|
|
|
+ // setLoading(false);
|
|
|
+ // setError(true);
|
|
|
+ // });
|
|
|
+ // }
|
|
|
}, []);
|
|
|
|
|
|
const setRepo = (x: RepoType) => {
|
|
|
@@ -194,6 +210,16 @@ const RepoList = ({
|
|
|
});
|
|
|
};
|
|
|
|
|
|
+ const updateSearchResults = () => {
|
|
|
+ setLoading(true);
|
|
|
+
|
|
|
+ // api.
|
|
|
+ // .searchGitRepos("<token>", {}, { project_id: currentProject.id })
|
|
|
+ // .then((res: AxiosResponse) => {
|
|
|
+ // console.log(res);
|
|
|
+ // });
|
|
|
+ };
|
|
|
+
|
|
|
const renderExpanded = () => {
|
|
|
if (readOnly) {
|
|
|
return <ExpandedWrapperAlt>{renderRepoList()}</ExpandedWrapperAlt>;
|
|
|
@@ -211,7 +237,12 @@ const RepoList = ({
|
|
|
placeholder="Search repos..."
|
|
|
/>
|
|
|
</SearchBar>
|
|
|
- <Button>Search</Button>
|
|
|
+ <Button
|
|
|
+ onClick={updateSearchResults}
|
|
|
+ disabled={!searchFilter || !/\S/.test(searchFilter) || loading}
|
|
|
+ >
|
|
|
+ Search
|
|
|
+ </Button>
|
|
|
</SearchRow>
|
|
|
<ExpandedWrapper>
|
|
|
<ExpandedWrapper>{renderRepoList()}</ExpandedWrapper>
|