Skip to content

Commit

Permalink
Feat/sample file upload (#868)
Browse files Browse the repository at this point in the history
Added feature of passing sample file from the **showWidget**
  • Loading branch information
chavda-bhavik authored Nov 16, 2024
2 parents bcc7a96 + 72976bd commit 73cc9f0
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 16 deletions.
2 changes: 2 additions & 0 deletions apps/widget/src/components/Common/Container/Container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function Container({ children }: PropsWithChildren<{}>) {
setSecondaryPayload({
accessToken: data.value.accessToken,
host: data.value.host,
sampleFile: data.value.sampleFile,
projectId: data.value.projectId,
uuid: data.value.uuid,
extra: isObject(data.value.extra) ? JSON.stringify(data.value.extra) : data.value.extra,
Expand Down Expand Up @@ -339,6 +340,7 @@ export function Container({ children }: PropsWithChildren<{}>) {
authHeaderValue={secondaryPayload?.authHeaderValue}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
primaryColor={secondaryPayload.primaryColor!}
sampleFile={secondaryPayload?.sampleFile}
>
{children}
</Provider>
Expand Down
3 changes: 3 additions & 0 deletions apps/widget/src/components/Common/Provider/Provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface IProviderProps {
texts: typeof WIDGET_TEXTS;
primaryColor: string;
output?: string;
sampleFile?: File | Blob;
schema?: string;
data?: string;
host: string;
Expand All @@ -31,6 +32,7 @@ export function Provider(props: PropsWithChildren<IProviderProps>) {
api,
data,
title,
sampleFile,
texts,
output,
projectId,
Expand Down Expand Up @@ -58,6 +60,7 @@ export function Provider(props: PropsWithChildren<IProviderProps>) {
host={host}
data={data}
title={title}
sampleFile={sampleFile}
texts={texts}
output={output}
schema={schema}
Expand Down
7 changes: 4 additions & 3 deletions apps/widget/src/components/widget/Phases/Phase0/Phase0.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface IPhase0Props {
}

export function Phase0({ onValidationSuccess: goNext }: IPhase0Props) {
const { isLoading, error, isWidgetOpened, handleValidate } = usePhase0({
const { isLoading, error, fileError, isWidgetOpened, handleValidate } = usePhase0({
goNext,
});

Expand All @@ -20,7 +20,7 @@ export function Phase0({ onValidationSuccess: goNext }: IPhase0Props) {

if (isLoading) return <LoadingOverlay visible={isLoading} />;

if (error)
if (error || fileError) {
return (
<Alert
color="yellow"
Expand All @@ -34,9 +34,10 @@ export function Phase0({ onValidationSuccess: goNext }: IPhase0Props) {
border: '.1rem solid #FFC300',
}}
>
{error.message}
{error?.message || fileError}
</Alert>
);
}

return null;
}
25 changes: 20 additions & 5 deletions apps/widget/src/hooks/Phase0/usePhase0.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useMutation } from '@tanstack/react-query';

import { useState } from 'react';
import { FlowsEnum } from '@types';
import { useAPIState } from '@store/api.context';
import { useAppState } from '@store/app.context';
import { identifyImportIntent } from '@amplitude';
import { useImplerState } from '@store/impler.context';
import { IErrorObject, IImportConfig, TemplateModeEnum } from '@impler/shared';
import { FileMimeTypesEnum, IErrorObject, IImportConfig, TemplateModeEnum } from '@impler/shared';
import { isValidFileType } from 'util/helpers/common.helpers';

interface IUsePhase0Props {
goNext: () => void;
Expand All @@ -14,7 +15,8 @@ interface IUsePhase0Props {
export function usePhase0({ goNext }: IUsePhase0Props) {
const { api } = useAPIState();
const { projectId, templateId } = useImplerState();
const { schema, setImportConfig, showWidget, setFlow } = useAppState();
const [fileError, setFileError] = useState<string | null>(null);
const { schema, setImportConfig, showWidget, setFlow, sampleFile } = useAppState();

const { mutate: fetchImportConfig } = useMutation<IImportConfig, IErrorObject, void>(
['importConfig', projectId, templateId],
Expand All @@ -25,10 +27,22 @@ export function usePhase0({ goNext }: IUsePhase0Props) {
importConfigData.mode === TemplateModeEnum.AUTOMATIC ? FlowsEnum.AUTO_IMPORT : FlowsEnum.STRAIGHT_IMPORT
);
setImportConfig(importConfigData);
goNext();
const allowedTypes = [
FileMimeTypesEnum.CSV,
FileMimeTypesEnum.EXCEL,
FileMimeTypesEnum.EXCELM,
FileMimeTypesEnum.EXCELX,
];

if (sampleFile && !isValidFileType(sampleFile as Blob)) {
setFileError(`Only ${allowedTypes.join(',')} are supported`);
} else {
goNext();
}
},
}
);

const {
error,
isLoading,
Expand All @@ -47,11 +61,12 @@ export function usePhase0({ goNext }: IUsePhase0Props) {
);

const handleValidate = async () => {
return checkIsRequestvalid({ projectId, templateId, schema: schema });
return checkIsRequestvalid({ projectId, templateId, schema });
};

return {
error,
fileError,
isLoading,
handleValidate,
isWidgetOpened: showWidget,
Expand Down
22 changes: 16 additions & 6 deletions apps/widget/src/hooks/Phase1/usePhase1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useAPIState } from '@store/api.context';
import { useAppState } from '@store/app.context';
import { useImplerState } from '@store/impler.context';
import { IUpload, WIDGET_TEXTS } from '@impler/client';
import { IErrorObject, ITemplate, FileMimeTypesEnum, IColumn } from '@impler/shared';
import { IErrorObject, ITemplate, FileMimeTypesEnum, IColumn, downloadFile } from '@impler/shared';

import { variables } from '@config';
import { useSample } from '@hooks/useSample';
Expand Down Expand Up @@ -40,7 +40,7 @@ export function usePhase1({ goNext, texts, onManuallyEnterData }: IUsePhase1Prop
const { templateId, authHeaderValue, extra } = useImplerState();
const [excelSheetNames, setExcelSheetNames] = useState<string[]>([]);
const [isDownloadInProgress, setIsDownloadInProgress] = useState<boolean>(false);
const { setUploadInfo, setTemplateInfo, output, schema, data, importId, imageSchema } = useAppState();
const { setUploadInfo, setTemplateInfo, output, schema, data, importId, imageSchema, sampleFile } = useAppState();

const selectedTemplateId = watch('templateId');

Expand Down Expand Up @@ -73,7 +73,8 @@ export function usePhase1({ goNext, texts, onManuallyEnterData }: IUsePhase1Prop
string[],
IErrorObject,
{ file: File }
>(['getExcelSheetNames'], (file) => api.getExcelSheetNames(file), {
// eslint-disable-next-line prettier/prettier
>(['getExcelSheetNames'], (excelSheetFile) => api.getExcelSheetNames(excelSheetFile), {
onSuccess(sheetNames) {
if (sheetNames.length <= 1) {
setValue('selectedSheetName', sheetNames[0]);
Expand Down Expand Up @@ -107,6 +108,12 @@ export function usePhase1({ goNext, texts, onManuallyEnterData }: IUsePhase1Prop
}
};
const onDownloadClick = async () => {
if (sampleFile) {
const fileName = (sampleFile as File).name;
const fileBaseName = fileName.split('.')[0];
downloadFile(sampleFile as Blob, fileBaseName);
}

setIsDownloadInProgress(true);
const isTemplateValid = await trigger('templateId');
if (!isTemplateValid) {
Expand Down Expand Up @@ -148,9 +155,12 @@ export function usePhase1({ goNext, texts, onManuallyEnterData }: IUsePhase1Prop
});
}
};
const onSubmit = async (file?: File) => {
if (file && [FileMimeTypesEnum.EXCEL, FileMimeTypesEnum.EXCELX].includes(file.type as FileMimeTypesEnum)) {
getExcelSheetNames({ file: file });
const onSubmit = async (uploadedFile?: File) => {
if (
uploadedFile &&
[FileMimeTypesEnum.EXCEL, FileMimeTypesEnum.EXCELX].includes(uploadedFile.type as FileMimeTypesEnum)
) {
getExcelSheetNames({ file: uploadedFile });
} else {
handleSubmit(uploadFile)();
}
Expand Down
2 changes: 2 additions & 0 deletions apps/widget/src/store/app.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ const AppContextProvider = ({
title,
texts,
data,
sampleFile,
schema,
output,
host,
Expand Down Expand Up @@ -66,6 +67,7 @@ const AppContextProvider = ({
uploadInfo,
setImportId,
imageSchema,
sampleFile,
templateInfo,
importConfig,
primaryColor,
Expand Down
1 change: 1 addition & 0 deletions apps/widget/src/types/store.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface IAppStore {
schema?: string;
output?: string;
flow: FlowsEnum;
sampleFile?: File | Blob;
showWidget: boolean;
importConfig: IImportConfig;
setFlow: (flow: FlowsEnum) => void;
Expand Down
14 changes: 13 additions & 1 deletion apps/widget/src/util/helpers/common.helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'tippy.js/dist/tippy.css';
import 'tippy.js/animations/shift-away.css';
import { variables } from '@config';
import { WIDGET_TEXTS, isObject } from '@impler/client';
import { convertStringToJson, downloadFile } from '@impler/shared';
import { convertStringToJson, downloadFile, FileMimeTypesEnum } from '@impler/shared';

// eslint-disable-next-line no-magic-numbers
export function formatBytes(bytes, decimals = 2) {
Expand Down Expand Up @@ -151,3 +151,15 @@ export function memoize<T extends (...args: any[]) => any>(fn: T): T {
return result;
} as T;
}

export const isValidFileType = (sampleFile: Blob): boolean => {
if (
sampleFile instanceof Blob &&
[FileMimeTypesEnum.CSV, FileMimeTypesEnum.EXCEL, FileMimeTypesEnum.EXCELM, FileMimeTypesEnum.EXCELX].includes(
sampleFile.type as FileMimeTypesEnum
)
)
return true;

return false;
};
1 change: 1 addition & 0 deletions libs/shared/src/types/widget/widget.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface ICommonShowPayload {
primaryColor?: string;
colorScheme?: string;
title?: string;
sampleFile?: File | Blob;
projectId: string;
accessToken: string;
uuid: string;
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export interface IShowWidgetProps {
projectId: string;
templateId: string;
accessToken: string;
sampleFile?: File | Blob;
texts?: CustomTexts;
title?: string;
primaryColor?: string;
Expand Down
4 changes: 3 additions & 1 deletion packages/react/src/hooks/useImpler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ export function useImpler({
const showWidget = async ({
colorScheme,
data,
sampleFile,
schema,
output,
}: Pick<IShowWidgetProps, 'colorScheme' | 'data' | 'schema' | 'output'> = {}) => {
}: Pick<IShowWidgetProps, 'colorScheme' | 'data' | 'schema' | 'output' | 'sampleFile'> = {}) => {
if (window.impler && isImplerInitiated) {
const payload: IShowWidgetProps & { uuid: string; host: string } = {
uuid,
Expand All @@ -91,6 +92,7 @@ export function useImpler({
accessToken,
schema,
data,
sampleFile,
output,
title,
extra,
Expand Down

0 comments on commit 73cc9f0

Please sign in to comment.