Skip to content

Commit

Permalink
attachment fixed (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
AryanGKulkarni authored Sep 28, 2024
1 parent 2a66273 commit 732d711
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/components/JAF/JAF.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ function JAF() {
job: {
role: values.role,
description: values.description,
//attachment: values.attachment,//file
attachment: values.attachment,//file
skills: values.skills,
location: values.location,
noOfVacancies: values.noOfVacancies,
Expand Down Expand Up @@ -154,7 +154,7 @@ function JAF() {
contact: "+91 " + values.phoneNumber,
landline: values.landline,
},
//attachment: values.attachment,//file
attachment: values.attachment,//file
others: values.jobOthers,
skills: values.skills,
location: values.location,
Expand Down
55 changes: 36 additions & 19 deletions src/components/JAF/JobDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,28 @@ const ReactQuill = dynamic(() => import('react-quill'), { ssr: false });

const { TextArea } = Input;

const props: UploadProps = {
name: "attachment",
action: "https://run.mocky.io/v3/435e224c-44fb-4773-9faf-380c5e6a2188",
headers: {
authorization: "authorization-text",
},
onChange(info: any) {
if (info.file.status !== "uploading") {
console.log(info.file, info.fileList);
}
if (info.file.status === "done") {
message.success(`${info.file.name} file uploaded successfully`);
} else if (info.file.status === "error") {
message.error(`${info.file.name} file upload failed.`);
}
},
};

type StepProps = {
errors: FormikErrors<FormikValues>;
values: FormikValues;
handleChange: FormikHandlers["handleChange"];
setFieldValue: (field: string, value: any) => void;
};

const getBase64 = (file: File): Promise<string> => {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result as string);
reader.onerror = (error) => reject(error);
});
};

const JobDetails = ({ errors, values, handleChange, setFieldValue }: StepProps) => {
const [form] = Form.useForm();

const [skills, setSkills] = useState([]);
const [skillInput, setSkillInput] = useState("");
const [fileList, setFileList] = useState([]);

const [testType, setTestType] = useState([]);
const [seasonType, setSeasonType] = useState("");
Expand All @@ -70,6 +62,28 @@ const JobDetails = ({ errors, values, handleChange, setFieldValue }: StepProps)
setSkillInput(e.target.value);
};

const handleFileChange = async (info: any) => {
if (info.file.status !== 'uploading') {
console.log('Uploading:', info.file, info.fileList);
}
if (info.file.status === "done") {
const file = info.file.originFileObj;
try {
const base64String = await getBase64(file);
console.log(`Base64 string length: ${base64String.length}`);
console.log(`Base64 string: ${base64String.substring(0, 50)}...`);
setFieldValue("attachment", base64String);
message.success(`${info.file.name} file uploaded successfully`);
} catch (error) {
message.error("File conversion to Base64 failed.");
}
} else if (info.file.status === "error") {
message.error(`${info.file.name} file upload failed.`);
}

setFileList(info.fileList);
};

const addSkill = () => {
if (skillInput) {
const updatedSkills = [...skills, skillInput];
Expand Down Expand Up @@ -305,7 +319,10 @@ const JobDetails = ({ errors, values, handleChange, setFieldValue }: StepProps)
<Row gutter={24}>
<Col span={12}>
<Form.Item label="Attachments">
<Upload {...props}>
<Upload
fileList={fileList}
onChange={handleFileChange}
>
<Button icon={<UploadOutlined />}>Click to Upload</Button>
</Upload>
</Form.Item>
Expand Down

0 comments on commit 732d711

Please sign in to comment.