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

Rewrite GoogleButton to tsx format and fix the sonar issues #2242

Merged
merged 1 commit into from
Aug 12, 2024
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
60 changes: 0 additions & 60 deletions src/containers/guest-home-page/google-button/GoogleButton.jsx

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export const styles = {
height: '44px',
width: '100%',
overflow: 'hidden',
minWidth: { sm: '315px' }
minWidth: '315px'
}
}
113 changes: 113 additions & 0 deletions src/containers/guest-home-page/google-button/GoogleButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { useCallback, useEffect } from 'react'
import { useHref } from 'react-router-dom'

import { useGoogleAuthMutation } from '~/services/auth-service'
import { useModalContext } from '~/context/modal-context'
import { useAppDispatch } from '~/hooks/use-redux'
import { scrollToHash } from '~/utils/hash-scroll'

import { snackbarVariants } from '~/constants'
import { styles } from '~/containers/guest-home-page/google-button/GoogleButton.styles'
import { openAlert } from '~/redux/features/snackbarSlice'
import { getErrorKey } from '~/utils/get-error-key'
import { UserRole } from '~/types'

interface GoogleAuth {
initialize: (config: {
client_id: string
callback: (token: string) => void
}) => void
renderButton: (
container: HTMLElement,
options: { size: string; width: string; locale: string; text: string }
) => void
}

declare global {
interface Window {
google: {
accounts: {
id: GoogleAuth
}
}
}
}

interface GoogleButtonProps {
role: UserRole
route: string
buttonWidth: string
type: string
}

interface GoogleId {
initialize: (config: {
client_id: string
callback: (token: string) => void
}) => void
renderButton: (
container: HTMLElement,
options: { size: string; width: string; locale: string; text: string }
) => void
}

const GoogleButton: React.FC<GoogleButtonProps> = ({
role,
route,
buttonWidth,
type
}) => {
const ref = useHref(route)
const { closeModal } = useModalContext()
const dispatch = useAppDispatch()
const [googleAuth] = useGoogleAuthMutation()

const handleCredentialResponse = useCallback(
async (token: string) => {
try {
await googleAuth({ token, role }).unwrap()
closeModal()
} catch (e) {
const error = e as { data: { code: string } }
dispatch(
openAlert({
severity: snackbarVariants.error,
message: getErrorKey(error.data)
})
)
if (error.data.code === 'USER_NOT_FOUND') {
closeModal()
scrollToHash(ref)
}
}
},
[googleAuth, role, closeModal, dispatch, ref]
)

useEffect(() => {
const googleId = window.google.accounts.id as GoogleId

googleId.initialize({
client_id: import.meta.env.VITE_GMAIL_CLIENT_ID,
callback: (token: string) => {
handleCredentialResponse(token).catch((error) => {
console.error('Error handling credential response:', error)
})
}
})

googleId.renderButton(
document.getElementById('googleButton') as HTMLElement,
{
size: 'large',
width: buttonWidth,
locale: 'en',
text: `${type}_with`
}
)
}, [handleCredentialResponse, buttonWidth, type])

return <div id='googleButton' style={styles.google} />
}

export default GoogleButton
Loading