Skip to content

Commit

Permalink
Fix in Upload Again and Reset
Browse files Browse the repository at this point in the history
* feat: Made ReactQuery cache clear on upload reset

* feat: Made app to reset progress on close and Upload Again

* feat: Updated footer actions
  • Loading branch information
chavda-bhavik authored Nov 9, 2022
1 parent bfeb308 commit 7267efc
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 25 deletions.
7 changes: 2 additions & 5 deletions apps/widget/src/components/Common/Footer/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function Footer(props: IFooterProps) {
[PhasesEum.REVIEW]: (
<>
<Button loading={secondaryButtonLoading} onClick={onPrevClick} variant="outline">
{TEXTS.PHASE3.BACK_TO_MAPPING}
{TEXTS.PHASE2.UPLOAD_AGAIN}
</Button>
<Button loading={primaryButtonLoading} onClick={onNextClick}>
{TEXTS.PHASE3.CONFIRM_UPLOAD}
Expand All @@ -42,11 +42,8 @@ export function Footer(props: IFooterProps) {
),
[PhasesEum.CONFIRMATION]: (
<>
<Button loading={secondaryButtonLoading} onClick={onPrevClick} variant="outline">
{TEXTS.PHASE3.BACK_TO_MAPPING}
</Button>
<Button loading={primaryButtonLoading} onClick={onNextClick}>
{TEXTS.PHASE3.CONFIRM_UPLOAD}
{TEXTS.PHASE2.UPLOAD_AGAIN}
</Button>
</>
),
Expand Down
14 changes: 9 additions & 5 deletions apps/widget/src/components/widget/Widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { PromptModal } from './Phases/PromptModal';
import { Phase4 } from './Phases/Phase4';
import { ParentWindow } from '@util';
import { PhasesEum, PromptModalTypesEnum } from '@types';
import { useQueryClient } from '@tanstack/react-query';

export function Widget() {
const queryClient = useQueryClient();
const [phase, setPhase] = useState<PhasesEum>(PhasesEum.UPLOAD);
const [showConfirmModal, setShowConfirmModal] = useState(false);
const [promptContinueAction, setPromptContinueAction] = useState<PromptModalTypesEnum>();
Expand All @@ -24,7 +26,7 @@ export function Widget() {
const onPromptConfirm = () => {
setPromptContinueAction(undefined);
if (promptContinueAction === PromptModalTypesEnum.CLOSE) closeWidget();
else if (promptContinueAction === PromptModalTypesEnum.UPLOAD_AGAIN) setPhase(PhasesEum.UPLOAD);
resetProgress();
};
const onPromptCancel = () => {
setPromptContinueAction(undefined);
Expand All @@ -36,14 +38,16 @@ export function Widget() {
const closeWidget = () => {
ParentWindow.Close();
};
const resetProgress = () => {
queryClient.clear();
setPhase(PhasesEum.UPLOAD);
};

const PhaseView = {
[PhasesEum.UPLOAD]: <Phase1 onNextClick={() => setPhase(PhasesEum.MAPPING)} />,
[PhasesEum.MAPPING]: <Phase2 onNextClick={() => setPhase(PhasesEum.REVIEW)} onPrevClick={onUploadResetClick} />,
[PhasesEum.REVIEW]: (
<Phase3 onNextClick={() => setShowConfirmModal(true)} onPrevClick={() => setPhase(PhasesEum.MAPPING)} />
),
[PhasesEum.CONFIRMATION]: <Phase4 rowsCount={1000000} onUploadAgainClick={() => setPhase(PhasesEum.UPLOAD)} />,
[PhasesEum.REVIEW]: <Phase3 onNextClick={() => setShowConfirmModal(true)} onPrevClick={onUploadResetClick} />,
[PhasesEum.CONFIRMATION]: <Phase4 rowsCount={1000000} onUploadAgainClick={onUploadResetClick} />,
};

return (
Expand Down
1 change: 0 additions & 1 deletion apps/widget/src/config/texts.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export const TEXTS = {
PHASE3: {
INVALID_DATA_INFO: 'Sheet contains invalid data, hover over columns to see error',
EXPORT_DATA: 'Export Data',
BACK_TO_MAPPING: 'Back to mapping',
CONFIRM_UPLOAD: 'Complete Upload',
},
CONFIRM_MODAL: {
Expand Down
20 changes: 8 additions & 12 deletions apps/widget/src/hooks/Phase1/usePhase1.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,20 @@
import { useState } from 'react';
import { useEffect, useState } from 'react';
import { useForm } from 'react-hook-form';
import { useImplerState } from '@store/impler.context';
import { useMutation, useQuery } from '@tanstack/react-query';
import { useAPIState } from '@store/api.context';
import { IErrorObject, IOption, ITemplate, IUpload } from '@impler/shared';
import { useAppState } from '@store/app.context';
import { downloadFileFromURL, notifier } from '@util';

interface IFormvalues {
template: string;
file: File;
}

interface IUploadValues extends IFormvalues {
authHeaderValue?: string;
extra?: string;
}
import { IFormvalues, IUploadValues } from '@types';

interface IUsePhase1Props {
goNext: () => void;
}

export function usePhase1({ goNext }: IUsePhase1Props) {
const { api } = useAPIState();
const { setUploadInfo } = useAppState();
const { setUploadInfo, reset } = useAppState();
const [templates, setTemplates] = useState<IOption[]>([]);
const [isDownloadInProgress, setIsDownloadInProgress] = useState<boolean>(false);
const { projectId, template, authHeaderValue, extra } = useImplerState();
Expand Down Expand Up @@ -60,6 +51,11 @@ export function usePhase1({ goNext }: IUsePhase1Props) {
formState: { errors },
} = useForm<IFormvalues>();

// reset App State on initial load
useEffect(() => {
reset();
}, []);

const onDownload = async () => {
setIsDownloadInProgress(true);
const isTemplateValid = await trigger('template');
Expand Down
8 changes: 6 additions & 2 deletions apps/widget/src/store/app.context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,16 @@ const AppContext = createContext<IAppStore | null>(null);
const AppContextProvider = ({ children }: AppContextProviderProps) => {
const [uploadInfo, setUploadInfo] = useState<IUpload>({} as IUpload);

return <AppContext.Provider value={{ uploadInfo, setUploadInfo }}>{children}</AppContext.Provider>;
const reset = () => {
setUploadInfo({} as IUpload);
};

return <AppContext.Provider value={{ uploadInfo, setUploadInfo, reset }}>{children}</AppContext.Provider>;
};

export function useAppState() {
const context = useContext(AppContext);
if (!context) throw new Error('API Context must be used within APIContextProvider');
if (!context) throw new Error('App Context must be used within AppContextProvider');

return context;
}
Expand Down
11 changes: 11 additions & 0 deletions apps/widget/src/types/component.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type MessageHandlerDataType =
type: EventTypesEnum.SHOW_WIDGET;
value: IShowPayload;
};

export enum PromptModalTypesEnum {
'CLOSE' = 'CLOSE',
'UPLOAD_AGAIN' = 'UPLOAD_AGAIN',
Expand All @@ -21,3 +22,13 @@ export enum PhasesEum {
REVIEW,
CONFIRMATION,
}

export interface IFormvalues {
template: string;
file: File;
}

export interface IUploadValues extends IFormvalues {
authHeaderValue?: string;
extra?: string;
}
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 @@ -15,5 +15,6 @@ export interface IApiStore {

export interface IAppStore {
uploadInfo: IUpload;
reset: () => void;
setUploadInfo: (uploadInfo: IUpload) => void;
}

0 comments on commit 7267efc

Please sign in to comment.