Skip to content

Commit

Permalink
feat: update JobCard redirect logic to handle internalRole case
Browse files Browse the repository at this point in the history
  • Loading branch information
Luisfp0 committed Oct 29, 2024
1 parent da72c7d commit ed7dcf8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
19 changes: 19 additions & 0 deletions apps/web/app/components/isInternalRole.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use server'

import { getSupabaseClient } from 'db'

export async function isInternalRole(roleId: string): Promise<boolean> {
const supabase = getSupabaseClient()

const { data, error } = await supabase
.from('RoleOwner')
.select('*')
.eq('roleID', roleId)
.single()

if (error) {
return false
}

return Boolean(data)
}
44 changes: 35 additions & 9 deletions apps/web/app/components/ui/JobCard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
'use client'

import { shouldRedirectToUrl } from 'app/utils/shouldRedirect'
import { Database } from 'db'
import { useCallback, useMemo } from 'react'
import { trackedRoleURL } from 'shared/src/services/trackedRoleURL'
import { isInternalRole } from '../isInternalRole'

type Job = Database['public']['Tables']['Roles']['Row']
type Skill = Database['public']['Views']['vw_skills_in_roles']['Row']
Expand Down Expand Up @@ -37,19 +40,42 @@ const JobCard: React.FC<JobCardProps> = ({
(event) => {
event.preventDefault()

const redirectToInternalPage = () => {
window.open(`/vaga/${job.id}`, '_blank')
}

const redirectToExternalUrl = () => {
window.open(job.url, '_blank')
}

fetch(trackedRoleURL(job.id), { method: 'POST' })
.then(() => {
const linkUrl = shouldRedirectToUrl(job.description)
? job.url
: `/vaga/${job.id}`
window.open(linkUrl, '_blank')
.then(async () => {
try {
const isInternal = await isInternalRole(job.id)

if (isInternal) {
redirectToInternalPage()
} else if (shouldRedirectToUrl(job.description)) {
redirectToInternalPage()
} else {
redirectToExternalUrl()
}
} catch (error) {
console.error('Erro ao verificar tipo de vaga:', error)
if (shouldRedirectToUrl(job.description)) {
redirectToInternalPage()
} else {
redirectToExternalUrl()
}
}
})
.catch((error) => {
console.error('Erro ao contabilizar clique:', error)
const linkUrl = shouldRedirectToUrl(job.description)
? job.url
: `/vaga/${job.id}`
window.open(linkUrl, '_blank')
if (shouldRedirectToUrl(job.description)) {
redirectToInternalPage()
} else {
redirectToExternalUrl()
}
})
},
[job.id, job.description, job.url]
Expand Down

0 comments on commit ed7dcf8

Please sign in to comment.