-
Notifications
You must be signed in to change notification settings - Fork 655
/
Copy pathUserAvatar.tsx
155 lines (142 loc) · 4.67 KB
/
UserAvatar.tsx
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
import careConfig from "@careConfig";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { TooltipComponent } from "@/components/ui/tooltip";
import { Avatar } from "@/components/Common/Avatar";
import AvatarEditModal from "@/components/Common/AvatarEditModal";
import Loading from "@/components/Common/Loading";
import useAuthUser from "@/hooks/useAuthUser";
import { showAvatarEdit } from "@/Utils/permissions";
import routes from "@/Utils/request/api";
import mutate from "@/Utils/request/mutate";
import query from "@/Utils/request/query";
import uploadFile from "@/Utils/request/uploadFile";
import { getAuthorizationHeader } from "@/Utils/request/utils";
import { formatDisplayName, sleep } from "@/Utils/utils";
export default function UserAvatar({ username }: { username: string }) {
const { t } = useTranslation();
const [editAvatar, setEditAvatar] = useState(false);
const authUser = useAuthUser();
const queryClient = useQueryClient();
const { mutateAsync: mutateAvatarDelete } = useMutation({
mutationFn: mutate(routes.deleteProfilePicture, {
pathParams: { username },
}),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["getUserDetails", username] });
if (authUser.username === username) {
queryClient.invalidateQueries({ queryKey: ["currentUser"] });
}
toast.success(t("profile_picture_deleted"));
setEditAvatar(false);
},
});
const { data: userData, isLoading } = useQuery({
queryKey: ["getUserDetails", username],
queryFn: query(routes.getUserDetails, {
pathParams: { username },
}),
});
if (isLoading || !userData) {
return <Loading />;
}
const handleAvatarUpload = async (
file: File,
onSuccess: () => void,
onError: () => void,
) => {
const formData = new FormData();
formData.append("profile_picture", file);
const url = `${careConfig.apiUrl}/api/v1/users/${userData.username}/profile_picture/`;
await uploadFile(
url,
formData,
"POST",
{ Authorization: getAuthorizationHeader() },
async (xhr: XMLHttpRequest) => {
if (xhr.status === 200) {
setEditAvatar(false);
await sleep(1000);
queryClient.invalidateQueries({
queryKey: ["getUserDetails", username],
});
if (authUser.username === username) {
queryClient.invalidateQueries({ queryKey: ["currentUser"] });
}
toast.success(t("avatar_updated_success"));
}
},
null,
() => {
onError();
},
);
};
const handleAvatarDelete = async (
onSuccess: () => void,
onError: () => void,
) => {
try {
await mutateAvatarDelete();
onSuccess();
} catch {
onError();
}
};
return (
<>
<AvatarEditModal
title={t("edit_avatar")}
open={editAvatar}
imageUrl={userData?.profile_picture_url}
handleUpload={handleAvatarUpload}
handleDelete={handleAvatarDelete}
onOpenChange={(open) => setEditAvatar(open)}
/>
<div>
<div className="my-4 overflow-visible rounded-lg bg-white px-4 py-5 shadow sm:rounded-lg sm:px-6 flex justify-between">
<div className="flex items-center">
<Avatar
name={formatDisplayName(userData)}
imageUrl={userData?.profile_picture_url}
className="h-20 w-20"
/>
<div className="my-4 ml-4 flex flex-col gap-2">
{!showAvatarEdit(authUser, userData) ? (
<TooltipComponent
content={t("edit_avatar_permission_error")}
className="w-full"
>
<Button
variant="white"
onClick={() => setEditAvatar(!editAvatar)}
type="button"
id="change-avatar"
disabled
>
{t("change_avatar")}
</Button>
</TooltipComponent>
) : (
<Button
variant="white"
onClick={() => setEditAvatar(!editAvatar)}
type="button"
id="change-avatar"
>
{t("change_avatar")}
</Button>
)}
<p className="text-xs leading-5 text-gray-500">
{t("change_avatar_note")}
</p>
</div>
</div>
</div>
</div>
</>
);
}