Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: hardcoded api host in file download fetch requests #1332

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Button } from '@/components';
import { AiOutlineDeliveredProcedure } from 'react-icons/ai';
import { Alert } from 'flowbite-react';
import { FormattedMessage } from 'react-intl';
import { useSelector } from 'react-redux';
import { RootState } from '@/store';

interface Props {
disabled?: boolean;
Expand All @@ -11,6 +13,7 @@ interface Props {
const ProjectOfferFileDownloadButton: React.FC<Props> = ({ disabled }) => {
const [downloadLoading, setDownloadLoading] = useState(false);
const [showDownLoadFailedAlert, setShowDownloadFailedAlert] = useState<boolean>(false);
const appStore = useSelector((state: RootState) => state.app);

useEffect(() => {
if (showDownLoadFailedAlert) {
Expand All @@ -33,9 +36,20 @@ const ProjectOfferFileDownloadButton: React.FC<Props> = ({ disabled }) => {
const handleClickDownload = async () => {
setDownloadLoading(true);
try {
const url = new URL('http://localhost:31310/v1/offer');
const url = new URL(`${appStore.apiHost}/v1/offer`);
const headers = {
Accept: '*/*',
};

if (appStore?.apiKey) {
headers['X-Api-Key'] = appStore.apiKey;
}

const downloadResponse: Response = await fetch(url, {
mode: 'cors',
headers,
});

const downloadResponse: Response = await fetch(url);
if (!downloadResponse?.ok) {
setShowDownloadFailedAlert(true);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Button } from '@/components';
import { useUploadProjectsXlsMutation } from '@/api';
import { Alert } from 'flowbite-react';
import { FormattedMessage } from 'react-intl';
import { useSelector } from 'react-redux';
import { RootState } from '@/store';

interface XlsUploadDownloadButtonsProps {
orgUid: string;
Expand All @@ -20,6 +22,7 @@ const ProjectXlsUploadDownloadButtons: React.FC<XlsUploadDownloadButtonsProps> =
}: XlsUploadDownloadButtonsProps) => {
// upload hooks and state
const [triggerUploadProjectXls] = useUploadProjectsXlsMutation();
const appStore = useSelector((state: RootState) => state.app);
const fileInputRef = useRef<HTMLInputElement>(null);
const [showUploadFailedAlert, setShowUploadFailedAlert] = useState<boolean>(false);

Expand Down Expand Up @@ -63,13 +66,25 @@ const ProjectXlsUploadDownloadButtons: React.FC<XlsUploadDownloadButtonsProps> =
const handleClickDownload = async () => {
setDownloadLoading(true);
try {
const url = new URL('http://localhost:31310/v1/projects');
const url = new URL(`${appStore.apiHost}/v1/projects`);
url.searchParams.append('xls', 'true');
orgUid && url.searchParams.append('orgUid', orgUid);
search && url.searchParams.append('search', search);
order && url.searchParams.append('order', order);

const downloadResponse: Response = await fetch(url);
const headers = {
Accept: '*/*',
};

if (appStore?.apiKey) {
headers['X-Api-Key'] = appStore.apiKey;
}

const downloadResponse: Response = await fetch(url, {
mode: 'cors',
headers,
});

if (!downloadResponse?.ok) {
setShowDownloadFailedAlert(true);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { Button } from '@/components';
import { useUploadUnitsXlsMutation } from '@/api';
import { Alert } from 'flowbite-react';
import { FormattedMessage } from 'react-intl';
import { useSelector } from 'react-redux';
import { RootState } from '@/store';

interface XlsUploadDownloadButtonsProps {
orgUid: string;
Expand All @@ -20,6 +22,7 @@ const UnitXlsUploadDownloadButtons: React.FC<XlsUploadDownloadButtonsProps> = ({
}: XlsUploadDownloadButtonsProps) => {
// upload hooks and state
const [triggerUploadUnitXls] = useUploadUnitsXlsMutation();
const appStore = useSelector((state: RootState) => state.app);
const fileInputRef = useRef<HTMLInputElement>(null);
const [showUploadFailedAlert, setShowUploadFailedAlert] = useState<boolean>(false);
const [downloadLoading, setDownloadLoading] = useState(false);
Expand Down Expand Up @@ -63,13 +66,25 @@ const UnitXlsUploadDownloadButtons: React.FC<XlsUploadDownloadButtonsProps> = ({
const handleClickDownload = async () => {
setDownloadLoading(true);
try {
const url = new URL('http://localhost:31310/v1/units');
const url = new URL(`${appStore.apiHost}/v1/units`);
url.searchParams.append('xls', 'true');
orgUid && url.searchParams.append('orgUid', orgUid);
search && url.searchParams.append('search', search);
order && url.searchParams.append('order', order);

const downloadResponse: Response = await fetch(url);
const headers = {
Accept: '*/*',
};

if (appStore?.apiKey) {
headers['X-Api-Key'] = appStore.apiKey;
}

const downloadResponse: Response = await fetch(url, {
mode: 'cors',
headers,
});

if (!downloadResponse?.ok) {
setShowDownloadFailedAlert(true);
return;
Expand Down
Loading