webapp/src/components/Inbox/CreateModalContent.tsx

117 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-11-27 05:49:48 +00:00
import { createSignal, For, createEffect } from 'solid-js'
import styles from './CreateModalContent.module.scss'
import { t } from '../../utils/intl'
import InviteUser from './InviteUser'
import type { Author } from '../../graphql/types.gen'
import { hideModal } from '../../stores/ui'
import { useInbox } from '../../context/inbox'
2022-12-02 11:11:45 +00:00
type InvitingUser = Author & {
selected: boolean
}
2022-11-27 05:49:48 +00:00
type query =
| {
theme: string
members: string[]
}
| undefined
type Props = {
users: Author[]
}
const CreateModalContent = (props: Props) => {
2022-12-02 11:11:45 +00:00
const inviteUsers: InvitingUser[] = props.users.map((user) => ({ ...user, selected: false }))
const [title, setTitle] = createSignal<string>('')
const [uids, setUids] = createSignal<number[]>([])
const [collectionToInvite, setCollectionToInvite] = createSignal<InvitingUser[]>(inviteUsers)
2022-11-27 05:49:48 +00:00
let textInput: HTMLInputElement
const reset = () => {
2022-12-02 11:11:45 +00:00
setTitle('')
setUids([])
2022-11-27 05:49:48 +00:00
hideModal()
}
createEffect(() => {
2022-12-02 11:11:45 +00:00
console.log(collectionToInvite())
setUids(() => {
2022-11-27 05:49:48 +00:00
return collectionToInvite()
2022-12-02 11:11:45 +00:00
.filter((user: InvitingUser) => {
2022-11-27 05:49:48 +00:00
return user.selected === true
})
2022-12-02 11:11:45 +00:00
.map((user: InvitingUser) => {
return user.id
2022-11-27 05:49:48 +00:00
})
})
2022-12-02 11:11:45 +00:00
if (uids().length > 1 && title().length === 0) {
setTitle(t('group_chat'))
2022-11-27 05:49:48 +00:00
}
})
const handleSetTheme = () => {
2022-12-02 11:11:45 +00:00
setTitle(textInput.value.length > 0 && textInput.value)
2022-11-27 05:49:48 +00:00
}
const handleClick = (user) => {
2022-12-02 11:11:45 +00:00
setCollectionToInvite((userCollection: InvitingUser[]) => {
return userCollection.map((clickedUser: InvitingUser) =>
2022-11-27 05:49:48 +00:00
user.slug === clickedUser.slug ? { ...clickedUser, selected: !clickedUser.selected } : clickedUser
)
})
}
const { chatEntities, actions } = useInbox()
console.log('!!! chatEntities:', chatEntities)
const handleCreate = async () => {
try {
2022-12-02 11:11:45 +00:00
const initChat = await actions.createChat(uids(), title())
2022-11-27 05:49:48 +00:00
console.debug('[initChat]', initChat)
} catch (error) {
console.error(error)
}
}
return (
<div class={styles.CreateModalContent}>
<h4>{t('create_chat')}</h4>
2022-12-02 11:11:45 +00:00
{uids().length > 1 && (
2022-11-27 05:49:48 +00:00
<input
ref={textInput}
onInput={handleSetTheme}
type="text"
required={true}
class="form-control form-control-lg fs-3"
placeholder={t('discourse_theme')}
/>
)}
<div class="invite-recipients" style={{ height: '400px', overflow: 'auto' }}>
<For each={collectionToInvite()}>
2022-12-02 11:11:45 +00:00
{(author: InvitingUser) => (
2022-11-27 05:49:48 +00:00
<InviteUser onClick={() => handleClick(author)} author={author} selected={author.selected} />
)}
</For>
</div>
<div class={styles.footer}>
<button type="button" class="btn btn-lg fs-3 btn-outline-danger" onClick={reset}>
{t('cancel')}
</button>
<button
type="button"
class="btn btn-lg fs-3 btn-outline-primary"
onClick={handleCreate}
2022-12-02 11:11:45 +00:00
disabled={uids().length === 0}
2022-11-27 05:49:48 +00:00
>
2022-12-02 11:11:45 +00:00
{uids().length > 1 ? t('create_group') : t('create_chat')}
2022-11-27 05:49:48 +00:00
</button>
</div>
</div>
)
}
export default CreateModalContent