2023-10-04 19:04:09 +00:00
|
|
|
import { clsx } from 'clsx'
|
|
|
|
import styles from './AuthorBadge.module.scss'
|
|
|
|
import { Userpic } from '../Userpic'
|
|
|
|
import { Author, FollowingEntity } from '../../../graphql/types.gen'
|
|
|
|
import { createMemo, createSignal, Show } from 'solid-js'
|
|
|
|
import { formatDate } from '../../../utils'
|
|
|
|
import { useLocalize } from '../../../context/localize'
|
|
|
|
import { Button } from '../../_shared/Button'
|
|
|
|
import { useSession } from '../../../context/session'
|
|
|
|
import { follow, unfollow } from '../../../stores/zine/common'
|
|
|
|
import { CheckButton } from '../../_shared/CheckButton'
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
author: Author
|
|
|
|
minimizeSubscribeButton?: boolean
|
|
|
|
}
|
|
|
|
export const AuthorBadge = (props: Props) => {
|
|
|
|
const [isSubscribing, setIsSubscribing] = createSignal(false)
|
|
|
|
const {
|
|
|
|
session,
|
2023-10-05 04:24:01 +00:00
|
|
|
actions: { loadSession, requireAuthentication }
|
2023-10-04 19:04:09 +00:00
|
|
|
} = useSession()
|
|
|
|
|
|
|
|
const { t } = useLocalize()
|
|
|
|
const subscribed = createMemo<boolean>(() => {
|
|
|
|
return session()?.news?.authors?.some((u) => u === props.author.slug) || false
|
|
|
|
})
|
|
|
|
|
|
|
|
const subscribe = async (really = true) => {
|
|
|
|
setIsSubscribing(true)
|
|
|
|
|
|
|
|
await (really
|
|
|
|
? follow({ what: FollowingEntity.Author, slug: props.author.slug })
|
|
|
|
: unfollow({ what: FollowingEntity.Author, slug: props.author.slug }))
|
|
|
|
|
|
|
|
await loadSession()
|
|
|
|
setIsSubscribing(false)
|
|
|
|
}
|
2023-10-05 04:24:01 +00:00
|
|
|
const handleSubscribe = (really: boolean) => {
|
|
|
|
requireAuthentication(() => {
|
|
|
|
subscribe(really)
|
|
|
|
}, 'subscribe')
|
|
|
|
}
|
2023-10-04 19:04:09 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div class={clsx(styles.AuthorBadge)}>
|
|
|
|
<Userpic hasLink={true} isMedium={true} name={props.author.name} userpic={props.author.userpic} />
|
|
|
|
<a href={`/author/${props.author.slug}`} class={styles.info}>
|
|
|
|
<div class={styles.name}>{props.author.name}</div>
|
|
|
|
<Show
|
|
|
|
when={props.author.bio}
|
|
|
|
fallback={
|
|
|
|
<div class={styles.bio}>
|
|
|
|
{t('Registered since {{date}}', { date: formatDate(new Date(props.author.createdAt)) })}
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
>
|
2023-10-05 04:24:01 +00:00
|
|
|
<div class={clsx('text-truncate', styles.bio)} innerHTML={props.author.bio} />
|
2023-10-04 19:04:09 +00:00
|
|
|
</Show>
|
|
|
|
</a>
|
2023-10-05 04:24:01 +00:00
|
|
|
<Show when={props.author.slug !== session()?.user.slug}>
|
2023-10-04 19:04:09 +00:00
|
|
|
<div class={styles.actions}>
|
|
|
|
<Show
|
|
|
|
when={!props.minimizeSubscribeButton}
|
|
|
|
fallback={
|
|
|
|
<CheckButton
|
|
|
|
text={t('Follow')}
|
|
|
|
checked={subscribed()}
|
2023-10-05 04:24:01 +00:00
|
|
|
onClick={() => handleSubscribe(!subscribed())}
|
2023-10-04 19:04:09 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Show
|
|
|
|
when={subscribed()}
|
|
|
|
fallback={
|
|
|
|
<Button
|
|
|
|
variant="primary"
|
|
|
|
size="S"
|
|
|
|
value={isSubscribing() ? t('...subscribing') : t('Subscribe')}
|
2023-10-05 04:24:01 +00:00
|
|
|
onClick={() => handleSubscribe(true)}
|
2023-10-04 19:04:09 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<Button
|
|
|
|
variant="secondary"
|
|
|
|
size="S"
|
|
|
|
value={t('You are subscribed')}
|
2023-10-05 04:24:01 +00:00
|
|
|
onClick={() => handleSubscribe(false)}
|
2023-10-04 19:04:09 +00:00
|
|
|
/>
|
|
|
|
</Show>
|
|
|
|
</Show>
|
|
|
|
</div>
|
|
|
|
</Show>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|