2022-11-27 05:49:48 +00:00
|
|
|
import { createSignal, For, createEffect } from 'solid-js'
|
|
|
|
import styles from './CreateModalContent.module.scss'
|
2023-02-17 09:21:02 +00:00
|
|
|
|
2022-11-27 05:49:48 +00:00
|
|
|
import InviteUser from './InviteUser'
|
|
|
|
import type { Author } from '../../graphql/types.gen'
|
|
|
|
import { hideModal } from '../../stores/ui'
|
|
|
|
import { useInbox } from '../../context/inbox'
|
2023-02-17 09:21:02 +00:00
|
|
|
import { useLocalize } from '../../context/localize'
|
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)
|
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) => {
|
|
|
|
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
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-12-17 03:27:00 +00:00
|
|
|
const { actions } = useInbox()
|
2022-11-27 05:49:48 +00:00
|
|
|
|
|
|
|
const handleCreate = async () => {
|
|
|
|
try {
|
2023-02-20 17:01:23 +00:00
|
|
|
const initChat = await actions.createChat(usersId(), chatTitle())
|
2022-11-27 05:49:48 +00:00
|
|
|
console.debug('[initChat]', initChat)
|
2022-12-17 03:27:00 +00:00
|
|
|
hideModal()
|
|
|
|
await actions.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}>
|
2023-07-28 09:47:19 +00:00
|
|
|
{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
|