refactor: usersControl

This commit is contained in:
Takeshi Kimata
2024-07-18 10:39:07 +09:00
parent 7e05eda259
commit aab9f91156
8 changed files with 118 additions and 115 deletions

View File

@@ -5,7 +5,7 @@ import { Button, Input, Modal, ModalContent, ModalHeader, ModalBody, ModalFooter
import { SettingsMessages } from '@/types/settings';
import { TokenContext } from '@/utils/TokenProvider';
import { UserType } from '@/types/user';
import { searchUsers } from './membersControl';
import { searchUsers } from '@/utils/usersControl';
import CandidatesTable from './CandidatesTable';
type Props = {
@@ -32,7 +32,7 @@ export default function AddMemberDialog({ isOpen, projectId, onCancel, onAddMemb
}
try {
const data = await searchUsers(context.token.access_token, projectId, searchText);
const data = await searchUsers(context.token.access_token, Number(projectId), searchText);
setCandidates(data);
} catch (error: any) {
console.error('Error in effect:', error.message);

View File

@@ -24,29 +24,6 @@ async function fetchProjectMembers(jwt: string, projectId: string) {
}
}
async function searchUsers(jwt: string, projectId: string, searchText: string) {
const fetchOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${jwt}`,
},
};
const url = `${apiServer}/users/find?projectId=${projectId}&search=${searchText}`;
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error: any) {
console.error('Error fetching data:', error.message);
}
}
async function addMember(jwt: string, userId: string, projectId: string) {
const fetchOptions = {
method: 'POST',
@@ -114,4 +91,4 @@ async function updateMember(jwt: string, userId: string, projectId: string, role
}
}
export { fetchProjectMembers, searchUsers, addMember, deleteMember, updateMember };
export { fetchProjectMembers, addMember, deleteMember, updateMember };

View File

@@ -11,9 +11,8 @@ import { ProjectType } from '@/types/project';
import DeleteConfirmDialog from '@/components/DeleteConfirmDialog';
import { useRouter } from '@/src/navigation';
import ProjectDialog from '@/components/ProjectDialog';
import Config from '@/config/config';
import { UserType } from '@/types/user';
const apiServer = Config.apiServer;
import { findUser } from '@/utils/usersControl';
type Props = {
projectId: string;
@@ -44,29 +43,6 @@ export default function SettingsPage({ projectId, messages, locale }: Props) {
username: '',
});
async function fetchUser(jwt: string, userId: string) {
const fetchOptions = {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${jwt}`,
},
};
const url = `${apiServer}/users/${userId}`;
try {
const response = await fetch(url, fetchOptions);
if (!response.ok) {
throw new Error(`HTTP error! Status: ${response.status}`);
}
const data = await response.json();
return data;
} catch (error: any) {
console.error('Error fetching data:', error.message);
}
}
useEffect(() => {
async function fetchDataEffect() {
if (!context.isSignedIn()) {
@@ -78,7 +54,7 @@ export default function SettingsPage({ projectId, messages, locale }: Props) {
setProject(data);
if (data.userId) {
const ownerData = await fetchUser(context.token.access_token, data.userId);
const ownerData = await findUser(context.token.access_token, data.userId);
setOwner(ownerData);
} else {
console.error('failed to get project owner id');