Skip to content

Commit

Permalink
functions for uploading docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuang28 committed Sep 2, 2024
1 parent 4bbeee2 commit ff29a50
Showing 1 changed file with 42 additions and 6 deletions.
48 changes: 42 additions & 6 deletions components/records/Header.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
import React from 'react';
import React, { useRef } from 'react';
import axios from 'axios';
import moment from 'moment';
import { ChevronDownIcon } from '@heroicons/react/24/outline';
import { CLOUDINARY_URL } from '@/utils/constants';
import { Button } from '../TextComponents';

export function Header({ patient, visits, handleVisitChange }) {
const fileInputRef = useRef(null);

function uploadDocument() {
//function where on click triggers file selection
//then send to backend using axios of fetch
console.log('Upload document');
fileInputRef.current.click(); //trigger file input click
}

function handleFileChange(event) {
console.log('event change');
const file = event.target.files[0];
if (file) {
const formData = new FormData();
formData.append('file', file);

axios

This comment has been minimized.

Copy link
@AngPengXuan

AngPengXuan Sep 2, 2024

Contributor

use axioInstance instead, example here, the file is here

.post('http://127.0.0.1:8000/upload/', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
})
.then(response => {
console.log('File uploaded successfully', response.data);

This comment has been minimized.

Copy link
@AngPengXuan

AngPengXuan Sep 2, 2024

Contributor

use toast so the user have feedback that file upload is successful, example here

})
.catch(error => {
console.error('Error uploading file:', error);

This comment has been minimized.

Copy link
@AngPengXuan

AngPengXuan Sep 2, 2024

Contributor

Similarly use toast

});
}
}

function viewDocument() {
Expand Down Expand Up @@ -54,11 +82,19 @@ export function Header({ patient, visits, handleVisitChange }) {
<p className="text-lg font-medium">{patient.name}</p>
</div>
<div className="col-span-12 md:col-span-4 flex items-center justify-between md:justify-start md:space-x-4 mt-4 md:mt-0">
<Button
text="Upload Document"
onClick={uploadDocument}
colour="green"
/>
<div>
<Button
text="Upload Document"
onClick={uploadDocument}
colour="green"
/>
<input
type="file"
ref={fileInputRef}
style={{ display: 'none' }}
onChange={handleFileChange}
/>
</div>
<Button text="View Document" onClick={viewDocument} colour="blue" />
</div>
</div>
Expand Down

0 comments on commit ff29a50

Please sign in to comment.