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 in Upload Again #44

Merged
merged 3 commits into from
Nov 9, 2022
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
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;
}