2021-11-13 21:50:34 +01:00
|
|
|
import Image from 'next/image'
|
2022-03-05 18:22:30 +01:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { useState } from 'react'
|
2022-01-01 20:42:25 +01:00
|
|
|
import useTranslation from 'next-translate/useTranslation'
|
2022-02-09 23:27:54 +01:00
|
|
|
import classNames from 'classnames'
|
2022-03-05 18:22:30 +01:00
|
|
|
import axios from 'axios'
|
2022-02-09 23:27:54 +01:00
|
|
|
|
2022-03-16 12:18:09 +01:00
|
|
|
import { Emoji } from '../../../Emoji'
|
|
|
|
import { ConfirmGuildJoin } from '../../ConfirmGuildJoin'
|
|
|
|
import { API_URL } from '../../../../tools/api'
|
2022-03-05 18:22:30 +01:00
|
|
|
import {
|
|
|
|
GuildPublic as GuildPublicType,
|
|
|
|
GuildWithDefaultChannelId
|
|
|
|
} from '../../../../models/Guild'
|
|
|
|
import { useAuthentication } from '../../../../tools/authentication'
|
2021-11-13 21:50:34 +01:00
|
|
|
|
2022-01-01 20:42:25 +01:00
|
|
|
export interface GuildPublicProps {
|
|
|
|
guild: GuildPublicType
|
2021-11-13 21:50:34 +01:00
|
|
|
}
|
|
|
|
|
2022-01-01 20:42:25 +01:00
|
|
|
export const GuildPublic: React.FC<GuildPublicProps> = (props) => {
|
2021-11-13 21:50:34 +01:00
|
|
|
const { guild } = props
|
2022-03-05 18:22:30 +01:00
|
|
|
const router = useRouter()
|
|
|
|
const { authentication } = useAuthentication()
|
2022-02-09 23:27:54 +01:00
|
|
|
const [isConfirmed, setIsConfirmed] = useState(false)
|
2022-01-01 20:42:25 +01:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2022-03-05 18:22:30 +01:00
|
|
|
const handleIsConfirmed = (): void => {
|
|
|
|
setIsConfirmed((isConfirmed) => !isConfirmed)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleYes = async (): Promise<void> => {
|
|
|
|
try {
|
|
|
|
const { data } = await authentication.api.post<{
|
|
|
|
guild: GuildWithDefaultChannelId
|
|
|
|
}>(`/guilds/${guild.id}/members/join`)
|
|
|
|
await router.push(
|
|
|
|
`/application/${guild.id}/${data.guild.defaultChannelId}`
|
|
|
|
)
|
|
|
|
} catch (error) {
|
|
|
|
if (
|
|
|
|
axios.isAxiosError(error) &&
|
|
|
|
error.response?.status === 400 &&
|
|
|
|
typeof error.response?.data.defaultChannelId === 'number'
|
|
|
|
) {
|
|
|
|
const defaultChannelId = error.response.data.defaultChannelId as number
|
|
|
|
await router.push(`/application/${guild.id}/${defaultChannelId}`)
|
|
|
|
} else {
|
|
|
|
await router.push('/application')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-13 21:50:34 +01:00
|
|
|
return (
|
2022-03-16 11:25:44 +01:00
|
|
|
<div className='relative h-80 overflow-hidden rounded border border-gray-500 shadow-lg transition duration-200 ease-in-out hover:-translate-y-2 hover:shadow-none dark:border-gray-700'>
|
2022-02-09 23:27:54 +01:00
|
|
|
<div
|
|
|
|
className={classNames(
|
2022-02-19 23:20:33 +01:00
|
|
|
'flex h-full cursor-pointer flex-col items-center justify-center p-4 pt-8 transition duration-200 ease-in-out',
|
2022-02-09 23:27:54 +01:00
|
|
|
{ '-translate-x-full': isConfirmed }
|
|
|
|
)}
|
2022-03-05 18:22:30 +01:00
|
|
|
onClick={handleIsConfirmed}
|
2022-02-09 23:27:54 +01:00
|
|
|
>
|
|
|
|
<Image
|
2022-04-07 16:54:05 +02:00
|
|
|
quality={100}
|
2022-02-09 23:27:54 +01:00
|
|
|
className='rounded-full'
|
|
|
|
src={
|
2022-03-05 18:22:30 +01:00
|
|
|
guild.icon != null
|
|
|
|
? API_URL + guild.icon
|
|
|
|
: '/images/data/guild-default.png'
|
2022-02-09 23:27:54 +01:00
|
|
|
}
|
|
|
|
alt='logo'
|
|
|
|
width={80}
|
|
|
|
height={80}
|
|
|
|
/>
|
2022-02-19 23:20:33 +01:00
|
|
|
<div className='m-2 mt-6 w-full px-4 text-center'>
|
2022-02-09 23:27:54 +01:00
|
|
|
<h3
|
|
|
|
data-cy='guild-name'
|
2022-02-19 23:20:33 +01:00
|
|
|
className='center mb-2 w-full truncate text-xl font-bold'
|
2022-02-09 23:27:54 +01:00
|
|
|
>
|
|
|
|
{guild.name}
|
|
|
|
</h3>
|
|
|
|
<p className='break-words'>
|
|
|
|
{guild.description != null ? (
|
|
|
|
guild.description
|
|
|
|
) : (
|
2022-03-16 11:25:44 +01:00
|
|
|
<span className='flex h-full items-center justify-center opacity-40 dark:opacity-20'>
|
2022-02-09 23:27:54 +01:00
|
|
|
<Emoji value=':eyes:' size={25} />
|
2022-03-16 11:25:44 +01:00
|
|
|
<span className='ml-2'>{t('application:nothing-here')}</span>
|
2022-02-09 23:27:54 +01:00
|
|
|
</span>
|
|
|
|
)}
|
|
|
|
</p>
|
|
|
|
</div>
|
2022-02-19 23:20:33 +01:00
|
|
|
<p className='mt-auto flex flex-col text-green-800 dark:text-green-400'>
|
2022-02-09 23:27:54 +01:00
|
|
|
{guild.membersCount} {t('application:members')}
|
|
|
|
</p>
|
2021-11-13 21:50:34 +01:00
|
|
|
</div>
|
2022-02-09 23:27:54 +01:00
|
|
|
<ConfirmGuildJoin
|
|
|
|
className={classNames(
|
2022-02-19 23:20:33 +01:00
|
|
|
'w-ful h-ful translate-x- absolute top-1/2 left-full flex h-full w-full -translate-y-1/2 flex-col items-center justify-center rounded-2xl transition-all',
|
2022-02-09 23:27:54 +01:00
|
|
|
{
|
|
|
|
'!left-0': isConfirmed
|
|
|
|
}
|
|
|
|
)}
|
2022-03-05 18:22:30 +01:00
|
|
|
handleYes={handleYes}
|
|
|
|
handleNo={handleIsConfirmed}
|
2022-02-09 23:27:54 +01:00
|
|
|
/>
|
2021-11-13 21:50:34 +01:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|