Skip to content

Commit

Permalink
chore: npm prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
huglx committed Nov 12, 2024
1 parent 6426f6f commit 9f10844
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 138 deletions.
235 changes: 119 additions & 116 deletions webapp/src/component/security/ChangeAuthProviderView.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {DashboardPage} from "tg.component/layout/DashboardPage";
import {CompactView} from "tg.component/layout/CompactView";
import {Box, styled} from "@mui/material";
import {T, useTranslate} from "@tolgee/react";
import {LINKS} from "tg.constants/links";
import LoadingButton from "tg.component/common/form/LoadingButton";
import {globalContext} from "tg.globalContext/globalActions";
import {useApiMutation, useApiQuery} from "tg.service/http/useQueryApi";
import React from "react";
import {Alert} from "tg.component/common/Alert";
import {TranslatedError} from "tg.translationTools/TranslatedError";
import { DashboardPage } from 'tg.component/layout/DashboardPage';
import { CompactView } from 'tg.component/layout/CompactView';
import { Box, styled } from '@mui/material';
import { T, useTranslate } from '@tolgee/react';
import { LINKS } from 'tg.constants/links';
import LoadingButton from 'tg.component/common/form/LoadingButton';
import { globalContext } from 'tg.globalContext/globalActions';
import { useApiMutation, useApiQuery } from 'tg.service/http/useQueryApi';
import React from 'react';
import { Alert } from 'tg.component/common/Alert';
import { TranslatedError } from 'tg.translationTools/TranslatedError';

const StyledContainer = styled('div')`
padding: 28px 4px 0px 4px;
Expand Down Expand Up @@ -37,118 +37,121 @@ const StyledDescription = styled('div')`
const LOCAL_STORAGE_CHANGE_PROVIDER = 'change_provider';

function getProviderName(accountType?: string, authType?: string) {
if (accountType === "LOCAL") {
return 'username and password';
} else if (accountType === "THIRD_PARTY") {
return authType || 'Third-Party Provider';
}
return 'Unknown Provider';
if (accountType === 'LOCAL') {
return 'username and password';
} else if (accountType === 'THIRD_PARTY') {
return authType || 'Third-Party Provider';
}
return 'Unknown Provider';
}

