Skip to content

Commit

Permalink
Merge branch 'refactor/ci-build-05142024' of github.com:Chia-Network/…
Browse files Browse the repository at this point in the history
…cadt-ui into refactor/ci-build-05142024
  • Loading branch information
TheLastCicada committed May 22, 2024
2 parents cf49598 + 01a72f8 commit b44a48c
Show file tree
Hide file tree
Showing 15 changed files with 308 additions and 210 deletions.
45 changes: 4 additions & 41 deletions src/renderer/api/cadt/v1/projects/projects.api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import { cadtApi, projectsTag, stagedProjectsTag } from '../';
import { Project } from '@/schemas/Project.schema';

interface GetProjectsParams {
page: number;
page?: number;
orgUid?: string | null;
search?: string | null;
order?: string | null;
xls?: boolean | null;
}

interface GetProjectParams {
Expand All @@ -27,7 +26,7 @@ interface GetProjectsResponse {
const projectsApi = cadtApi.injectEndpoints({
endpoints: (builder) => ({
getProjects: builder.query<GetProjectsResponse, GetProjectsParams>({
query: ({ page, orgUid, search, order, xls }: GetProjectsParams) => {
query: ({ page, orgUid, search, order }: GetProjectsParams) => {
// Initialize the params object with page and limit
const params: GetProjectsParams & { limit: number } = { page, limit: 10 };

Expand All @@ -43,27 +42,18 @@ const projectsApi = cadtApi.injectEndpoints({
params.order = order;
}

if (xls) {
params.xls = xls;
if (!orgUid) {
params.orgUid = 'all';
}
}

return {
url: `/v1/projects`,
params,
method: 'GET',
};
},
// @ts-ignore
providesTags: (_response, _error, { orgUid }) => [{ type: projectsTag, id: orgUid }],
}),

getProjectsImmediate: builder.mutation<GetProjectsResponse, GetProjectsParams>({
query: ({ page, orgUid, search, order, xls }: GetProjectsParams) => {
query: ({ orgUid, search, order }: GetProjectsParams) => {
// Initialize the params object with page and limit
const params: GetProjectsParams & { limit: number } = { page, limit: 10 };
const params: GetProjectsParams = {};

if (orgUid) {
params.orgUid = orgUid;
Expand All @@ -77,21 +67,12 @@ const projectsApi = cadtApi.injectEndpoints({
params.order = order;
}

if (xls) {
params.xls = xls;
if (!orgUid) {
params.orgUid = 'all';
}
}

return {
url: `/v1/projects`,
params,
method: 'GET',
};
},
// @ts-ignore
providesTags: (_response, _error, { orgUid }) => [{ type: projectsTag, id: orgUid }],
}),

getProject: builder.query<Project, GetProjectParams>({
Expand Down Expand Up @@ -206,22 +187,6 @@ const projectsApi = cadtApi.injectEndpoints({
},
invalidatesTags: [stagedProjectsTag],
}),

downloadProjectsXls: builder.query<any, { orgUid: string }>({
query: ({ orgUid }) => ({
url: `/v1/projects/xlsx`,
method: 'GET',
params: { orgUid, xls: true },
}),
}),

downloadProjectsXlsImmediate: builder.mutation<any, { orgUid: string }>({
query: ({ orgUid }) => ({
url: `/v1/projects/xlsx`,
method: 'GET',
params: { orgUid, xls: true },
}),
}),
}),
});

Expand All @@ -234,7 +199,5 @@ export const {
useDeleteProjectMutation,
useStageCreateProjectMutation,
useStageUpdateProjectMutation,
useDownloadProjectsXlsQuery,
useUploadProjectsXlsMutation,
useDownloadProjectsXlsImmediateMutation,
} = projectsApi;
22 changes: 2 additions & 20 deletions src/renderer/api/cadt/v1/units/units.api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNil, isEmpty, omit } from 'lodash';
import { isEmpty, isNil, omit } from 'lodash';
import { cadtApi, stagedUnitsTag, unitsTag } from '../';
import { Unit } from '@/schemas/Unit.schema';

Expand Down Expand Up @@ -110,7 +110,7 @@ const unitsApi = cadtApi.injectEndpoints({
}),

stageSplitUnit: builder.mutation<any, any>({
query: ({warehouseUnitId, records}) => {
query: ({ warehouseUnitId, records }) => {
return {
url: `/v1/units/split`,
method: 'POST',
Expand All @@ -136,22 +136,6 @@ const unitsApi = cadtApi.injectEndpoints({
},
invalidatesTags: [stagedUnitsTag],
}),

downloadUnitsXls: builder.query<any, { orgUid: string }>({
query: ({ orgUid }) => ({
url: `/v1/units/xlsx`,
method: 'GET',
params: { orgUid, xls: true },
}),
}),

downloadUnitsXlsImmediate: builder.mutation<any, { orgUid: string }>({
query: ({ orgUid }) => ({
url: `/v1/units/xlsx`,
method: 'GET',
params: { orgUid, xls: true },
}),
}),
}),
});

Expand All @@ -164,7 +148,5 @@ export const {
useStageCreateUnitMutation,
useStageUpdateUnitMutation,
useStageSplitUnitMutation,
useDownloadUnitsXlsQuery,
useUploadUnitsXlsMutation,
useDownloadUnitsXlsImmediateMutation,
} = unitsApi;
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import React, { useEffect, useRef, useState } from 'react';
import { AiOutlineDownload, AiOutlineUpload } from 'react-icons/ai';
import { Button } from '@/components';
import { useUploadProjectsXlsMutation } from '@/api';
import { Alert } from 'flowbite-react';
import { FormattedMessage } from 'react-intl';

interface XlsUploadDownloadButtonsProps {
orgUid: string;
order: string;
search: string;
downloadOnly?: boolean;
}

const ProjectXlsUploadDownloadButtons: React.FC<XlsUploadDownloadButtonsProps> = ({
downloadOnly,
orgUid,
order,
search,
}: XlsUploadDownloadButtonsProps) => {
// upload hooks and state
const [triggerUploadProjectXls] = useUploadProjectsXlsMutation();
const fileInputRef = useRef<HTMLInputElement>(null);
const [showUploadFailedAlert, setShowUploadFailedAlert] = useState<boolean>(false);
const [downloadLoading, setDownloadLoading] = useState(false);

// download hooks and state
const [showDownLoadFailedAlert, setShowDownloadFailedAlert] = useState<boolean>(false);

useEffect(() => {
if (showDownLoadFailedAlert) {
setDownloadLoading(false);
}
}, [showDownLoadFailedAlert]);

const handleFileChange = async (event: React.ChangeEvent<HTMLInputElement>) => {
const xlsx: File | undefined = event.target.files?.[0];

if (xlsx) {
const uploadResult: any = await triggerUploadProjectXls({ xlsx });
if (uploadResult?.data?.error || uploadResult?.error) {
setShowUploadFailedAlert(true);
}
}
};

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

const handleDownloadedData = async (data: Blob) => {
const blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'projects-data.xlsx';
document.body.appendChild(link);
link.click();
link.remove();
URL.revokeObjectURL(url);
};

const handleClickDownload = async () => {
setDownloadLoading(true);
try {
const url = new URL('http://localhost:31310/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);
if (!downloadResponse?.ok) {
setShowDownloadFailedAlert(true);
return;
}

const blob: Blob = await downloadResponse.blob();
if (!blob) {
setShowDownloadFailedAlert(true);
return;
}

await handleDownloadedData(blob);
setDownloadLoading(false);
} catch (error) {
setShowDownloadFailedAlert(true);
}
};

return (
<>
{downloadOnly ? (
<Button color="gray" onClick={handleClickDownload} isProcessing={downloadLoading}>
<AiOutlineDownload className="h-5 w-5" />
</Button>
) : (
<>
<input type="file" ref={fileInputRef} style={{ display: 'none' }} onChange={handleFileChange} />
<Button.Group>
<Button color="gray" onClick={handleClickDownload} isProcessing={downloadLoading}>
<AiOutlineDownload className="h-5 w-5" />
</Button>
<Button color="gray" onClick={handleUpload}>
<AiOutlineUpload className="h-5 w-5" />
</Button>
</Button.Group>
</>
)}

{showUploadFailedAlert && (
<div className="fixed w-1/4">
<Alert color="failure" onDismiss={() => setShowUploadFailedAlert(false)}>
<FormattedMessage id="upload-failed" />!
</Alert>
</div>
)}
{showDownLoadFailedAlert && (
<div className="fixed w-1/4">
<Alert color="failure" onDismiss={() => setShowDownloadFailedAlert(false)}>
<FormattedMessage id="download-failed" />!
</Alert>
</div>
)}
</>
);
};

export { ProjectXlsUploadDownloadButtons };
Loading

0 comments on commit b44a48c

Please sign in to comment.