Skip to content

Commit 28cd340

Browse files
authored
Add: User reset avatar to default (#695)
1 parent ed704dc commit 28cd340

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed

src/databaseAdapters/user.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class MongoUserAdapter implements DatabaseAdapter {
2222

2323
user.avatarImgRes = resLink;
2424
user.avatarImgSpy = spyLink;
25+
2526
await user.save();
2627
}
2728
}

src/routes/profile/avatarRoutes.tsx

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ import { checkProfileOwnership } from '../middleware';
1010
import userAdapter from '../../databaseAdapters/user';
1111
import { S3AvatarSet, S3Agent } from '../../clients/s3/S3Agent';
1212
import S3Controller from '../../clients/s3/S3Controller';
13-
import { PatreonAgent } from '../../clients/patreon/patreonAgent';
14-
import { PatreonController } from '../../clients/patreon/patreonController';
1513

1614
import { getAndUpdatePatreonRewardTierForUser } from '../../rewards/getRewards';
17-
import { isMod } from '../../modsadmins/mods';
1815

1916
export type AllAvatarsRouteReturnType = {
2017
currentResLink: string;
@@ -24,7 +21,6 @@ export type AllAvatarsRouteReturnType = {
2421

2522
const router = express.Router();
2623
const s3Agent = new S3Agent(new S3Controller());
27-
const patreonAgent = new PatreonAgent(new PatreonController());
2824

2925
// Show the user's avatar homepage
3026
router.get(
@@ -70,6 +66,21 @@ router.post(
7066
},
7167
);
7268

69+
// Reset user's avatar to default
70+
router.post(
71+
'/:profileUsername/avatar/resetavatar',
72+
checkProfileOwnership,
73+
async (req: EnrichedRequest, res: Response) => {
74+
if (!req.user.avatarImgRes && !req.user.avatarImgSpy) {
75+
return res.status(400).send('You are already using the default avatar.');
76+
}
77+
78+
await userAdapter.updateAvatar(req.user.username, null, null);
79+
80+
return res.status(200).send('Avatar reset successful.');
81+
},
82+
);
83+
7384
// Get a user's avatar library links
7485
router.get(
7586
'/:profileUsername/avatar/getalluseravatars',

src/views/changelog.ejs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<li class="list-group-item">
1919
<strong>4-06-2024: </strong>
2020
<ul>
21+
<li>Added: Reset avatar button in avatar homepage.</li>
2122
<li>Fixed: Dark mode for Avatar Library view.</li>
2223
</ul>
2324
</li>

src/views/components/avatar/avatarHome/avatarHome.tsx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const getLinks = {
1111
`/profile/${username}/avatar/getalluseravatars`,
1212
changeavatar: (username: string) =>
1313
`/profile/${username}/avatar/changeavatar`,
14+
resetavatar: (username: string) => `/profile/${username}/avatar/resetavatar`,
15+
1416
customavatar: (username: string) => `/profile/${username}/customavatar`,
1517
edit: (username: string) => `/profile/${username}/edit`,
1618
};
@@ -97,6 +99,29 @@ export function AvatarHome() {
9799
setSelectedAvatarSet(selectedAvatarSet === avatarSet ? null : avatarSet);
98100
};
99101

102+
const handleResetAvatar = async () => {
103+
await Swal.fire({
104+
title: 'Sending request',
105+
didOpen: async () => {
106+
Swal.showLoading();
107+
const response = await fetch(getLinks.resetavatar(username), {
108+
method: 'POST',
109+
});
110+
111+
if (response.status === 200) {
112+
setCurrentResImgLink(BaseAvatarLinks.baseRes);
113+
setCurrentSpyImgLink(BaseAvatarLinks.baseSpy);
114+
115+
Swal.close();
116+
await Swal.fire({ title: await response.text(), icon: 'success' });
117+
} else {
118+
Swal.close();
119+
await Swal.fire({ title: await response.text(), icon: 'error' });
120+
}
121+
},
122+
});
123+
};
124+
100125
if (isLoading) {
101126
return (
102127
<div className="loading-container">
@@ -131,6 +156,19 @@ export function AvatarHome() {
131156
<a className="btn btn-info" href={getLinks.customavatar(username)}>
132157
Submit a custom avatar
133158
</a>
159+
<button
160+
className="btn btn-warning"
161+
style={{
162+
marginLeft: '5px',
163+
}}
164+
onClick={handleResetAvatar}
165+
disabled={
166+
currentResImgLink === BaseAvatarLinks.baseRes &&
167+
currentSpyImgLink === BaseAvatarLinks.baseSpy
168+
}
169+
>
170+
Reset avatar
171+
</button>
134172
<hr
135173
style={{
136174
borderColor: 'lightgrey',

0 commit comments

Comments
 (0)