forked from nilbuild/developer-roadmap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRespondInviteForm.tsx
More file actions
168 lines (151 loc) · 4.9 KB
/
Copy pathRespondInviteForm.tsx
File metadata and controls
168 lines (151 loc) · 4.9 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { useEffect, useState } from 'react';
import { httpGet, httpPatch } from '../lib/http';
import { pageProgressMessage } from '../stores/page';
import type { TeamDocument } from './CreateTeam/CreateTeamForm';
import type { AllowedRoles } from './CreateTeam/RoleDropdown';
import type { AllowedMemberStatus } from './TeamDropdown/TeamDropdown';
import { isLoggedIn } from '../lib/jwt';
import { showLoginPopup } from '../lib/popup';
import { getUrlParams } from '../lib/browser';
import { ErrorIcon2 } from './ReactIcons/ErrorIcon2';
import { BuildingIcon } from './ReactIcons/BuildingIcon';
type InvitationResponse = {
team: TeamDocument;
invite: {
_id?: string;
userId?: string;
invitedEmail?: string;
teamId: string;
role: AllowedRoles;
status: AllowedMemberStatus;
createdAt: Date;
updatedAt: Date;
};
};
export function RespondInviteForm() {
const { i: inviteId } = getUrlParams();
const [isLoadingInvite, setIsLoadingInvite] = useState(true);
const [error, setError] = useState('');
const [invite, setInvite] = useState<InvitationResponse>();
const isAuthenticated = isLoggedIn();
async function loadInvitation(inviteId: string) {
const { response, error } = await httpGet<InvitationResponse>(
`${import.meta.env.PUBLIC_API_URL}/v1-get-invitation/${inviteId}`
);
if (error || !response) {
setError(error?.message || 'Something went wrong');
return;
}
setInvite(response);
}
useEffect(() => {
if (inviteId) {
loadInvitation(inviteId).finally(() => {
pageProgressMessage.set('');
setIsLoadingInvite(false);
});
} else {
setIsLoadingInvite(false);
setError('Missing invite ID in URL');
pageProgressMessage.set('');
}
}, [inviteId]);
async function respondInvitation(status: 'accept' | 'reject') {
pageProgressMessage.set('Please wait...');
setError('');
const { response, error } = await httpPatch<{ teamId: string }>(
`${import.meta.env.PUBLIC_API_URL}/v1-respond-invite/${inviteId}`,
{
status,
}
);
if (error || !response) {
setError(error?.message || 'Something went wrong');
return;
}
if (status === 'reject') {
window.location.href = '/';
return;
}
window.location.href = `/team/activity?t=${response.teamId}`;
}
if (isLoadingInvite) {
return null;
}
if (!invite) {
return (
<div className="container text-center">
<ErrorIcon2 className="mx-auto mb-4 mt-24 w-20 opacity-20" />
<h2 className={'mb-1 text-2xl font-bold'}>Error</h2>
<p className="mb-4 text-base leading-6 text-gray-600">
{error || 'There was a problem, please try again.'}
</p>
<div>
<a
href="/"
className="flex-grow cursor-pointer rounded-lg bg-gray-200 px-3 py-2 text-center"
>
Back to home
</a>
</div>
</div>
);
}
return (
<div className="container text-center">
<BuildingIcon className="mx-auto mb-4 mt-24 w-20 h-20 opacity-20" />
<h2 className={'mb-1 text-2xl font-bold'}>Join Team</h2>
<p className="mb-3 text-base leading-6 text-gray-600">
You have been invited to join the team{' '}
<strong id="team-name">{invite?.team?.name}</strong>.
</p>
{!isAuthenticated && (
<div className="mx-auto w-full duration-500 sm:max-w-md">
<div className="flex w-full items-center gap-2">
<button
onClick={() => showLoginPopup()}
data-popup="login-popup"
type="button"
className="flex-grow cursor-pointer rounded-lg bg-gray-200 px-3 py-2 text-center"
>
Login to respond
</button>
</div>
</div>
)}
{isAuthenticated && (
<div className={`mx-auto w-full max-w-md`}>
<div className="flex w-full items-center gap-2">
<button
type="button"
onClick={() =>
respondInvitation('accept').finally(() => {
pageProgressMessage.set('');
})
}
className="flex-grow cursor-pointer rounded-lg hover:bg-gray-300 bg-gray-200 px-3 py-2 text-center"
>
Accept
</button>
<button
type="button"
onClick={() =>
respondInvitation('reject').finally(() => {
pageProgressMessage.set('');
})
}
className="flex-grow cursor-pointer rounded-lg bg-red-500 hover:bg-red-600 px-3 py-2 text-white disabled:opacity-40"
>
Reject
</button>
</div>
{error && (
<p className="mt-2 rounded-lg bg-red-100 p-2 text-red-700">
{error}
</p>
)}
</div>
)}
</div>
);
}