fix: scheduleId not unset properly bug (#15993)
* fix: scheduleId not unset properly bug * Refactor almost complete, couple of tests needed * More testid fixes * Add migration to fixup schedule id bug * Remove migration * fix: Refactor useLockedFieldManager for correct useState use --------- Co-authored-by: zomars <[email protected]>
This commit is contained in:
1 parent
d5f7065fdb
commit
b09b32b121
7 files changed
+155
-182
No files matched your search
@@ -15,7 +15,7 @@ import { Badge, Icon, Switch, Tooltip } from "@calcom/ui";
|
||||
|
||||
export const LockedSwitch = (
|
||||
isManagedEventType: boolean,
|
||||
[isLocked, setIsLocked]: [boolean, Dispatch<SetStateAction<boolean>>],
|
||||
[fieldState, setFieldState]: [Record<string, boolean>, Dispatch<SetStateAction<Record<string, boolean>>>],
|
||||
fieldName: string,
|
||||
setUnlockedFields: (fieldName: string, val: boolean | undefined) => void,
|
||||
options = { simple: false }
|
||||
@@ -24,10 +24,13 @@ export const LockedSwitch = (
|
||||
<Switch
|
||||
data-testid={`locked-indicator-${fieldName}`}
|
||||
onCheckedChange={(enabled) => {
|
||||
setIsLocked(enabled);
|
||||
setFieldState({
|
||||
...fieldState,
|
||||
[fieldName]: enabled,
|
||||
});
|
||||
setUnlockedFields(fieldName, !enabled || undefined);
|
||||
}}
|
||||
checked={isLocked}
|
||||
checked={fieldState[fieldName]}
|
||||
small={!options.simple}
|
||||
/>
|
||||
) : null;
|
||||
@@ -36,12 +39,13 @@ export const LockedSwitch = (
|
||||
export const LockedIndicator = (
|
||||
isChildrenManagedEventType: boolean,
|
||||
isManagedEventType: boolean,
|
||||
[isLocked, setIsLocked]: [boolean, Dispatch<SetStateAction<boolean>>],
|
||||
[fieldState, setFieldState]: [Record<string, boolean>, Dispatch<SetStateAction<Record<string, boolean>>>],
|
||||
t: TFunction,
|
||||
fieldName: string,
|
||||
setUnlockedFields: (fieldName: string, val: boolean | undefined) => void,
|
||||
options = { simple: false }
|
||||
) => {
|
||||
const isLocked = fieldState[fieldName];
|
||||
const stateText = t(isLocked ? "locked" : "unlocked");
|
||||
const tooltipText = t(
|
||||
`${isLocked ? "locked" : "unlocked"}_fields_${isManagedEventType ? "admin" : "member"}_description`
|
||||
@@ -67,7 +71,10 @@ export const LockedIndicator = (
|
||||
<Switch
|
||||
data-testid={`locked-indicator-${fieldName}`}
|
||||
onCheckedChange={(enabled) => {
|
||||
setIsLocked(enabled);
|
||||
setFieldState({
|
||||
...fieldState,
|
||||
[fieldName]: enabled,
|
||||
});
|
||||
setUnlockedFields(fieldName, !enabled || undefined);
|
||||
}}
|
||||
checked={isLocked}
|
||||
@@ -91,7 +98,7 @@ const useLockedFieldsManager = ({
|
||||
formMethods: UseFormReturn<FormValues>;
|
||||
}) => {
|
||||
const { setValue, getValues } = formMethods;
|
||||
const fieldStates: Record<string, [boolean, Dispatch<SetStateAction<boolean>>]> = {};
|
||||
const [fieldStates, setFieldStates] = useState<Record<string, boolean>>({});
|
||||
const unlockedFields =
|
||||
(eventType.metadata?.managedEventConfig?.unlockedFields !== undefined &&
|
||||
eventType.metadata?.managedEventConfig?.unlockedFields) ||
|
||||
@@ -123,28 +130,31 @@ const useLockedFieldsManager = ({
|
||||
|
||||
const getLockedInitState = (fieldName: string): boolean => {
|
||||
let locked = isManagedEventType || isChildrenManagedEventType;
|
||||
const unlockedFieldList = getValues("metadata")?.managedEventConfig?.unlockedFields;
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
//@ts-ignore
|
||||
const fieldIsUnlocked = unlockedFieldList && unlockedFieldList[fieldName] === true;
|
||||
|
||||
if (fieldName.includes(".")) {
|
||||
locked = locked && get(unlockedFields, fieldName) === undefined;
|
||||
} else {
|
||||
type FieldName = string;
|
||||
const unlockedFieldList = getValues("metadata")?.managedEventConfig?.unlockedFields as
|
||||
| Record<FieldName, boolean>
|
||||
| undefined;
|
||||
const fieldIsUnlocked = !!unlockedFieldList?.[fieldName];
|
||||
locked = locked && !fieldIsUnlocked;
|
||||
}
|
||||
return locked;
|
||||
};
|
||||
|
||||
const useShouldLockIndicator = (fieldName: string, options?: { simple: true }) => {
|
||||
if (!fieldStates[fieldName]) {
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||
fieldStates[fieldName] = useState(getLockedInitState(fieldName));
|
||||
if (typeof fieldStates[fieldName] === "undefined") {
|
||||
setFieldStates({
|
||||
...fieldStates,
|
||||
[fieldName]: getLockedInitState(fieldName),
|
||||
});
|
||||
}
|
||||
|
||||
return LockedIndicator(
|
||||
isChildrenManagedEventType,
|
||||
isManagedEventType,
|
||||
fieldStates[fieldName],
|
||||
[fieldStates, setFieldStates],
|
||||
translate,
|
||||
fieldName,
|
||||
setUnlockedFields,
|
||||
@@ -153,12 +163,13 @@ const useLockedFieldsManager = ({
|
||||
};
|
||||
|
||||
const useLockedLabel = (fieldName: string, options?: { simple: true }) => {
|
||||
if (!fieldStates[fieldName]) {
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||
fieldStates[fieldName] = useState(getLockedInitState(fieldName));
|
||||
if (typeof fieldStates[fieldName] === "undefined") {
|
||||
setFieldStates({
|
||||
...fieldStates,
|
||||
[fieldName]: getLockedInitState(fieldName),
|
||||
});
|
||||
}
|
||||
const isLocked = fieldStates[fieldName][0];
|
||||
|
||||
const isLocked = fieldStates[fieldName];
|
||||
return {
|
||||
disabled:
|
||||
!isManagedEventType &&
|
||||
@@ -170,19 +181,22 @@ const useLockedFieldsManager = ({
|
||||
};
|
||||
|
||||
const useLockedSwitch = (fieldName: string, options = { simple: false }) => {
|
||||
if (!fieldStates[fieldName]) {
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||
fieldStates[fieldName] = useState(getLockedInitState(fieldName));
|
||||
if (typeof fieldStates[fieldName] === "undefined") {
|
||||
setFieldStates({
|
||||
...fieldStates,
|
||||
[fieldName]: getLockedInitState(fieldName),
|
||||
});
|
||||
}
|
||||
|
||||
return () =>
|
||||
LockedSwitch(isManagedEventType, fieldStates[fieldName], fieldName, setUnlockedFields, options);
|
||||
LockedSwitch(isManagedEventType, [fieldStates, setFieldStates], fieldName, setUnlockedFields, options);
|
||||
};
|
||||
|
||||
const useShouldLockDisableProps = (fieldName: string, options?: { simple: true }) => {
|
||||
if (!fieldStates[fieldName]) {
|
||||
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||
fieldStates[fieldName] = useState(getLockedInitState(fieldName));
|
||||
if (typeof fieldStates[fieldName] === "undefined") {
|
||||
setFieldStates({
|
||||
...fieldStates,
|
||||
[fieldName]: getLockedInitState(fieldName),
|
||||
});
|
||||
}
|
||||
return {
|
||||
disabled:
|
||||
@@ -190,7 +204,7 @@ const useLockedFieldsManager = ({
|
||||
eventType.metadata?.managedEventConfig !== undefined &&
|
||||
unlockedFields[fieldName as keyof Omit<Prisma.EventTypeSelect, "id">] === undefined,
|
||||
LockedIcon: useShouldLockIndicator(fieldName, options),
|
||||
isLocked: fieldStates[fieldName][0],
|
||||
isLocked: fieldStates[fieldName],
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -75,7 +75,7 @@ export default function TeamList(props: Props) {
|
||||
actionButton={{
|
||||
href: `/settings/organizations/new`,
|
||||
child: t("set_up_your_organization"),
|
||||
"data-testId": "setup_your_org_action_button",
|
||||
"data-testid": "setup_your_org_action_button",
|
||||
}}
|
||||
/>
|
||||
<Card
|
||||
|
||||
Reference in new issue
Block a user