Skip to content

Commit 483339c

Browse files
authored
fix(clerk-js): Hide slug field on OrganizationProfile based on environment settings (#7001)
1 parent d8147fb commit 483339c

File tree

4 files changed

+54
-13
lines changed

4 files changed

+54
-13
lines changed

.changeset/quick-trams-sink.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Hide slug field on `OrganizationProfile` based on environment settings

packages/clerk-js/src/ui/components/OrganizationProfile/ProfileForm.tsx

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { useOrganization } from '@clerk/shared/react';
2+
import type { UpdateOrganizationParams } from '@clerk/types';
23
import React from 'react';
34

5+
import { useEnvironment } from '@/ui/contexts';
46
import { useCardState, withCardStateProvider } from '@/ui/elements/contexts';
57
import { Form } from '@/ui/elements/Form';
68
import { FormButtons } from '@/ui/elements/FormButtons';
@@ -20,6 +22,7 @@ export const ProfileForm = withCardStateProvider((props: ProfileFormProps) => {
2022
const title = localizationKeys('organizationProfile.profilePage.title');
2123
const card = useCardState();
2224
const { organization } = useOrganization();
25+
const { organizationSettings } = useEnvironment();
2326

2427
const nameField = useFormControl('name', organization?.name || '', {
2528
type: 'text',
@@ -39,14 +42,20 @@ export const ProfileForm = withCardStateProvider((props: ProfileFormProps) => {
3942

4043
const dataChanged = organization.name !== nameField.value || organization.slug !== slugField.value;
4144
const canSubmit = dataChanged && slugField.feedbackType !== 'error';
45+
const organizationSlugEnabled = !organizationSettings.slug.disabled;
4246

4347
const onSubmit = async (e: React.FormEvent) => {
4448
e.preventDefault();
45-
return (canSubmit ? organization.update({ name: nameField.value, slug: slugField.value }) : Promise.resolve())
46-
.then(onSuccess)
47-
.catch(err => {
48-
handleError(err, [nameField, slugField], card.setError);
49-
});
49+
50+
const updateOrgParams: UpdateOrganizationParams = { name: nameField.value };
51+
52+
if (organizationSlugEnabled) {
53+
updateOrgParams.slug = slugField.value;
54+
}
55+
56+
return (canSubmit ? organization.update(updateOrgParams) : Promise.resolve()).then(onSuccess).catch(err => {
57+
handleError(err, [nameField, slugField], card.setError);
58+
});
5059
};
5160

5261
const uploadAvatar = (file: File) => {
@@ -92,13 +101,15 @@ export const ProfileForm = withCardStateProvider((props: ProfileFormProps) => {
92101
ignorePasswordManager
93102
/>
94103
</Form.ControlRow>
95-
<Form.ControlRow elementId={slugField.id}>
96-
<Form.PlainInput
97-
{...slugField.props}
98-
onChange={onChangeSlug}
99-
ignorePasswordManager
100-
/>
101-
</Form.ControlRow>
104+
{organizationSlugEnabled && (
105+
<Form.ControlRow elementId={slugField.id}>
106+
<Form.PlainInput
107+
{...slugField.props}
108+
onChange={onChangeSlug}
109+
ignorePasswordManager
110+
/>
111+
</Form.ControlRow>
112+
)}
102113
<FormButtons
103114
isDisabled={!canSubmit}
104115
onReset={onReset}

packages/clerk-js/src/ui/components/OrganizationProfile/__tests__/OrganizationGeneralPage.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ describe('OrganizationSettings', () => {
246246
it('open the profile section', async () => {
247247
const { wrapper } = await createFixtures(f => {
248248
f.withOrganizations();
249+
f.withOrganizationSlug(true);
249250
f.withUser({
250251
email_addresses: ['[email protected]'],
251252
organization_memberships: [{ name: 'Org1', slug: 'Org1' }],

packages/clerk-js/src/ui/components/OrganizationProfile/__tests__/ProfileSettingsPage.test.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ describe('OrganizationProfileScreen', () => {
6666
await userEvent.type(getByLabelText(/^name/i), '234');
6767
expect(getByDisplayValue('Org1234')).toBeDefined();
6868
await userEvent.click(getByRole('button', { name: /save/i }));
69-
expect(fixtures.clerk.organization?.update).toHaveBeenCalledWith({ name: 'Org1234', slug: '' });
69+
expect(fixtures.clerk.organization?.update).toHaveBeenCalledWith({ name: 'Org1234' });
7070
});
7171

7272
it('updates organization slug on clicking continue', async () => {
7373
const { wrapper, fixtures } = await createFixtures(f => {
7474
f.withOrganizations();
75+
f.withOrganizationSlug(true);
7576
f.withUser({
7677
email_addresses: ['[email protected]'],
7778
organization_memberships: [{ name: 'Org1', slug: '', role: 'admin' }],
@@ -91,4 +92,27 @@ describe('OrganizationProfileScreen', () => {
9192
await userEvent.click(getByRole('button', { name: /save$/i }));
9293
expect(fixtures.clerk.organization?.update).toHaveBeenCalledWith({ name: 'Org1', slug: 'my-org' });
9394
});
95+
96+
it("does not display slug field if it's disabled on environment", async () => {
97+
const { wrapper, fixtures } = await createFixtures(f => {
98+
f.withOrganizations();
99+
f.withOrganizationSlug(false);
100+
f.withUser({
101+
email_addresses: ['[email protected]'],
102+
organization_memberships: [{ name: 'Org1', role: 'admin' }],
103+
});
104+
});
105+
106+
fixtures.clerk.organization?.update.mockResolvedValue({} as OrganizationResource);
107+
const { queryByLabelText, userEvent, getByRole } = render(
108+
<ProfileForm
109+
onSuccess={vi.fn()}
110+
onReset={vi.fn()}
111+
/>,
112+
{ wrapper },
113+
);
114+
expect(queryByLabelText(/Slug/i)).not.toBeInTheDocument();
115+
await userEvent.click(getByRole('button', { name: /save$/i }));
116+
expect(fixtures.clerk.organization?.update).toHaveBeenCalledWith({ name: 'Org1' });
117+
});
94118
});

0 commit comments

Comments
 (0)