Skip to content

Commit

Permalink
Remove states and fix onChange fired after mount
Browse files Browse the repository at this point in the history
Signed-off-by: Lin Wang <[email protected]>
  • Loading branch information
wanglam committed Mar 13, 2024
1 parent 582ea9e commit a85666c
Showing 1 changed file with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,24 +164,34 @@ export const WorkspacePermissionSettingPanel = ({
permissionSettings,
lastAdminItemDeletable,
}: WorkspacePermissionSettingPanelProps) => {
const [userPermissionSettings, setUserPermissionSettings] = useState<
Array<Partial<WorkspacePermissionSetting>>
>(
permissionSettings?.filter(
(permissionSettingItem) => permissionSettingItem.type === WorkspacePermissionItemType.User
) ?? []
const userPermissionSettings = useMemo(
() =>
permissionSettings?.filter(
(permissionSettingItem) => permissionSettingItem.type === WorkspacePermissionItemType.User
) ?? [],
[permissionSettings]
);
const [groupPermissionSettings, setGroupPermissionSettings] = useState<
Array<Partial<WorkspacePermissionSetting>>
>(
permissionSettings?.filter(
(permissionSettingItem) => permissionSettingItem.type === WorkspacePermissionItemType.Group
) ?? []
const groupPermissionSettings = useMemo(
() =>
permissionSettings?.filter(
(permissionSettingItem) => permissionSettingItem.type === WorkspacePermissionItemType.Group
) ?? [],
[permissionSettings]
);

useEffect(() => {
onChange?.([...userPermissionSettings, ...groupPermissionSettings]);
}, [onChange, userPermissionSettings, groupPermissionSettings]);
const handleUserPermissionSettingsChange = useCallback(
(newSettings) => {
onChange?.([...newSettings, ...groupPermissionSettings]);
},
[groupPermissionSettings, onChange]
);

const handleGroupPermissionSettingsChange = useCallback(
(newSettings) => {
onChange?.([...userPermissionSettings, ...newSettings]);
},
[userPermissionSettings, onChange]
);

const nonDeletableIndex = useMemo(() => {
let userNonDeletableIndex = -1;
Expand Down Expand Up @@ -215,7 +225,7 @@ export const WorkspacePermissionSettingPanel = ({
defaultMessage: 'User',
})}
errors={errors}
onChange={setUserPermissionSettings}
onChange={handleUserPermissionSettingsChange}
nonDeletableIndex={userNonDeletableIndex}
permissionSettings={userPermissionSettings}
type={WorkspacePermissionItemType.User}
Expand All @@ -226,7 +236,7 @@ export const WorkspacePermissionSettingPanel = ({
defaultMessage: 'User Groups',
})}
errors={errors}
onChange={setGroupPermissionSettings}
onChange={handleGroupPermissionSettingsChange}
nonDeletableIndex={groupNonDeletableIndex}
permissionSettings={groupPermissionSettings}
type={WorkspacePermissionItemType.Group}
Expand Down

0 comments on commit a85666c

Please sign in to comment.