reading from env

reading from env
This commit is contained in:
Benjamin Loh 2023-10-30 13:51:29 +08:00
parent 8914c6b356
commit bb9a1e7f82
3 changed files with 13 additions and 3 deletions

View File

@ -0,0 +1,2 @@
REACT_APP_API_HOST=http://127.0.0.1
REACT_APP_API_PORT=8000

View File

@ -20,6 +20,10 @@ function DownloadFile() {
const [errorMsg, setErrorcode] = useState(''); const [errorMsg, setErrorcode] = useState('');
let subtitle; let subtitle;
const [modalIsOpen, setIsOpen] = React.useState(false); const [modalIsOpen, setIsOpen] = React.useState(false);
const apiHost = process.env.REACT_APP_API_HOST || 'localhost';
const apiPort = process.env.REACT_APP_API_PORT || '8000';
const apiUrl = `${apiHost}:${apiPort}`;
function openModal() { function openModal() {
setIsOpen(true); setIsOpen(true);
@ -40,7 +44,7 @@ function DownloadFile() {
const handleDownloadFile = () => { const handleDownloadFile = () => {
if (passcode) { if (passcode) {
axios.get(`http://127.0.0.1:8000/api/files/${passcode}/`, {responseType: 'blob'}) axios.get(`${apiUrl}/api/files/${passcode}/`, {responseType: 'blob'})
.then(response => { .then(response => {
let filename = 'downloaded_file'; // Default filename let filename = 'downloaded_file'; // Default filename
let mimeType = 'application/octet-stream'; // Default MIME type let mimeType = 'application/octet-stream'; // Default MIME type

View File

@ -23,7 +23,10 @@ function ShareFile() {
const [errorMsg, setErrorcode] = useState(''); const [errorMsg, setErrorcode] = useState('');
let subtitle; let subtitle;
const [modalIsOpen, setIsOpen] = React.useState(false); const [modalIsOpen, setIsOpen] = React.useState(false);
const apiHost = process.env.REACT_APP_API_HOST || 'localhost';
const apiPort = process.env.REACT_APP_API_PORT || '8000';
const apiUrl = `${apiHost}:${apiPort}`;
function openModal() { function openModal() {
setIsOpen(true); setIsOpen(true);
@ -42,6 +45,7 @@ function ShareFile() {
setFile(file); setFile(file);
//setPasscode('1234'); //setPasscode('1234');
console.log(file); console.log(file);
console.log(apiUrl);
if (file) { if (file) {
const formData = new FormData(); const formData = new FormData();
formData.append('file', file); formData.append('file', file);
@ -49,7 +53,7 @@ function ShareFile() {
// Send POST request to the backend API using Axios // Send POST request to the backend API using Axios
axios axios
.post('http://127.0.0.1:8000/api/files/', formData) .post(`${apiUrl}/api/files/`, formData)
.then((response) => { .then((response) => {
// Handle a successful response from the server, set passcode to "key" in the response body // Handle a successful response from the server, set passcode to "key" in the response body
const data = response.data; const data = response.data;
@ -57,7 +61,7 @@ function ShareFile() {
// If data is an array, take the first item // If data is an array, take the first item
if (Array.isArray(data)) { if (Array.isArray(data)) {
const passcode = data[0].key; const passcode = data[0].key;
const baseUrl = 'http://localhost:8000/api/files/'; const baseUrl = '${apiUrl}/api/files/';
setPasscode(passcode); setPasscode(passcode);
setShareableLink(baseUrl + passcode); setShareableLink(baseUrl + passcode);