webapp/src/components/Inbox/CreateModalContent.tsx

110 lines
3.0 KiB
TypeScript
Raw Normal View History

2023-11-28 13:18:25 +00:00
import type { Author } from '../../graphql/schema/core.gen'
2024-02-04 11:25:21 +00:00
import { For, createEffect, createSignal } from 'solid-js'
2023-02-17 09:21:02 +00:00
2022-11-27 05:49:48 +00:00
import { useInbox } from '../../context/inbox'
2023-02-17 09:21:02 +00:00
import { useLocalize } from '../../context/localize'
import { hideModal } from '../../stores/ui'
import InviteUser from './InviteUser'
import styles from './CreateModalContent.module.scss'
2022-11-27 05:49:48 +00:00
2022-12-17 03:27:00 +00:00
type inviteUser = Author & { selected: boolean }
2022-11-27 05:49:48 +00:00
type Props = {
users: Author[]
}
const CreateModalContent = (props: Props) => {
2023-02-17 09:21:02 +00:00
const { t } = useLocalize()
2022-12-17 03:27:00 +00:00
const inviteUsers: inviteUser[] = props.users.map((user) => ({ ...user, selected: false }))
2023-02-20 17:01:23 +00:00
const [chatTitle, setChatTitle] = createSignal<string>('')
2022-12-17 03:27:00 +00:00
const [usersId, setUsersId] = createSignal<number[]>([])
const [collectionToInvite, setCollectionToInvite] = createSignal<inviteUser[]>(inviteUsers)
2024-02-04 17:47:26 +00:00
const { createChat, loadChats } = useInbox()
2022-11-27 05:49:48 +00:00
let textInput: HTMLInputElement
const reset = () => {
2023-02-20 17:01:23 +00:00
setChatTitle('')
2022-12-17 03:27:00 +00:00
setUsersId([])
2022-11-27 05:49:48 +00:00
hideModal()
}
createEffect(() => {
2022-12-17 03:27:00 +00:00
setUsersId(() => {
2023-02-20 17:01:23 +00:00
const s = collectionToInvite()
2022-12-17 03:27:00 +00:00
.filter((user) => {
2022-11-27 05:49:48 +00:00
return user.selected === true
})
2022-12-17 03:27:00 +00:00
.map((user) => {
2024-02-04 09:03:15 +00:00
return user.id
2022-11-27 05:49:48 +00:00
})
2023-02-20 17:01:23 +00:00
return [...s]
2022-11-27 05:49:48 +00:00
})
})
const handleSetTheme = () => {
2023-02-20 17:01:23 +00:00
setChatTitle(textInput.value.length > 0 && textInput.value)
2022-11-27 05:49:48 +00:00
}
const handleClick = (user) => {
2022-12-17 03:27:00 +00:00
setCollectionToInvite((userCollection) => {
return userCollection.map((clickedUser) =>
user.id === clickedUser.id ? { ...clickedUser, selected: !clickedUser.selected } : clickedUser,
2022-11-27 05:49:48 +00:00
)
})
}
const handleCreate = async () => {
try {
2024-02-04 17:47:26 +00:00
const initChat = await createChat(usersId(), chatTitle())
2023-12-20 16:54:20 +00:00
console.debug('[components.Inbox] create chat result:', initChat)
2022-12-17 03:27:00 +00:00
hideModal()
2024-02-04 17:47:26 +00:00
await loadChats()
2022-11-27 05:49:48 +00:00
} catch (error) {
console.error(error)
}
}
return (
<div class={styles.CreateModalContent}>
2023-02-17 09:21:02 +00:00
<h4>{t('Create Chat')}</h4>
2022-12-17 03:27:00 +00:00
{usersId().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"
2023-02-17 09:21:02 +00:00
placeholder={t('Chat Title')}
2022-11-27 05:49:48 +00:00
/>
)}
<div class="invite-recipients" style={{ height: '400px', overflow: 'auto' }}>
<For each={collectionToInvite()}>
2022-12-17 03:27:00 +00:00
{(author) => (
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')}
2022-11-27 05:49:48 +00:00
</button>
<button
type="button"
class="btn btn-lg fs-3 btn-outline-primary"
onClick={handleCreate}
2022-12-17 03:27:00 +00:00
disabled={usersId().length === 0}
2022-11-27 05:49:48 +00:00
>
2023-02-17 09:21:02 +00:00
{usersId().length > 1 ? t('Create Group') : t('Create Chat')}
2022-11-27 05:49:48 +00:00
</button>
</div>
</div>
)
}
export default CreateModalContent