Skip to content

Commit 3b34c5e

Browse files
committed
copilot
1 parent dedf6be commit 3b34c5e

2 files changed

Lines changed: 32 additions & 30 deletions

File tree

src/components/ListingPhotosManager/ListingPhotosManager.tsx

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,11 @@ function ListingPhotosManager({
194194
uploadError?.statusCode === "413" ||
195195
uploadError?.error?.statusCode === "413"
196196
) {
197-
alert(overSizedFileAlertPlural);
197+
alert(
198+
files.length === 1
199+
? overSizedFileAlertSingular
200+
: overSizedFileAlertPlural
201+
);
198202
} else if (uploadError?.message?.includes("max_photos")) {
199203
alert(`You can only upload up to ${MAX_PHOTOS} photos`);
200204
} else {
@@ -208,6 +212,7 @@ function ListingPhotosManager({
208212
const handlePhotoDelete = async (photoToDelete: string) => {
209213
if (isMutatingPhotos) return;
210214

215+
const deletedPhotoIndex = photosRef.current.indexOf(photoToDelete);
211216
setDeletingPhoto(photoToDelete);
212217
try {
213218
// Immediately remove from react-beautiful-dnd's context
@@ -221,11 +226,22 @@ function ListingPhotosManager({
221226
console.error("Error deleting photo:", error);
222227

223228
// Restore only the failed deletion, preserving other state changes.
224-
updatePhotos((currentPhotos) =>
225-
currentPhotos.includes(photoToDelete)
226-
? currentPhotos
227-
: [...currentPhotos, photoToDelete]
228-
);
229+
updatePhotos((currentPhotos) => {
230+
if (currentPhotos.includes(photoToDelete)) {
231+
return currentPhotos;
232+
}
233+
234+
const restoreIndex =
235+
deletedPhotoIndex === -1
236+
? currentPhotos.length
237+
: Math.min(deletedPhotoIndex, currentPhotos.length);
238+
239+
return [
240+
...currentPhotos.slice(0, restoreIndex),
241+
photoToDelete,
242+
...currentPhotos.slice(restoreIndex),
243+
];
244+
});
229245
alert("Failed to delete photo. Please try again.");
230246
} finally {
231247
setDeletingPhoto(null);

src/components/ProfileActions/ProfileActions.tsx

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
import { signOutAction, deleteAccountAction } from "@/app/actions";
44
import { siteConfig } from "@/config/site";
5-
import Button from "@/components/Button";
65
import ButtonToDialog from "@/components/ButtonToDialog";
76
import EncodedEmailLink from "@/components/EncodedEmailLink";
8-
import { useState } from "react";
7+
import SubmitButton from "@/components/SubmitButton";
98

109
import { styled } from "@pigment-css/react";
1110

@@ -36,40 +35,27 @@ const ListItemText = styled("div")(({ theme }) => ({
3635
},
3736
}));
3837

38+
const ActionForm = styled("form")({
39+
flexShrink: 0,
40+
});
41+
3942
type ProfileActionsProps = {
4043
listings?: unknown[];
4144
};
4245

4346
export default function ProfileActions({ listings = [] }: ProfileActionsProps) {
44-
const [isSigningOut, setIsSigningOut] = useState(false);
45-
46-
const handleSignOut = async () => {
47-
if (isSigningOut) return;
48-
49-
setIsSigningOut(true);
50-
try {
51-
await signOutAction();
52-
} catch (error) {
53-
console.error("Error signing out:", error);
54-
setIsSigningOut(false);
55-
}
56-
};
57-
5847
return (
5948
<List>
6049
<ListItem>
6150
<ListItemText>
6251
<h4>Sign out</h4>
6352
<p>Goodbye for now!</p>
6453
</ListItemText>
65-
<Button
66-
variant="secondary"
67-
onClick={handleSignOut}
68-
loading={isSigningOut}
69-
loadingText="Signing out..."
70-
>
71-
Sign out
72-
</Button>
54+
<ActionForm action={signOutAction}>
55+
<SubmitButton variant="secondary" loadingText="Signing out...">
56+
Sign out
57+
</SubmitButton>
58+
</ActionForm>
7359
</ListItem>
7460

7561
<ListItem>

0 commit comments

Comments
 (0)