Skip to content

Commit

Permalink
Merge pull request #46 from african-pathogen-archive/studies-2
Browse files Browse the repository at this point in the history
feat: studies
  • Loading branch information
vuyaniShabangu authored Jul 30, 2024
2 parents 69873e4 + 5fcdbf0 commit 3c89c1c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
16 changes: 10 additions & 6 deletions components/AddStudy/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import { authorizedApiRequest } from '@/global/utils/api';

const { TextArea } = Input;

const AddStudy: FC = () => {
type Props = {
refetchStudies: Function;
};

const AddStudy: FC<Props> = ({ refetchStudies }) => {
const [isModalOpen, setIsModalOpen] = useState(false);
const [study, setStudy] = useState('');
const [name, setName] = useState('');
const [description, setDescription] = useState('');
const [loading, setLoading] = useState<boolean>(false);

Expand All @@ -33,15 +37,15 @@ const AddStudy: FC = () => {
return;
}
authorizedApiRequest(HttpMethods.POST, API_ROUTES_PATHS.STUDIES, {
study: study,
name: name,
project_id: projectId,
description: description,
})
.then((data) => {
console.log(data);
setLoading(false);
toast(ToastType.SUCCESS, 'Study was successfully created');
handleCancel();
refetchStudies();
})
.catch((error) => {
console.log(error);
Expand Down Expand Up @@ -80,8 +84,8 @@ const AddStudy: FC = () => {
rules={[{ required: true, message: 'Study ID is required' }]}
>
<Input
value={study}
onChange={(e) => setStudy(e.target.value)}
value={name}
onChange={(e) => setName(e.target.value)}
placeholder="Study ID"
/>
</Form.Item>
Expand Down
24 changes: 16 additions & 8 deletions components/StudiesTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ type Props = {

interface Study {
key: string;
description: string;
id: string;
description?: string;
numberOfSamples: number;
}

function convertToTableData(responseData: []): Study[] {
return responseData.map((element: any) => {
return {
key: element.id,
id: element?.study,
description: element?.description,
numberOfSamples: element?.number_of_samples,
};
Expand All @@ -39,34 +41,40 @@ const Studies: FC<Props> = ({ isGroupMember }) => {
const projectId = router['project-id'];

useEffect(() => {
getStudies();
}, []);

const getStudies = () => {
if (projectId) {
authorizedApiRequest(HttpMethods.GET, `${API_ROUTES_PATHS.PROJECTS}/${projectId}/studies`)
.then((data) => {
setLoading(false);
setStudies(convertToTableData(data));
console.log(data);
})
.catch((error) => {
setLoading(false);
console.log(error);
});
}
}, []);
};

const truncateDescription = (value: string): string => {
return value.length > 50 ? value.split('').slice(0, 50).join('').concat('...') : value;
const truncateDescription = (value?: string): string => {
if (!value) {
return '';
}
return value?.length > 50 ? value?.split('').slice(0, 50).join('').concat('...') : value;
};

const columns: ColumnsType<Study> = [
{
title: 'Study ID',
dataIndex: 'key',
dataIndex: 'id',
key: 'study ID',
},
{
title: 'Description',
key: 'description',
render: (_, record) => <Text>{truncateDescription(record.description)}</Text>,
render: (_, record) => <Text>{truncateDescription(record?.description)}</Text>,
},
{
title: 'No. of samples',
Expand Down Expand Up @@ -95,7 +103,7 @@ const Studies: FC<Props> = ({ isGroupMember }) => {
title={() => (
<div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
<Title level={5}>Studies</Title>
{isGroupMember && <AddStudy />}
{isGroupMember && <AddStudy refetchStudies={getStudies} />}
</div>
)}
/>
Expand Down
2 changes: 1 addition & 1 deletion global/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export enum INTERNAL_PATHS {
LOGIN = '/login',
POLICIES = '/policies',
RELEASES = '/releases',
SUBMISSION = '/submission/holderSubmission',
SUBMISSION = '/submission/submission',
STUDIES = '/studies',
TEAM = '/team',
USER = '/user',
Expand Down

0 comments on commit 3c89c1c

Please sign in to comment.