export function ChangeAuthProviderView() {
const { t } = useTranslate();
const requestIdString = localStorage.getItem(LOCAL_STORAGE_CHANGE_PROVIDER);
const requestId = requestIdString ? Number(requestIdString) : 0;

const { data } = useApiQuery({
url: '/api/public/auth-provider/get-request',
method: 'get',
query: {
requestId
const { t } = useTranslate();
const requestIdString = localStorage.getItem(LOCAL_STORAGE_CHANGE_PROVIDER);
const requestId = requestIdString ? Number(requestIdString) : 0;

const { data } = useApiQuery({
url: '/api/public/auth-provider/get-request',
method: 'get',
query: {
requestId,
},
});

const { error, isSuccess, isLoading, mutate } = useApiMutation({
url: '/api/public/auth-provider/request-change',
method: 'post',
});

const currentProvider = getProviderName(
data?.oldAccountType,
data?.oldAuthType
);
const newProvider = getProviderName(data?.newAccountType, data?.newAuthType);

const confirmChange = () => {
mutate({
content: {
'application/json': {
changeRequestId: requestId,
isConfirmed: true,
},
},
});

const {error, isSuccess, isLoading, mutate } = useApiMutation({
url: '/api/public/auth-provider/request-change',
method: 'post',
if (isSuccess) {
globalContext.actions?.redirectTo(LINKS.LOGIN.build());
}
};

const discardChange = () => {
mutate({
content: {
'application/json': {
changeRequestId: requestId,
isConfirmed: false,
},
},
});

const currentProvider = getProviderName(data?.oldAccountType, data?.oldAuthType);
const newProvider = getProviderName(data?.newAccountType, data?.newAuthType);

const confirmChange = () => {
mutate({
content: {
'application/json': {
changeRequestId: requestId,
isConfirmed: true,
},
},
});

if(isSuccess) {
globalContext.actions?.redirectTo(LINKS.LOGIN.build());
if (isSuccess) {
globalContext.actions?.redirectTo(LINKS.LOGIN.build());
}
};

return (
<DashboardPage>
<CompactView
alerts={
error?.code &&
!isLoading && (
<Alert severity="error">
<TranslatedError code={error.code} />
</Alert>
)
}
};

const discardChange = () => {
mutate({
content: {
'application/json': {
changeRequestId: requestId,
isConfirmed: false,
},
},
});

if(isSuccess) {
globalContext.actions?.redirectTo(LINKS.LOGIN.build());
maxWidth={800}
windowTitle={t('slack_connect_title')}
primaryContent={
<StyledContainer>
<Box display="flex" justifyContent="center"></Box>
<StyledTitle>
<T keyName="change_auth_provider_title" />
</StyledTitle>

<StyledDescription>
<T
keyName="change_auth_provider_description"
params={{
current_provider: currentProvider,
new_provider: newProvider,
b: <strong />,
}}
/>
</StyledDescription>

<Box display="flex" gap={2} mb={1.5}>
<LoadingButton
size="medium"
variant="outlined"
loading={isLoading}
onClick={discardChange}
>
<T keyName="change_auth_provider_cancel" />
</LoadingButton>
<LoadingButton
size="medium"
variant="contained"
color="primary"
loading={isLoading}
onClick={confirmChange}
>
<T keyName="change_auth_provider_confirm" />
</LoadingButton>
</Box>
</StyledContainer>
}
};

return (
<DashboardPage>
<CompactView
alerts={
error?.code &&
!isLoading && (
<Alert severity="error">
<TranslatedError code={error.code} />
</Alert>
)
}
maxWidth={800}
windowTitle={t('slack_connect_title')}
primaryContent={
<StyledContainer>
<Box display="flex" justifyContent="center">
</Box>
<StyledTitle>
<T keyName="change_auth_provider_title" />
</StyledTitle>

<StyledDescription>
<T keyName="change_auth_provider_description"
params={{
current_provider: currentProvider,
new_provider: newProvider,
b: <strong />
}}
/>
</StyledDescription>

<Box display="flex" gap={2} mb={1.5}>
<LoadingButton
size="medium"
variant="outlined"
loading={isLoading}
onClick={discardChange}
>
<T keyName="change_auth_provider_cancel" />
</LoadingButton>
<LoadingButton
size="medium"
variant="contained"
color="primary"
loading={isLoading}
onClick={confirmChange}
>
<T keyName="change_auth_provider_confirm" />
</LoadingButton>
</Box>
</StyledContainer>
}
/>
</DashboardPage>
)
}
/>
</DashboardPage>
);
}
18 changes: 9 additions & 9 deletions webapp/src/component/security/Login/LoginRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import {default as React, FunctionComponent} from 'react';
import {Route, Switch} from 'react-router-dom';
import { default as React, FunctionComponent } from 'react';
import { Route, Switch } from 'react-router-dom';

import {LINKS} from 'tg.constants/links';
import { LINKS } from 'tg.constants/links';

import {LoginView} from './LoginView';
import {EmailVerificationHandler} from './EmailVerificationHandler';
import {OAuthRedirectionHandler} from './OAuthRedirectionHandler';
import {PublicOnlyRoute} from 'tg.component/common/PublicOnlyRoute';
import {SsoRedirectionHandler} from 'tg.component/security/Sso/SsoRedirectionHandler';
import {ChangeAuthProviderView} from "tg.component/security/ChangeAuthProviderView";
import { LoginView } from './LoginView';
import { EmailVerificationHandler } from './EmailVerificationHandler';
import { OAuthRedirectionHandler } from './OAuthRedirectionHandler';
import { PublicOnlyRoute } from 'tg.component/common/PublicOnlyRoute';
import { SsoRedirectionHandler } from 'tg.component/security/Sso/SsoRedirectionHandler';
import { ChangeAuthProviderView } from 'tg.component/security/ChangeAuthProviderView';

interface LoginRouterProps {}

Expand Down
4 changes: 2 additions & 2 deletions webapp/src/constants/links.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ export class LINKS {
);

static CHANGE_AUTH_PROVIDER = Link.ofParent(
LINKS.LOGIN,
'change-auth-provider'
LINKS.LOGIN,
'change-auth-provider'
);

static OPENID_RESPONSE = Link.ofParent(LINKS.LOGIN, 'open-id/auth-callback');
Expand Down
30 changes: 19 additions & 11 deletions webapp/src/service/http/ApiHttpService.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {LINKS} from 'tg.constants/links';
import {GlobalError} from 'tg.error/GlobalError';
import { LINKS } from 'tg.constants/links';
import { GlobalError } from 'tg.error/GlobalError';

import {tokenService} from '../TokenService';
import {getUtmCookie} from 'tg.fixtures/utmCookie';
import {handleApiError} from './handleApiError';
import {ApiError} from './ApiError';
import {errorAction} from './errorAction';
import {globalContext} from 'tg.globalContext/globalActions';
import { tokenService } from '../TokenService';
import { getUtmCookie } from 'tg.fixtures/utmCookie';
import { handleApiError } from './handleApiError';
import { ApiError } from './ApiError';
import { errorAction } from './errorAction';
import { globalContext } from 'tg.globalContext/globalActions';

const LOCAL_STORAGE_CHANGE_PROVIDER = 'change_provider';

Expand Down Expand Up @@ -66,9 +66,17 @@ export class ApiHttpService {
handleApiError(r, responseData, init, options)
);

if(r.status === 401 && responseData.code === 'username_already_exists') {
localStorage.setItem(LOCAL_STORAGE_CHANGE_PROVIDER, resultError.data?.params?.[0]);
globalContext.actions?.redirectToLink(LINKS.CHANGE_AUTH_PROVIDER);
if (
r.status === 401 &&
responseData.code === 'username_already_exists'
) {
localStorage.setItem(
LOCAL_STORAGE_CHANGE_PROVIDER,
resultError.data?.params?.[0]
);
globalContext.actions?.redirectToLink(
LINKS.CHANGE_AUTH_PROVIDER
);
}

if (r.status === 400) {
Expand Down

0 comments on commit 9f10844

Please sign in to comment.