2020-03-15 03:48:32 +00:00
|
|
|
|
// @flow
|
2020-06-20 20:59:15 +00:00
|
|
|
|
import { debounce } from "lodash";
|
2020-08-09 05:53:59 +00:00
|
|
|
|
import { observable } from "mobx";
|
|
|
|
|
import { inject, observer } from "mobx-react";
|
|
|
|
|
import * as React from "react";
|
2020-11-30 04:04:58 +00:00
|
|
|
|
import { withTranslation, type TFunction } from "react-i18next";
|
2020-08-09 05:53:59 +00:00
|
|
|
|
import AuthStore from "stores/AuthStore";
|
|
|
|
|
import GroupMembershipsStore from "stores/GroupMembershipsStore";
|
|
|
|
|
import UiStore from "stores/UiStore";
|
|
|
|
|
import UsersStore from "stores/UsersStore";
|
|
|
|
|
import Group from "models/Group";
|
2020-11-30 04:04:58 +00:00
|
|
|
|
import User from "models/User";
|
2020-08-09 05:53:59 +00:00
|
|
|
|
import Invite from "scenes/Invite";
|
|
|
|
|
import Empty from "components/Empty";
|
2020-07-10 05:33:07 +00:00
|
|
|
|
import Flex from "components/Flex";
|
2020-06-20 20:59:15 +00:00
|
|
|
|
import HelpText from "components/HelpText";
|
|
|
|
|
import Input from "components/Input";
|
|
|
|
|
import Modal from "components/Modal";
|
|
|
|
|
import PaginatedList from "components/PaginatedList";
|
|
|
|
|
import GroupMemberListItem from "./components/GroupMemberListItem";
|
2020-03-15 03:48:32 +00:00
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
ui: UiStore,
|
|
|
|
|
auth: AuthStore,
|
|
|
|
|
group: Group,
|
|
|
|
|
groupMemberships: GroupMembershipsStore,
|
|
|
|
|
users: UsersStore,
|
|
|
|
|
onSubmit: () => void,
|
2020-11-30 04:04:58 +00:00
|
|
|
|
t: TFunction,
|
2020-03-15 03:48:32 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@observer
|
|
|
|
|
class AddPeopleToGroup extends React.Component<Props> {
|
|
|
|
|
@observable inviteModalOpen: boolean = false;
|
2020-06-20 20:59:15 +00:00
|
|
|
|
@observable query: string = "";
|
2020-03-15 03:48:32 +00:00
|
|
|
|
|
|
|
|
|
handleInviteModalOpen = () => {
|
|
|
|
|
this.inviteModalOpen = true;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleInviteModalClose = () => {
|
|
|
|
|
this.inviteModalOpen = false;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
handleFilter = (ev: SyntheticInputEvent<HTMLInputElement>) => {
|
|
|
|
|
this.query = ev.target.value;
|
|
|
|
|
this.debouncedFetch();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
debouncedFetch = debounce(() => {
|
|
|
|
|
this.props.users.fetchPage({
|
|
|
|
|
query: this.query,
|
|
|
|
|
});
|
|
|
|
|
}, 250);
|
|
|
|
|
|
2020-11-30 04:04:58 +00:00
|
|
|
|
handleAddUser = async (user: User) => {
|
|
|
|
|
const { t } = this.props;
|
|
|
|
|
|
2020-03-15 03:48:32 +00:00
|
|
|
|
try {
|
|
|
|
|
await this.props.groupMemberships.create({
|
|
|
|
|
groupId: this.props.group.id,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
});
|
2020-11-30 04:04:58 +00:00
|
|
|
|
this.props.ui.showToast(
|
|
|
|
|
t(`{{userName}} was added to the group`, { userName: user.name })
|
|
|
|
|
);
|
2020-03-15 03:48:32 +00:00
|
|
|
|
} catch (err) {
|
2020-11-30 04:04:58 +00:00
|
|
|
|
this.props.ui.showToast(t("Could not add user"));
|
2020-03-15 03:48:32 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
2020-11-30 04:04:58 +00:00
|
|
|
|
const { users, group, auth, t } = this.props;
|
2020-03-15 03:48:32 +00:00
|
|
|
|
const { user, team } = auth;
|
|
|
|
|
if (!user || !team) return null;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Flex column>
|
|
|
|
|
<HelpText>
|
2020-11-30 04:04:58 +00:00
|
|
|
|
{t(
|
|
|
|
|
"Add team members below to give them access to the group. Need to add someone who’s not yet on the team yet?"
|
|
|
|
|
)}{" "}
|
2020-03-15 03:48:32 +00:00
|
|
|
|
<a role="button" onClick={this.handleInviteModalOpen}>
|
2020-11-30 04:04:58 +00:00
|
|
|
|
{t("Invite them to {{teamName}}", { teamName: team.name })}
|
2020-08-09 01:53:11 +00:00
|
|
|
|
</a>
|
|
|
|
|
.
|
2020-03-15 03:48:32 +00:00
|
|
|
|
</HelpText>
|
|
|
|
|
|
|
|
|
|
<Input
|
|
|
|
|
type="search"
|
2020-11-30 04:04:58 +00:00
|
|
|
|
placeholder={t("Search by name…")}
|
2020-03-15 03:48:32 +00:00
|
|
|
|
value={this.query}
|
|
|
|
|
onChange={this.handleFilter}
|
2020-11-30 04:04:58 +00:00
|
|
|
|
label={t("Search people")}
|
2020-03-15 03:48:32 +00:00
|
|
|
|
labelHidden
|
|
|
|
|
autoFocus
|
|
|
|
|
flex
|
|
|
|
|
/>
|
|
|
|
|
<PaginatedList
|
|
|
|
|
empty={
|
|
|
|
|
this.query ? (
|
2020-11-30 04:04:58 +00:00
|
|
|
|
<Empty>{t("No people matching your search")}</Empty>
|
2020-03-15 03:48:32 +00:00
|
|
|
|
) : (
|
2020-11-30 04:04:58 +00:00
|
|
|
|
<Empty>{t("No people left to add")}</Empty>
|
2020-03-15 03:48:32 +00:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
items={users.notInGroup(group.id, this.query)}
|
|
|
|
|
fetch={this.query ? undefined : users.fetchPage}
|
2020-08-09 01:53:11 +00:00
|
|
|
|
renderItem={(item) => (
|
2020-03-15 03:48:32 +00:00
|
|
|
|
<GroupMemberListItem
|
|
|
|
|
key={item.id}
|
|
|
|
|
user={item}
|
|
|
|
|
onAdd={() => this.handleAddUser(item)}
|
|
|
|
|
canEdit
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<Modal
|
2020-11-30 04:04:58 +00:00
|
|
|
|
title={t("Invite people")}
|
2020-03-15 03:48:32 +00:00
|
|
|
|
onRequestClose={this.handleInviteModalClose}
|
|
|
|
|
isOpen={this.inviteModalOpen}
|
|
|
|
|
>
|
|
|
|
|
<Invite onSubmit={this.handleInviteModalClose} />
|
|
|
|
|
</Modal>
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-30 04:04:58 +00:00
|
|
|
|
export default withTranslation()<AddPeopleToGroup>(
|
|
|
|
|
inject("auth", "users", "groupMemberships", "ui")(AddPeopleToGroup)
|
|
|
|
|
);
|