Skip to content

Commit

Permalink
Add PDF to questionnaire converter
Browse files Browse the repository at this point in the history
  • Loading branch information
atuonufure committed Dec 16, 2023
1 parent 26ca7ac commit 8cb7b29
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/containers/QuestionnaireBuilder/PromptForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Button, Form, Input, Timeline } from 'antd';
import { FormProps } from 'antd/lib/form';

import s from './QuestionnaireBuilder.module.scss';
import { QuestionnaireFromPdfButton } from './QuestionnaireFromPdfButton';

const { TextArea } = Input;

Expand Down Expand Up @@ -72,9 +73,12 @@ export function PromptForm(props: Props) {
<Form.Item name="prompt" label={t`Describe requirements to a questionnaire`}>
<TextArea rows={5} disabled={disabled} />
</Form.Item>
<Form.Item>
<Button htmlType="submit" disabled={disabled}>{t`Submit`}</Button>
</Form.Item>
<div className={s.submitButtons}>
<Form.Item>
<Button htmlType="submit" disabled={disabled}>{t`Submit`}</Button>
</Form.Item>
<QuestionnaireFromPdfButton onSubmit={onSubmit} disabled={disabled} />
</div>
<div className={s.prompts}>
<Timeline items={items} />
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@
display: flex;
flex-direction: column;
}

.submitButtons {
display: flex;
justify-content: space-between;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { t } from '@lingui/macro';
import { Button } from 'antd';
import axios from 'axios';
import { useRef } from 'react';

interface Props {
onSubmit: (prompt: string) => Promise<any>;
disabled?: boolean;
}

const context = `Questions might be type choice if answers are provided or text if no provided answers.
Keep original question numeration if possible in the text attribute.
Create from pdf:
{PDF_TEXT}`;

export function QuestionnaireFromPdfButton({ onSubmit, disabled }: Props) {
const fileInputRef = useRef<HTMLInputElement>(null);

const uploadPdf = async () => {
if (fileInputRef.current?.files && fileInputRef.current.files[0]) {
console.log('Uploading Questionnaire');

const file = fileInputRef.current.files[0];
const url = 'http://localhost:8081/';

const formData = new FormData();
formData.append('file', file);

try {
const response = await axios.post(url, formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
});

await onSubmit(context + response.data);
} catch (error) {
console.error(error);
}
} else {
console.log('Please select a file');
}
};

const handleButtonClick = () => {
fileInputRef.current?.click();
};

return (
<div>
<input type="file" ref={fileInputRef} style={{ display: 'none' }} accept=".pdf" onChange={uploadPdf} />
<Button onClick={handleButtonClick} disabled={disabled}>{t`Upload PDF`}</Button>
</div>
);
}

0 comments on commit 8cb7b29

Please sign in to comment.