mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-16 12:50:38 +00:00
fix: Make webapp package env variables dynamic
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import React from 'react';
|
||||
import { Redirect } from 'react-router-dom';
|
||||
import { Config } from '@/config';
|
||||
import { useOneClickDemoBoot } from './OneClickDemoBoot';
|
||||
|
||||
interface EnsureOneClickDemoAccountEnabledProps {
|
||||
children: React.ReactNode;
|
||||
@@ -11,9 +11,10 @@ export const EnsureOneClickDemoAccountEnabled = ({
|
||||
children,
|
||||
redirectTo = '/',
|
||||
}: EnsureOneClickDemoAccountEnabledProps) => {
|
||||
const enabeld = Config.oneClickDemo.enable || false;
|
||||
const { authMeta } = useOneClickDemoBoot();
|
||||
const enabled = authMeta?.meta?.one_click_demo?.enable || false;
|
||||
|
||||
if (!enabeld) {
|
||||
if (!enabled) {
|
||||
return <Redirect to={{ pathname: redirectTo }} />;
|
||||
}
|
||||
return <>{children}</>;
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
import React, { createContext, useContext, ReactNode } from 'react';
|
||||
import { useAuthMetadata } from '@/hooks/query/authentication';
|
||||
|
||||
interface OneClickDemoContextType {
|
||||
authMeta: any;
|
||||
}
|
||||
|
||||
const OneClickDemoContext = createContext<OneClickDemoContextType>(
|
||||
{} as OneClickDemoContextType,
|
||||
);
|
||||
|
||||
export const useOneClickDemoBoot = () => {
|
||||
const context = useContext(OneClickDemoContext);
|
||||
|
||||
if (!context) {
|
||||
throw new Error(
|
||||
'useOneClickDemo must be used within a OneClickDemoProvider',
|
||||
);
|
||||
}
|
||||
return context;
|
||||
};
|
||||
|
||||
interface OneClickDemoBootProps {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export const OneClickDemoBoot: React.FC<OneClickDemoBootProps> = ({
|
||||
children,
|
||||
}) => {
|
||||
const { isLoading: isAuthMetaLoading, data: authMeta } = useAuthMetadata();
|
||||
|
||||
const value = {
|
||||
isAuthMetaLoading,
|
||||
authMeta,
|
||||
};
|
||||
|
||||
if (isAuthMetaLoading) {
|
||||
return null;
|
||||
}
|
||||
return (
|
||||
<OneClickDemoContext.Provider value={value}>
|
||||
{children}
|
||||
</OneClickDemoContext.Provider>
|
||||
);
|
||||
};
|
||||
@@ -1,97 +1,16 @@
|
||||
// @ts-nocheck
|
||||
import { Button, Intent, ProgressBar, Text } from '@blueprintjs/core';
|
||||
import { useEffect, useState } from 'react';
|
||||
import {
|
||||
useCreateOneClickDemo,
|
||||
useOneClickDemoSignin,
|
||||
} from '@/hooks/query/oneclick-demo';
|
||||
import { Box, Icon, Stack } from '@/components';
|
||||
import { useJob } from '@/hooks/query';
|
||||
import style from './OneClickDemoPage.module.scss';
|
||||
import { EnsureAuthNotAuthenticated } from '@/components/Guards/EnsureAuthNotAuthenticated';
|
||||
import { EnsureOneClickDemoAccountEnabled } from './EnsureOneClickDemoAccountEnabled';
|
||||
import { OneClickDemoBoot } from './OneClickDemoBoot';
|
||||
import { OneClickDemoPageContent } from './OneClickDemoPageContent';
|
||||
|
||||
export default function OneClickDemoPage() {
|
||||
const {
|
||||
mutateAsync: createOneClickDemo,
|
||||
isLoading: isCreateOneClickLoading,
|
||||
} = useCreateOneClickDemo();
|
||||
const {
|
||||
mutateAsync: oneClickDemoSignIn,
|
||||
isLoading: isOneclickDemoSigningIn,
|
||||
} = useOneClickDemoSignin();
|
||||
|
||||
// Job states.
|
||||
const [demoId, setDemoId] = useState<string>('');
|
||||
const [buildJobId, setBuildJobId] = useState<string>('');
|
||||
const [isJobDone, setIsJobDone] = useState<boolean>(false);
|
||||
|
||||
const {
|
||||
data: { running, completed },
|
||||
} = useJob(buildJobId, {
|
||||
refetchInterval: 2000,
|
||||
enabled: !isJobDone && !!buildJobId,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (completed) {
|
||||
setIsJobDone(true);
|
||||
}
|
||||
}, [completed, setIsJobDone]);
|
||||
|
||||
// One the job done request sign-in using the demo id.
|
||||
useEffect(() => {
|
||||
if (isJobDone) {
|
||||
oneClickDemoSignIn({ demoId }).then((res) => {
|
||||
debugger;
|
||||
});
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [isJobDone]);
|
||||
|
||||
const handleCreateAccountBtnClick = () => {
|
||||
createOneClickDemo({})
|
||||
.then(({ data: { data } }) => {
|
||||
setBuildJobId(data?.build_job?.job_id);
|
||||
setDemoId(data?.demo_id);
|
||||
})
|
||||
.catch(() => {});
|
||||
};
|
||||
const isLoading = running || isOneclickDemoSigningIn;
|
||||
|
||||
return (
|
||||
<Box className={style.root}>
|
||||
<Box className={style.inner}>
|
||||
<Stack align={'center'} spacing={40}>
|
||||
<Icon icon="bigcapital" height={37} width={228} />
|
||||
|
||||
{isLoading && (
|
||||
<Stack align={'center'} spacing={15}>
|
||||
<ProgressBar stripes value={null} className={style.progressBar} />
|
||||
{isOneclickDemoSigningIn && (
|
||||
<Text className={style.waitingText}>
|
||||
It's signin-in to your demo account, Just a second!
|
||||
</Text>
|
||||
)}
|
||||
{running && (
|
||||
<Text className={style.waitingText}>
|
||||
We're preparing temporary environment for trial, It typically
|
||||
take few seconds. Do not close or refresh the page.
|
||||
</Text>
|
||||
)}
|
||||
</Stack>
|
||||
)}
|
||||
</Stack>
|
||||
|
||||
{!isLoading && (
|
||||
<Button
|
||||
className={style.oneClickBtn}
|
||||
intent={Intent.NONE}
|
||||
onClick={handleCreateAccountBtnClick}
|
||||
loading={isCreateOneClickLoading}
|
||||
>
|
||||
Create Demo Account
|
||||
</Button>
|
||||
)}
|
||||
</Box>
|
||||
</Box>
|
||||
<EnsureAuthNotAuthenticated>
|
||||
<OneClickDemoBoot>
|
||||
<EnsureOneClickDemoAccountEnabled>
|
||||
<OneClickDemoPageContent />
|
||||
</EnsureOneClickDemoAccountEnabled>
|
||||
</OneClickDemoBoot>
|
||||
</EnsureAuthNotAuthenticated>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
// @ts-nocheck
|
||||
import { Button, Intent, ProgressBar, Text } from '@blueprintjs/core';
|
||||
import { useEffect, useState } from 'react';
|
||||
import {
|
||||
useCreateOneClickDemo,
|
||||
useOneClickDemoSignin,
|
||||
} from '@/hooks/query/oneclick-demo';
|
||||
import { Box, Icon, Stack } from '@/components';
|
||||
import { useJob } from '@/hooks/query';
|
||||
import style from './OneClickDemoPage.module.scss';
|
||||
|
||||
export function OneClickDemoPageContent() {
|
||||
const {
|
||||
mutateAsync: createOneClickDemo,
|
||||
isLoading: isCreateOneClickLoading,
|
||||
} = useCreateOneClickDemo();
|
||||
const {
|
||||
mutateAsync: oneClickDemoSignIn,
|
||||
isLoading: isOneclickDemoSigningIn,
|
||||
} = useOneClickDemoSignin();
|
||||
|
||||
// Job states.
|
||||
const [demoId, setDemoId] = useState<string>('');
|
||||
const [buildJobId, setBuildJobId] = useState<string>('');
|
||||
const [isJobDone, setIsJobDone] = useState<boolean>(false);
|
||||
|
||||
const {
|
||||
data: { running, completed },
|
||||
} = useJob(buildJobId, {
|
||||
refetchInterval: 2000,
|
||||
enabled: !isJobDone && !!buildJobId,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (completed) {
|
||||
setIsJobDone(true);
|
||||
}
|
||||
}, [completed, setIsJobDone]);
|
||||
|
||||
// One the job done request sign-in using the demo id.
|
||||
useEffect(() => {
|
||||
if (isJobDone) {
|
||||
oneClickDemoSignIn({ demoId }).then((res) => {
|
||||
debugger;
|
||||
});
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [isJobDone]);
|
||||
|
||||
const handleCreateAccountBtnClick = () => {
|
||||
createOneClickDemo({})
|
||||
.then(({ data: { data } }) => {
|
||||
setBuildJobId(data?.build_job?.job_id);
|
||||
setDemoId(data?.demo_id);
|
||||
})
|
||||
.catch(() => {});
|
||||
};
|
||||
const isLoading = running || isOneclickDemoSigningIn;
|
||||
|
||||
return (
|
||||
<Box className={style.root}>
|
||||
<Box className={style.inner}>
|
||||
<Stack align={'center'} spacing={40}>
|
||||
<Icon icon="bigcapital" height={37} width={228} />
|
||||
|
||||
{isLoading && (
|
||||
<Stack align={'center'} spacing={15}>
|
||||
<ProgressBar stripes value={null} className={style.progressBar} />
|
||||
{isOneclickDemoSigningIn && (
|
||||
<Text className={style.waitingText}>
|
||||
It's signin-in to your demo account, Just a second!
|
||||
</Text>
|
||||
)}
|
||||
{running && (
|
||||
<Text className={style.waitingText}>
|
||||
We're preparing temporary environment for trial, It typically
|
||||
take few seconds. Do not close or refresh the page.
|
||||
</Text>
|
||||
)}
|
||||
</Stack>
|
||||
)}
|
||||
</Stack>
|
||||
|
||||
{!isLoading && (
|
||||
<Button
|
||||
className={style.oneClickBtn}
|
||||
intent={Intent.NONE}
|
||||
onClick={handleCreateAccountBtnClick}
|
||||
loading={isCreateOneClickLoading}
|
||||
>
|
||||
Create Demo Account
|
||||
</Button>
|
||||
)}
|
||||
</Box>
|
||||
</Box>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user