feat: simpler adding channels redirect
This commit is contained in:
parent
d272890eba
commit
0129f5f05b
|
|
@ -1,60 +0,0 @@
|
|||
import { HttpStatusCode } from 'axios';
|
||||
export const dynamic = 'force-dynamic';
|
||||
import { internalFetch } from '@gitroom/helpers/utils/internal.fetch';
|
||||
import { redirect } from 'next/navigation';
|
||||
import { Redirect } from '@gitroom/frontend/components/layout/redirect';
|
||||
import { getT } from '@gitroom/react/translation/get.translation.service.backend';
|
||||
export default async function Page({
|
||||
params: { provider },
|
||||
searchParams,
|
||||
}: {
|
||||
params: {
|
||||
provider: string;
|
||||
};
|
||||
searchParams: any;
|
||||
}) {
|
||||
const t = await getT();
|
||||
if (provider === 'x') {
|
||||
searchParams = {
|
||||
...searchParams,
|
||||
state: searchParams.oauth_token || '',
|
||||
code: searchParams.oauth_verifier || '',
|
||||
refresh: searchParams.refresh || '',
|
||||
};
|
||||
}
|
||||
if (provider === 'vk') {
|
||||
searchParams = {
|
||||
...searchParams,
|
||||
state: searchParams.state || '',
|
||||
code: searchParams.code + '&&&&' + searchParams.device_id,
|
||||
};
|
||||
}
|
||||
const data = await internalFetch(`/integrations/social/${provider}/connect`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(searchParams),
|
||||
});
|
||||
if (data.status === HttpStatusCode.NotAcceptable) {
|
||||
const { msg } = await data.json();
|
||||
return redirect(`/launches?msg=${msg}`);
|
||||
}
|
||||
if (
|
||||
data.status !== HttpStatusCode.Ok &&
|
||||
data.status !== HttpStatusCode.Created
|
||||
) {
|
||||
return (
|
||||
<>
|
||||
<div className="mt-[50px] text-[50px]">
|
||||
{t('could_not_add_provider', 'Could not add provider.')}
|
||||
<br />
|
||||
{t('you_are_being_redirected_back', 'You are being redirected back')}
|
||||
</div>
|
||||
<Redirect url="/launches" delay={3000} />
|
||||
</>
|
||||
);
|
||||
}
|
||||
const { inBetweenSteps, id } = await data.json();
|
||||
if (inBetweenSteps && !searchParams.refresh) {
|
||||
return redirect(`/launches?added=${provider}&continue=${id}`);
|
||||
}
|
||||
return redirect(`/launches?added=${provider}&msg=Channel Updated`);
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import { IntegrationRedirectComponent } from '@gitroom/frontend/components/launches/integration.redirect.component';
|
||||
import { ContinueIntegration } from '@gitroom/frontend/components/launches/continue.integration';
|
||||
export const dynamic = 'force-dynamic';
|
||||
export default async function Page({
|
||||
params: { provider },
|
||||
|
|
@ -9,5 +9,5 @@ export default async function Page({
|
|||
};
|
||||
searchParams: any;
|
||||
}) {
|
||||
return <IntegrationRedirectComponent />;
|
||||
return <ContinueIntegration searchParams={searchParams} provider={provider} />;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,79 @@
|
|||
'use client';
|
||||
|
||||
import { FC, useEffect, useState } from 'react';
|
||||
import { HttpStatusCode } from 'axios';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { Redirect } from '@gitroom/frontend/components/layout/redirect';
|
||||
import { useT } from '@gitroom/react/translation/get.transation.service.client';
|
||||
import { useFetch } from '@gitroom/helpers/utils/custom.fetch';
|
||||
import dayjs from 'dayjs';
|
||||
|
||||
export const ContinueIntegration: FC<{
|
||||
provider: string;
|
||||
searchParams: any;
|
||||
}> = (props) => {
|
||||
const { provider, searchParams } = props;
|
||||
const { push } = useRouter();
|
||||
const t = useT();
|
||||
const fetch = useFetch();
|
||||
const [error, setError] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
(async () => {
|
||||
const timezone = String(dayjs.tz().utcOffset());
|
||||
const modifiedParams = { ...searchParams };
|
||||
if (provider === 'x') {
|
||||
Object.assign(modifiedParams, {
|
||||
state: searchParams.oauth_token || '',
|
||||
code: searchParams.oauth_verifier || '',
|
||||
refresh: searchParams.refresh || '',
|
||||
});
|
||||
}
|
||||
|
||||
if (provider === 'vk') {
|
||||
Object.assign(modifiedParams, {
|
||||
...searchParams,
|
||||
state: searchParams.state || '',
|
||||
code: searchParams.code + '&&&&' + searchParams.device_id,
|
||||
});
|
||||
}
|
||||
|
||||
const data = await fetch(`/integrations/social/${provider}/connect`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({...modifiedParams, timezone}),
|
||||
});
|
||||
|
||||
if (data.status === HttpStatusCode.NotAcceptable) {
|
||||
const { msg } = await data.json();
|
||||
push(`/launches?msg=${msg}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
data.status !== HttpStatusCode.Ok &&
|
||||
data.status !== HttpStatusCode.Created
|
||||
) {
|
||||
setError(true);
|
||||
return;
|
||||
}
|
||||
|
||||
const { inBetweenSteps, id } = await data.json();
|
||||
if (inBetweenSteps && !searchParams.refresh) {
|
||||
push(`/launches?added=${provider}&continue=${id}`);
|
||||
return;
|
||||
}
|
||||
push(`/launches?added=${provider}&msg=Channel Updated`);
|
||||
})();
|
||||
}, [provider, searchParams]);
|
||||
|
||||
return error ? (
|
||||
<>
|
||||
<div className="mt-[50px] text-[50px]">
|
||||
{t('could_not_add_provider', 'Could not add provider.')}
|
||||
<br />
|
||||
{t('you_are_being_redirected_back', 'You are being redirected back')}
|
||||
</div>
|
||||
<Redirect url="/launches" delay={3000} />
|
||||
</>
|
||||
) : null;
|
||||
};
|
||||
Loading…
Reference in New Issue