Skip to content

Commit

Permalink
fix: read-only Auth fields (#2781)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlessioGr authored and DanRibbens committed Jun 6, 2023
1 parent 576af01 commit 60f5522
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import Label from '../../Label';
import Error from '../../Error';
import { useFormFields } from '../../Form/context';
import { Field } from '../../Form/types';
import type { Props } from './types';

import './index.scss';

const ConfirmPassword: React.FC = () => {
const ConfirmPassword: React.FC<Props> = (props) => {
const {
disabled,
} = props;

const password = useFormFields<Field>(([fields]) => fields.password);
const { t } = useTranslation('fields');

Expand Down Expand Up @@ -59,6 +64,7 @@ const ConfirmPassword: React.FC = () => {
autoComplete="off"
id="field-confirm-password"
name="confirm-password"
disabled={!!disabled}
/>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export type Props = {
disabled?: boolean
}
3 changes: 2 additions & 1 deletion src/admin/components/forms/field-types/Password/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const Password: React.FC<Props> = (props) => {
width,
autoComplete,
label,
disabled,
} = props;

const path = pathFromProps || name;
Expand Down Expand Up @@ -67,7 +68,7 @@ const Password: React.FC<Props> = (props) => {
id={`field-${path.replace(/\./gi, '__')}`}
value={value as string || ''}
onChange={setValue}
disabled={formProcessing}
disabled={formProcessing || disabled}
type="password"
autoComplete={autoComplete}
name={path}
Expand Down
3 changes: 2 additions & 1 deletion src/admin/components/forms/field-types/Password/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ export type Props = {
required?: boolean
validate?: Validate
style?: React.CSSProperties
className?: string,
className?: string
width?: string
label?: string
description?: Description
disabled?: boolean
}
1 change: 1 addition & 0 deletions src/admin/components/views/Account/Default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const DefaultAccount: React.FC<Props> = (props) => {
collection={collection}
email={data?.email}
operation="update"
readOnly={!hasSavePermission}
/>
<RenderFields
permissions={permissions.fields}
Expand Down
12 changes: 7 additions & 5 deletions src/admin/components/views/collections/Edit/Auth/APIKey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import GenerateConfirmation from '../../../../elements/GenerateConfirmation';
const path = 'apiKey';
const baseClass = 'api-key';

const APIKey: React.FC = () => {
const APIKey: React.FC<{readOnly?: boolean}> = ({ readOnly }) => {
const [initialAPIKey, setInitialAPIKey] = useState(null);
const [highlightedField, setHighlightedField] = useState(false);
const { t } = useTranslation();
Expand Down Expand Up @@ -90,10 +90,12 @@ const APIKey: React.FC = () => {
name="apiKey"
/>
</div>
<GenerateConfirmation
setKey={() => setValue(uuidv4())}
highlightField={highlightField}
/>
{!readOnly && (
<GenerateConfirmation
setKey={() => setValue(uuidv4())}
highlightField={highlightField}
/>
)}
</React.Fragment>
);
};
Expand Down
14 changes: 10 additions & 4 deletions src/admin/components/views/collections/Edit/Auth/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import './index.scss';
const baseClass = 'auth-fields';

const Auth: React.FC<Props> = (props) => {
const { useAPIKey, requirePassword, verify, collection: { slug }, collection, email, operation } = props;
const { useAPIKey, requirePassword, verify, collection: { slug }, collection, email, operation, readOnly } = props;
const [changingPassword, setChangingPassword] = useState(requirePassword);
const enableAPIKey = useFormFields(([fields]) => fields.enableAPIKey);
const dispatchFields = useFormFields((reducer) => reducer[1]);
Expand Down Expand Up @@ -79,7 +79,7 @@ const Auth: React.FC<Props> = (props) => {
required
name="email"
label={t('general:email')}
admin={{ autoComplete: 'email' }}
admin={{ autoComplete: 'email', readOnly }}
/>
{(changingPassword || requirePassword) && (
<div className={`${baseClass}__changing-password`}>
Expand All @@ -88,13 +88,15 @@ const Auth: React.FC<Props> = (props) => {
required
name="password"
label={t('newPassword')}
disabled={readOnly}
/>
<ConfirmPassword />
<ConfirmPassword disabled={readOnly} />
{!requirePassword && (
<Button
size="small"
buttonStyle="secondary"
onClick={() => handleChangePassword(false)}
disabled={readOnly}
>
{t('general:cancel')}
</Button>
Expand All @@ -107,6 +109,7 @@ const Auth: React.FC<Props> = (props) => {
size="small"
buttonStyle="secondary"
onClick={() => handleChangePassword(true)}
disabled={readOnly}
>
{t('changePassword')}
</Button>
Expand All @@ -116,6 +119,7 @@ const Auth: React.FC<Props> = (props) => {
size="small"
buttonStyle="secondary"
onClick={() => unlock()}
disabled={readOnly}
>
{t('forceUnlock')}
</Button>
Expand All @@ -127,16 +131,18 @@ const Auth: React.FC<Props> = (props) => {
<Checkbox
label={t('enableAPIKey')}
name="enableAPIKey"
admin={{ readOnly }}
/>
{enableAPIKey?.value && (
<APIKey />
<APIKey readOnly={readOnly} />
)}
</div>
)}
{verify && (
<Checkbox
label={t('verified')}
name="_verified"
admin={{ readOnly }}
/>
)}
</div>
Expand Down
1 change: 1 addition & 0 deletions src/admin/components/views/collections/Edit/Auth/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export type Props = {
collection: SanitizedCollectionConfig
email: string
operation: 'update' | 'create'
readOnly: boolean
}
1 change: 1 addition & 0 deletions src/admin/components/views/collections/Edit/Default.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ const DefaultEditView: React.FC<Props> = (props) => {
collection={collection}
email={data?.email}
operation={operation}
readOnly={!hasSavePermission}
/>
)}

Expand Down

0 comments on commit 60f5522

Please sign in to comment.