2019-10-06 01:42:03 +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";
|
|
|
|
|
import AuthStore from "stores/AuthStore";
|
|
|
|
|
import MembershipsStore from "stores/MembershipsStore";
|
|
|
|
|
import UiStore from "stores/UiStore";
|
|
|
|
|
import UsersStore from "stores/UsersStore";
|
|
|
|
|
import Collection from "models/Collection";
|
|
|
|
|
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 MemberListItem from "./components/MemberListItem";
|
2019-10-06 01:42:03 +00:00
|
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
ui: UiStore,
|
|
|
|
|
auth: AuthStore,
|
|
|
|
|
collection: Collection,
|
|
|
|
|
memberships: MembershipsStore,
|
|
|
|
|
users: UsersStore,
|
|
|
|
|
onSubmit: () => void,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@observer
|
|
|
|
|
class AddPeopleToCollection extends React.Component<Props> {
|
|
|
|
|
@observable inviteModalOpen: boolean = false;
|
2020-06-20 20:59:15 +00:00
|
|
|
|
@observable query: string = "";
|
2019-10-06 01:42:03 +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-08-09 01:53:11 +00:00
|
|
|
|
handleAddUser = (user) => {
|
2019-10-06 01:42:03 +00:00
|
|
|
|
try {
|
|
|
|
|
this.props.memberships.create({
|
|
|
|
|
collectionId: this.props.collection.id,
|
|
|
|
|
userId: user.id,
|
2020-06-20 20:59:15 +00:00
|
|
|
|
permission: "read_write",
|
2019-10-06 01:42:03 +00:00
|
|
|
|
});
|
|
|
|
|
this.props.ui.showToast(`${user.name} was added to the collection`);
|
|
|
|
|
} catch (err) {
|
2020-06-20 20:59:15 +00:00
|
|
|
|
this.props.ui.showToast("Could not add user");
|
2019-10-06 01:42:03 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { users, collection, auth } = this.props;
|
|
|
|
|
const { user, team } = auth;
|
|
|
|
|
if (!user || !team) return null;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Flex column>
|
|
|
|
|
<HelpText>
|
2020-06-20 20:59:15 +00:00
|
|
|
|
Need to add someone who’s not yet on the team yet?{" "}
|
2019-10-06 01:42:03 +00:00
|
|
|
|
<a role="button" onClick={this.handleInviteModalOpen}>
|
|
|
|
|
Invite people to {team.name}
|
2020-08-09 01:53:11 +00:00
|
|
|
|
</a>
|
|
|
|
|
.
|
2019-10-06 01:42:03 +00:00
|
|
|
|
</HelpText>
|
|
|
|
|
|
|
|
|
|
<Input
|
|
|
|
|
type="search"
|
|
|
|
|
placeholder="Search by name…"
|
|
|
|
|
value={this.query}
|
|
|
|
|
onChange={this.handleFilter}
|
|
|
|
|
label="Search people"
|
2020-05-25 05:17:11 +00:00
|
|
|
|
autoFocus
|
2019-10-06 01:42:03 +00:00
|
|
|
|
labelHidden
|
|
|
|
|
flex
|
|
|
|
|
/>
|
|
|
|
|
<PaginatedList
|
|
|
|
|
empty={
|
|
|
|
|
this.query ? (
|
|
|
|
|
<Empty>No people matching your search</Empty>
|
|
|
|
|
) : (
|
|
|
|
|
<Empty>No people left to add</Empty>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
items={users.notInCollection(collection.id, this.query)}
|
|
|
|
|
fetch={this.query ? undefined : users.fetchPage}
|
2020-08-09 01:53:11 +00:00
|
|
|
|
renderItem={(item) => (
|
2019-10-06 01:42:03 +00:00
|
|
|
|
<MemberListItem
|
|
|
|
|
key={item.id}
|
|
|
|
|
user={item}
|
|
|
|
|
onAdd={() => this.handleAddUser(item)}
|
|
|
|
|
canEdit
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
<Modal
|
|
|
|
|
title="Invite people"
|
|
|
|
|
onRequestClose={this.handleInviteModalClose}
|
|
|
|
|
isOpen={this.inviteModalOpen}
|
|
|
|
|
>
|
|
|
|
|
<Invite onSubmit={this.handleInviteModalClose} />
|
|
|
|
|
</Modal>
|
|
|
|
|
</Flex>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 01:53:11 +00:00
|
|
|
|
export default inject(
|
|
|
|
|
"auth",
|
|
|
|
|
"users",
|
|
|
|
|
"memberships",
|
|
|
|
|
"ui"
|
|
|
|
|
)(AddPeopleToCollection);
|