webapp/src/components/Topic/Full.tsx

60 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-10-31 16:40:55 +00:00
import { createMemo, Show } from 'solid-js'
2022-09-09 11:53:35 +00:00
import type { Topic } from '../../graphql/types.gen'
import { FollowingEntity } from '../../graphql/types.gen'
2022-11-07 21:07:42 +00:00
import styles from './Full.module.scss'
2022-09-09 11:53:35 +00:00
import { follow, unfollow } from '../../stores/zine/common'
2023-02-17 09:21:02 +00:00
2022-11-07 21:07:42 +00:00
import { clsx } from 'clsx'
2022-11-14 10:02:08 +00:00
import { useSession } from '../../context/session'
2023-02-17 09:21:02 +00:00
import { useLocalize } from '../../context/localize'
2022-09-09 11:53:35 +00:00
type Props = {
topic: Topic
}
export const FullTopic = (props: Props) => {
const {
subscriptions,
actions: { requireAuthentication }
} = useSession()
2023-02-17 09:21:02 +00:00
const { t } = useLocalize()
const subscribed = createMemo(() =>
subscriptions().topics.some((topic) => topic.slug === props.topic?.slug)
)
const handleSubscribe = (isFollowed: boolean) => {
requireAuthentication(() => {
if (isFollowed) {
unfollow({ what: FollowingEntity.Topic, slug: props.topic.slug })
} else {
follow({ what: FollowingEntity.Topic, slug: props.topic.slug })
}
}, 'follow')
}
2022-09-09 11:53:35 +00:00
return (
2023-03-10 17:42:48 +00:00
<div class={clsx(styles.topicHeader, 'col-md-16 col-lg-12 offset-md-4 offset-lg-6')}>
2023-02-17 09:21:02 +00:00
<h1>#{props.topic.title}</h1>
<p>{props.topic.body}</p>
<div class={clsx(styles.topicActions)}>
<Show when={!subscribed()}>
<button onClick={() => handleSubscribe(false)} class="button">
2023-02-17 09:21:02 +00:00
{t('Follow the topic')}
</button>
</Show>
<Show when={subscribed()}>
<button onClick={() => handleSubscribe(true)} class="button">
2023-02-17 09:21:02 +00:00
{t('Unfollow the topic')}
</button>
</Show>
2023-04-11 13:57:48 +00:00
<a href={`/create/?topicId=${props.topic.id}`}>{t('Write about the topic')}</a>
2023-02-17 09:21:02 +00:00
</div>
<Show when={props.topic.pic}>
<img src={props.topic.pic} alt={props.topic.title} />
2022-11-07 21:07:42 +00:00
</Show>
2022-09-09 11:53:35 +00:00
</div>
)
}