Skip to content

Commit 4206833

Browse files
committed
refactor(ts): src/ui/services/repo
1 parent fbe4ebc commit 4206833

File tree

3 files changed

+142
-133
lines changed

3 files changed

+142
-133
lines changed

src/ui/services/repo.js

Lines changed: 0 additions & 132 deletions
This file was deleted.

src/ui/services/repo.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
import axios from 'axios';
2+
import { getAxiosConfig, processAuthError } from './auth.js';
3+
import { API_BASE } from '../apiBase';
4+
import { RepositoryData, RepositoryDataWithId } from '../views/RepoList/Components/NewRepo';
5+
6+
const canAddUser = (repoId: string, user: string, action: string) => {
7+
const url = new URL(`${API_BASE}/repo/${repoId}`);
8+
return axios
9+
.get(url.toString(), getAxiosConfig())
10+
.then((response) => {
11+
const data = response.data;
12+
if (action === 'authorise') {
13+
return !data.users.canAuthorise.includes(user);
14+
} else {
15+
return !data.users.canPush.includes(user);
16+
}
17+
})
18+
.catch((error: any) => {
19+
throw error;
20+
});
21+
};
22+
23+
class DupUserValidationError extends Error {
24+
constructor(message: string) {
25+
super(message);
26+
this.name = 'The user already has this role...';
27+
}
28+
}
29+
30+
const getRepos = async (
31+
setIsLoading: (isLoading: boolean) => void,
32+
setData: (data: any) => void,
33+
setAuth: (auth: boolean) => void,
34+
setIsError: (isError: boolean) => void,
35+
setErrorMessage: (errorMessage: string) => void,
36+
query: Record<string, boolean> = {},
37+
): Promise<void> => {
38+
const url = new URL(`${API_BASE}/repo`);
39+
url.search = new URLSearchParams(query as any).toString();
40+
setIsLoading(true);
41+
await axios(url.toString(), getAxiosConfig())
42+
.then((response) => {
43+
const sortedRepos = response.data.sort((a: RepositoryData, b: RepositoryData) =>
44+
a.name.localeCompare(b.name),
45+
);
46+
setData(sortedRepos);
47+
})
48+
.catch((error: any) => {
49+
setIsError(true);
50+
if (error.response && error.response.status === 401) {
51+
setAuth(false);
52+
setErrorMessage(processAuthError(error));
53+
} else {
54+
setErrorMessage(`Error fetching repos: ${error.response.data.message}`);
55+
}
56+
})
57+
.finally(() => {
58+
setIsLoading(false);
59+
});
60+
};
61+
62+
const getRepo = async (
63+
setIsLoading: (isLoading: boolean) => void,
64+
setData: (data: any) => void,
65+
setAuth: (auth: boolean) => void,
66+
setIsError: (isError: boolean) => void,
67+
id: string,
68+
): Promise<void> => {
69+
const url = new URL(`${API_BASE}/repo/${id}`);
70+
setIsLoading(true);
71+
await axios(url.toString(), getAxiosConfig())
72+
.then((response) => {
73+
const data = response.data;
74+
setData(data);
75+
})
76+
.catch((error: any) => {
77+
if (error.response && error.response.status === 401) {
78+
setAuth(false);
79+
} else {
80+
setIsError(true);
81+
}
82+
})
83+
.finally(() => {
84+
setIsLoading(false);
85+
});
86+
};
87+
88+
const addRepo = async (
89+
data: RepositoryData,
90+
): Promise<{ success: boolean; message?: string; repo: RepositoryDataWithId | null }> => {
91+
const url = new URL(`${API_BASE}/repo`);
92+
93+
try {
94+
const response = await axios.post(url.toString(), data, getAxiosConfig());
95+
return {
96+
success: true,
97+
repo: response.data,
98+
};
99+
} catch (error: any) {
100+
return {
101+
success: false,
102+
message: error.response?.data?.message || error.message,
103+
repo: null,
104+
};
105+
}
106+
};
107+
108+
const addUser = async (repoId: string, user: string, action: string): Promise<void> => {
109+
const canAdd = await canAddUser(repoId, user, action);
110+
if (canAdd) {
111+
const url = new URL(`${API_BASE}/repo/${repoId}/user/${action}`);
112+
const data = { username: user };
113+
await axios.patch(url.toString(), data, getAxiosConfig()).catch((error: any) => {
114+
console.log(error.response.data.message);
115+
throw error;
116+
});
117+
} else {
118+
console.log('Duplicate user can not be added');
119+
throw new DupUserValidationError('Duplicate user can not be added');
120+
}
121+
};
122+
123+
const deleteUser = async (user: string, repoId: string, action: string): Promise<void> => {
124+
const url = new URL(`${API_BASE}/repo/${repoId}/user/${action}/${user}`);
125+
126+
await axios.delete(url.toString(), getAxiosConfig()).catch((error: any) => {
127+
console.log(error.response.data.message);
128+
throw error;
129+
});
130+
};
131+
132+
const deleteRepo = async (repoId: string): Promise<void> => {
133+
const url = new URL(`${API_BASE}/repo/${repoId}/delete`);
134+
135+
await axios.delete(url.toString(), getAxiosConfig()).catch((error: any) => {
136+
console.log(error.response.data.message);
137+
throw error;
138+
});
139+
};
140+
141+
export { addUser, deleteUser, getRepos, getRepo, addRepo, deleteRepo };

src/ui/views/RepoList/Components/NewRepo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ const AddRepositoryDialog: React.FC<AddRepositoryDialogProps> = ({ open, onClose
9696
}
9797

9898
const result = await addRepo(data);
99-
if (result.success) {
99+
if (result.success && result.repo) {
100100
handleSuccess(result.repo);
101101
handleClose();
102102
} else {

0 commit comments

Comments
 (0)