Skip to content

Commit c9d4d93

Browse files
committed
better handling of org cache
1 parent bbb711e commit c9d4d93

File tree

6 files changed

+83
-177
lines changed

6 files changed

+83
-177
lines changed

apps/dashboard/app/(main)/organizations/components/general-settings.tsx

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import { BuildingsIcon, FloppyDiskIcon } from "@phosphor-icons/react";
4-
import { useState } from "react";
4+
import { useEffect, useState } from "react";
55
import { toast } from "sonner";
66
import { RightSidebar } from "@/components/right-sidebar";
77
import { Button } from "@/components/ui/button";
@@ -19,7 +19,12 @@ export function GeneralSettings({
1919
const [slug, setSlug] = useState(organization.slug);
2020
const [isSaving, setIsSaving] = useState(false);
2121

22-
const { updateOrganizationAsync } = useOrganizations();
22+
const { updateOrganization } = useOrganizations();
23+
24+
useEffect(() => {
25+
setName(organization.name);
26+
setSlug(organization.slug);
27+
}, [organization.name, organization.slug]);
2328

2429
const cleanSlug = (value: string) =>
2530
value
@@ -34,7 +39,7 @@ export function GeneralSettings({
3439

3540
const hasChanges = name !== organization.name || slug !== organization.slug;
3641

37-
const handleSave = async () => {
42+
const handleSave = () => {
3843
if (!name.trim()) {
3944
toast.error("Name is required");
4045
return;
@@ -45,17 +50,20 @@ export function GeneralSettings({
4550
}
4651

4752
setIsSaving(true);
48-
try {
49-
await updateOrganizationAsync({
53+
updateOrganization(
54+
{
5055
organizationId: organization.id,
5156
data: { name: name.trim(), slug: slug.trim() },
52-
});
53-
toast.success("Settings updated");
54-
} catch {
55-
toast.error("Failed to update settings");
56-
} finally {
57-
setIsSaving(false);
58-
}
57+
},
58+
{
59+
onSuccess: () => {
60+
setIsSaving(false);
61+
},
62+
onError: () => {
63+
setIsSaving(false);
64+
},
65+
}
66+
);
5967
};
6068

6169
return (

apps/dashboard/app/(main)/organizations/components/organization-logo-uploader.tsx

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"use client";
22

33
import Image from "next/image";
4-
import { useRef, useState } from "react";
4+
import { useEffect, useRef, useState } from "react";
55
import ReactCrop, {
66
type Crop,
77
centerCrop,
@@ -33,12 +33,16 @@ export function OrganizationLogoUploader({
3333
organization,
3434
}: OrganizationLogoUploaderProps) {
3535
const {
36-
uploadOrganizationLogo,
37-
isUploadingOrganizationLogo,
36+
updateOrganization,
37+
isUpdatingOrganization,
3838
deleteOrganizationLogo,
3939
isDeletingOrganizationLogo,
4040
} = useOrganizations();
4141
const [preview, setPreview] = useState(organization.logo);
42+
43+
useEffect(() => {
44+
setPreview(organization.logo);
45+
}, [organization.logo]);
4246
const [imageSrc, setImageSrc] = useState<string | null>(null);
4347
const [crop, setCrop] = useState<Crop>();
4448
const [completedCrop, setCompletedCrop] = useState<PixelCrop>();
@@ -117,21 +121,19 @@ export function OrganizationLogoUploader({
117121
const reader = new FileReader();
118122
reader.onloadend = () => {
119123
const fileData = reader.result as string;
120-
uploadOrganizationLogo(
124+
updateOrganization(
121125
{
122126
organizationId: organization.id,
123-
fileData,
124-
fileName: croppedFile.name,
125-
fileType: croppedFile.type,
127+
data: { logo: fileData },
126128
},
127129
{
128-
onSuccess: (data) => {
129-
setPreview(data.logoUrl);
130+
onSuccess: () => {
131+
setPreview(fileData);
130132
handleModalOpenChange(false);
131133
setTimeout(() => resetCropState(), 100);
132134
},
133135
onError: (error) => {
134-
toast.error(error.message || "Failed to upload logo.");
136+
toast.error(error.message || "Failed to update logo.");
135137
},
136138
}
137139
);
@@ -251,17 +253,17 @@ export function OrganizationLogoUploader({
251253
</Button>
252254
<Button
253255
disabled={
254-
isUploadingOrganizationLogo || !imageSrc || !completedCrop
256+
isUpdatingOrganization || !imageSrc || !completedCrop
255257
}
256258
onClick={handleUpload}
257259
>
258-
{isUploadingOrganizationLogo ? (
260+
{isUpdatingOrganization ? (
259261
<>
260262
<div className="mr-2 size-3 animate-spin rounded-full border border-primary-foreground/30 border-t-primary-foreground" />
261-
Uploading...
263+
Updating...
262264
</>
263265
) : (
264-
"Save and Upload"
266+
"Save Logo"
265267
)}
266268
</Button>
267269
</DialogFooter>

apps/dashboard/app/(main)/organizations/settings/danger/danger-zone-settings.tsx

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ export function DangerZoneSettings({
3737
const [isOwner, setIsOwner] = useState<boolean | null>(null);
3838
const [confirmText, setConfirmText] = useState("");
3939

40-
const { deleteOrganizationAsync, leaveOrganizationAsync } =
41-
useOrganizations();
40+
const { deleteOrganization, leaveOrganization } = useOrganizations();
4241

4342
useEffect(() => {
4443
const checkOwnership = async () => {
@@ -63,36 +62,38 @@ export function DangerZoneSettings({
6362
checkOwnership();
6463
}, [organization.id, session?.user?.id]);
6564

66-
const handleDelete = async () => {
65+
const handleDelete = () => {
6766
if (confirmText !== organization.name) {
6867
toast.error("Organization name does not match");
6968
return;
7069
}
7170

7271
setIsDeleting(true);
73-
try {
74-
await deleteOrganizationAsync(organization.id);
75-
router.push("/organizations");
76-
} catch {
77-
toast.error("Failed to delete organization");
78-
} finally {
79-
setIsDeleting(false);
80-
setShowDeleteDialog(false);
81-
setConfirmText("");
82-
}
72+
deleteOrganization(organization.id, {
73+
onSuccess: () => {
74+
router.push("/organizations");
75+
setIsDeleting(false);
76+
setShowDeleteDialog(false);
77+
setConfirmText("");
78+
},
79+
onError: () => {
80+
setIsDeleting(false);
81+
},
82+
});
8383
};
8484

85-
const handleLeave = async () => {
85+
const handleLeave = () => {
8686
setIsLeaving(true);
87-
try {
88-
await leaveOrganizationAsync(organization.id);
89-
router.push("/organizations");
90-
} catch {
91-
toast.error("Failed to leave organization");
92-
} finally {
93-
setIsLeaving(false);
94-
setShowLeaveDialog(false);
95-
}
87+
leaveOrganization(organization.id, {
88+
onSuccess: () => {
89+
router.push("/organizations");
90+
setIsLeaving(false);
91+
setShowLeaveDialog(false);
92+
},
93+
onError: () => {
94+
setIsLeaving(false);
95+
},
96+
});
9697
};
9798

9899
return (

apps/dashboard/components/organizations/create-organization-dialog.tsx

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ export function CreateOrganizationDialog({
6464
onClose,
6565
}: CreateOrganizationDialogProps) {
6666
const {
67-
createOrganizationAsync,
67+
createOrganization,
6868
isCreatingOrganization,
69-
uploadOrganizationLogoAsync,
69+
setActiveOrganization,
7070
} = useOrganizations();
7171
// const router = useRouter();
7272

@@ -232,44 +232,27 @@ export function CreateOrganizationDialog({
232232
.toUpperCase()
233233
.slice(0, 2);
234234

235-
const handleSubmit = async () => {
235+
const handleSubmit = () => {
236236
if (!isFormValid) {
237237
return;
238238
}
239-
try {
240-
const organization = await createOrganizationAsync({
239+
240+
createOrganization(
241+
{
241242
name: formData.name,
242243
slug: formData.slug,
244+
logo: preview || undefined,
243245
metadata: formData.metadata,
244-
});
245-
246-
if (logoFile && organization?.id) {
247-
try {
248-
const reader = new FileReader();
249-
const fileData = await new Promise<string>((resolve) => {
250-
reader.onloadend = () => resolve(reader.result as string);
251-
reader.readAsDataURL(logoFile);
252-
});
253-
254-
await uploadOrganizationLogoAsync({
255-
organizationId: organization.id,
256-
fileData,
257-
fileName: logoFile.name,
258-
fileType: logoFile.type,
259-
});
260-
} catch (logoError) {
261-
toast.warning(
262-
"Organization created, but logo upload failed. You can upload it later from settings."
263-
);
264-
console.error("Logo upload failed:", logoError);
265-
}
246+
},
247+
{
248+
onSuccess: (organization) => {
249+
if (organization?.id) {
250+
setActiveOrganization(organization.id);
251+
}
252+
handleClose();
253+
},
266254
}
267-
268-
handleClose();
269-
// router.push('/organizations');
270-
} catch {
271-
// handled by mutation toast
272-
}
255+
);
273256
};
274257

275258
return (

0 commit comments

Comments
 (0)