PromucFlow_constructor/app/client/src/ce/pages/UserAuth/ThirdPartyAuth.tsx

109 lines
2.9 KiB
TypeScript
Raw Normal View History

2019-12-16 08:49:10 +00:00
import React from "react";
import styled from "styled-components";
import {
getSocialLoginButtonProps,
SocialLoginType,
} from "@appsmith/constants/SocialLogin";
feat: Renamed design system package (#19854) ## Description This PR includes changes for renaming design system package. Since we are building new package for the refactored design system components, the old package is renaming to design-system-old. Fixes #19536 ## Type of change - New feature (non-breaking change which adds functionality) - Breaking change (fix or feature that would cause existing functionality to not work as expected) ## How Has This Been Tested? - Manual - Jest - Cypress ### Test Plan > Add Testsmith test cases links that relate to this PR ### Issues raised during DP testing > Link issues raised during DP testing for better visiblity and tracking (copy link from comments dropped on this PR) ## Checklist: ### Dev activity - [x] My code follows the style guidelines of this project - [x] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas - [ ] I have made corresponding changes to the documentation - [x] My changes generate no new warnings - [ ] I have added tests that prove my fix is effective or that my feature works - [x] New and existing unit tests pass locally with my changes - [ ] PR is being merged under a feature flag ### QA activity: - [ ] Test plan has been approved by relevant developers - [ ] Test plan has been peer reviewed by QA - [ ] Cypress test cases have been added and approved by either SDET or manual QA - [ ] Organized project review call with relevant stakeholders after Round 1/2 of QA - [ ] Added Test Plan Approved label after reveiwing all Cypress test
2023-01-23 03:50:47 +00:00
import { getTypographyByKey } from "design-system-old";
2020-03-11 13:59:46 +00:00
import AnalyticsUtil, { EventName } from "utils/AnalyticsUtil";
import { useLocation } from "react-router-dom";
import PerformanceTracker, {
PerformanceTransactionName,
} from "utils/PerformanceTracker";
2019-12-16 08:49:10 +00:00
const ThirdPartyAuthWrapper = styled.div`
display: flex;
flex-direction: column;
`;
//TODO(abhinav): Port this to use themes.
const StyledSocialLoginButton = styled.a`
display: flex;
align-items: center;
justify-content: center;
border: solid 1px ${(props) => props.theme.colors.auth.socialBtnBorder};
margin-bottom: ${(props) => props.theme.spaces[4]}px;
&:only-child, &:last-child {
margin-bottom: 0;
}
2019-12-16 08:49:10 +00:00
&:hover {
text-decoration: none;
background-color: ${(props) => props.theme.colors.auth.socialBtnHighlight};
2019-12-16 08:49:10 +00:00
}
& .login-method {
${getTypographyByKey("btnLarge")}
color: ${(props) => props.theme.colors.auth.socialBtnText};
text-transform: uppercase;
2019-12-16 08:49:10 +00:00
}
`;
const ButtonLogo = styled.img`
margin: ${(props) => props.theme.spaces[2]}px;
width: 24px;
`;
export const SocialLoginTypes = {
2019-12-16 08:49:10 +00:00
GOOGLE: "google",
GITHUB: "github",
};
2020-03-11 13:59:46 +00:00
type SignInType = "SIGNIN" | "SIGNUP";
function SocialLoginButton(props: {
2019-12-16 08:49:10 +00:00
logo: string;
name: string;
url: string;
label?: string;
2020-03-11 13:59:46 +00:00
type: SignInType;
}) {
const location = useLocation();
const queryParams = new URLSearchParams(location.search);
let url = props.url;
const redirectUrl = queryParams.get("redirectUrl");
if (redirectUrl != null) {
url += `?redirectUrl=${encodeURIComponent(redirectUrl)}`;
}
2019-12-16 08:49:10 +00:00
return (
2020-03-11 13:59:46 +00:00
<StyledSocialLoginButton
href={url}
2020-03-11 13:59:46 +00:00
onClick={() => {
let eventName: EventName = "LOGIN_CLICK";
if (props.type === "SIGNUP") {
eventName = "SIGNUP_CLICK";
}
PerformanceTracker.startTracking(
eventName === "SIGNUP_CLICK"
? PerformanceTransactionName.SIGN_UP
: PerformanceTransactionName.LOGIN_CLICK,
{ name: props.name.toUpperCase() },
);
2020-03-11 13:59:46 +00:00
AnalyticsUtil.logEvent(eventName, {
loginMethod: props.name.toUpperCase(),
});
}}
>
<ButtonLogo alt={` ${props.name} login`} src={props.logo} />
2022-04-21 06:14:02 +00:00
<div className="login-method" data-testid={`login-with-${props.name}`}>
{props.label ?? `continue with ${props.name}`}
</div>
2019-12-16 08:49:10 +00:00
</StyledSocialLoginButton>
);
}
2019-12-16 08:49:10 +00:00
export function ThirdPartyAuth(props: {
2020-03-11 13:59:46 +00:00
logins: SocialLoginType[];
type: SignInType;
}) {
2019-12-16 08:49:10 +00:00
const socialLoginButtons = getSocialLoginButtonProps(props.logins).map(
2020-12-24 04:32:25 +00:00
(item) => {
return <SocialLoginButton key={item.name} {...item} type={props.type} />;
2019-12-16 08:49:10 +00:00
},
);
return <ThirdPartyAuthWrapper>{socialLoginButtons}</ThirdPartyAuthWrapper>;
}
2019-12-16 08:49:10 +00:00
export default ThirdPartyAuth;