diff --git a/apps/console/src/pages/organizations/SettingsPage.tsx b/apps/console/src/pages/organizations/SettingsPage.tsx index 001d5ab68..dcae32ce0 100644 --- a/apps/console/src/pages/organizations/SettingsPage.tsx +++ b/apps/console/src/pages/organizations/SettingsPage.tsx @@ -262,6 +262,10 @@ export default function SettingsPage({ queryRef }: Props) { const memberships = membershipsPagination.data.memberships?.edges.map((edge) => edge.node) || []; const invitations = invitationsPagination.data.invitations?.edges.map((edge) => edge.node) || []; const [activeTab, setActiveTab] = useState<"memberships" | "invitations">("memberships"); + const [logoFile, setLogoFile] = useState(null); + const [horizontalLogoFile, setHorizontalLogoFile] = useState(null); + const [logoPreview, setLogoPreview] = useState(null); + const [horizontalLogoPreview, setHorizontalLogoPreview] = useState(null); const { formState, handleSubmit, register, reset } = useFormWithSchema( organizationSchema, @@ -284,20 +288,37 @@ export default function SettingsPage({ queryRef }: Props) { email: organization.email || "", headquarterAddress: organization.headquarterAddress || "", }); + setLogoFile(null); + setHorizontalLogoFile(null); + setLogoPreview(null); + setHorizontalLogoPreview(null); }, [organization, reset]); const onSubmit = handleSubmit((data: OrganizationFormData) => { + const uploadables: Record = {}; + + if (logoFile) { + uploadables["input.logo"] = logoFile; + } + + if (horizontalLogoFile) { + uploadables["input.horizontalLogoFile"] = horizontalLogoFile; + } + updateOrganization({ variables: { input: { organizationId: organization.id, name: data.name, - description: data.description || null, - websiteUrl: data.websiteUrl || null, - email: data.email || null, - headquarterAddress: data.headquarterAddress || null, + description: data.description || undefined, + websiteUrl: data.websiteUrl || undefined, + email: data.email || undefined, + headquarterAddress: data.headquarterAddress || undefined, + logo: logoFile ? null : undefined, + horizontalLogoFile: horizontalLogoFile ? null : undefined, }, }, + uploadables: Object.keys(uploadables).length > 0 ? uploadables : undefined, onError() { toast({ title: __("Error"), @@ -317,68 +338,30 @@ export default function SettingsPage({ queryRef }: Props) { }); }); - const updateOrganizationLogo: ChangeEventHandler = (e) => { + const handleLogoChange: ChangeEventHandler = (e) => { const file = e.target.files?.[0]; if (!file) { return; } - updateOrganization({ - variables: { - input: { - organizationId: organization.id, - logo: null, - }, - }, - uploadables: { - "input.logo": file, - }, - onError() { - toast({ - title: __("Error"), - description: __("Failed to update logo"), - variant: "error", - }); - }, - onCompleted() { - toast({ - title: __("Success"), - description: __("Your organization logo has been updated successfully."), - variant: "success", - }); - }, - }); + setLogoFile(file); + const reader = new FileReader(); + reader.onloadend = () => { + setLogoPreview(reader.result as string); + }; + reader.readAsDataURL(file); }; - const updateHorizontalLogo: ChangeEventHandler = (e) => { + const handleHorizontalLogoChange: ChangeEventHandler = (e) => { const file = e.target.files?.[0]; if (!file) { return; } - updateOrganization({ - variables: { - input: { - organizationId: organization.id, - horizontalLogoFile: null, - }, - }, - uploadables: { - "input.horizontalLogoFile": file, - }, - onError() { - toast({ - title: __("Error"), - description: __("Failed to update horizontal logo."), - variant: "error", - }); - }, - onCompleted() { - toast({ - title: __("Success"), - description: __("Your organization horizontal logo has been updated successfully."), - variant: "success", - }); - }, - }); + setHorizontalLogoFile(file); + const reader = new FileReader(); + reader.onloadend = () => { + setHorizontalLogoPreview(reader.result as string); + }; + reader.readAsDataURL(file); }; const deleteDialogRef = useDialogRef(); @@ -428,13 +411,13 @@ export default function SettingsPage({ queryRef }: Props) {
- {organization.horizontalLogoUrl && ( + {(horizontalLogoPreview || organization.horizontalLogoUrl) && (
{__("Horizontal @@ -460,17 +443,18 @@ export default function SettingsPage({ queryRef }: Props) { )} - {organization.horizontalLogoUrl ? __("Change horizontal logo") : __("Upload horizontal logo")} + {(horizontalLogoPreview || organization.horizontalLogoUrl) ? __("Change horizontal logo") : __("Upload horizontal logo")} - {organization.horizontalLogoUrl && ( + {(organization.horizontalLogoUrl && !horizontalLogoFile) && (
- {formState.isDirty && ( + {(formState.isDirty || logoFile || horizontalLogoFile) && (