-
Notifications
You must be signed in to change notification settings - Fork 802
Expand file tree
/
Copy pathTransferHostModal.tsx
More file actions
122 lines (110 loc) · 3.37 KB
/
TransferHostModal.tsx
File metadata and controls
122 lines (110 loc) · 3.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import React, { useCallback, useState } from "react";
import { Link } from "react-router";
import PATHS from "router/paths";
import Modal from "components/Modal";
import Button from "components/buttons/Button";
// ignore TS error for now until these are rewritten in ts.
// @ts-ignore
import Dropdown from "components/forms/fields/Dropdown";
import { ITeam, ITeamSummary } from "interfaces/team";
interface ITransferHostModal {
isGlobalAdmin: boolean;
teams: ITeam[];
onSubmit: (team: ITeam) => void;
onCancel: () => void;
isUpdating: boolean;
/** Manage host page only */
multipleHosts?: boolean;
/** Selected team's id on manage host page, or host detail's team id */
hostsTeamId?: number | null;
}
interface INoTeamOption {
id: string;
}
const baseClass = "transfer-host-modal";
const NO_TEAM_OPTION = {
value: "no-team",
label: "No team",
};
const TransferHostModal = ({
onCancel,
onSubmit,
teams,
isGlobalAdmin,
isUpdating,
multipleHosts,
hostsTeamId,
}: ITransferHostModal): JSX.Element => {
const [selectedTeam, setSelectedTeam] = useState<ITeam | INoTeamOption>();
const onChangeSelectTeam = useCallback(
(teamId: number | string) => {
if (teamId === "no-team") {
setSelectedTeam({ id: NO_TEAM_OPTION.value });
} else {
const teamWithId = teams.find((team) => team.id === teamId);
setSelectedTeam(teamWithId as ITeam);
}
},
[teams, setSelectedTeam]
);
const onSubmitTransferHost = useCallback(() => {
onSubmit(selectedTeam as ITeam);
}, [onSubmit, selectedTeam]);
const createTeamDropdownOptions = () => {
const teamOptions = teams
.filter((team) => team.id !== hostsTeamId) // Remove current team from options
.map((team) => {
return {
value: team.id,
label: team.name,
};
});
// Hosts on no team cannot transfer to no team again
const canTransferToNoTeam = hostsTeamId !== 0 && hostsTeamId !== null;
return canTransferToNoTeam ? [NO_TEAM_OPTION, ...teamOptions] : teamOptions;
};
return (
<Modal onExit={onCancel} title="Transfer" className={baseClass}>
<>
<form className={`${baseClass}__form`}>
<Dropdown
wrapperClassName={`${baseClass}__team-dropdown-wrapper`}
label={`Transfer ${multipleHosts ? "selected hosts" : "host"} to:`}
value={selectedTeam && selectedTeam.id}
options={createTeamDropdownOptions()}
onChange={onChangeSelectTeam}
placeholder="Select a team"
searchable
autoFocus
/>
{isGlobalAdmin ? (
<p>
Team not here?{" "}
<Link
to={PATHS.ADMIN_TEAMS}
className={`${baseClass}__team-link`}
>
Create a team
</Link>
</p>
) : null}
<div className="modal-cta-wrap">
<Button
disabled={selectedTeam === undefined}
type="button"
onClick={onSubmitTransferHost}
className="transfer-loading"
isLoading={isUpdating}
>
Transfer
</Button>
<Button onClick={onCancel} variant="inverse">
Cancel
</Button>
</div>
</form>
</>
</Modal>
);
};
export default TransferHostModal;