Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | 5x 101x 101x 101x 101x 101x 101x 101x 101x 91x 120x 120x 101x 75x 6x 6x 6x 14x 6x 101x 75x 101x 54x 101x 4x 3x 3x 3x 1x 1x 1x 1x 1x 101x 2x 2x 101x 4x 4x 2x 5x 4x 101x 1x 1x 1x 1x 1x 4x 1x 4x 1x 1x 1x 1x 101x 42x 101x 101x 101x 1x 4x 16x 16x 16x | import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import {
composePermission,
useAccessControl,
} from "features/Permissions/AccessControl";
import { difference } from "lodash";
import { useEffect, useMemo, useRef } from "react";
import { useForm } from "react-hook-form";
import {
BulkActionType,
BulkSelectArgs,
PermissionResponseType,
TPermission,
} from "../types";
import { useUpdatePermissions } from "features/Permissions/Modal/api";
import { useGetCurrentMerchantId } from "@hooks/common";
import { useQueryClient } from "react-query";
import { QKEY_LIST_TEAM_MEMBER_PERMISSIONS } from "@constants/queryKeys";
interface FormValues {
selected: string[];
}
interface Props {
data?: PermissionResponseType[];
hasSearchOrFilter?: boolean;
memberId?: number;
isFetching?: boolean;
}
export const usePermissions = ({
data,
hasSearchOrFilter,
memberId = 0,
isFetching,
}: Props) => {
const { merchantId } = useGetCurrentMerchantId();
const queryClient = useQueryClient();
const { mutateAsync: updatePermissions, isLoading: isBulkLoading } =
useUpdatePermissions(merchantId, memberId);
const methods = useForm<FormValues>({
defaultValues: { selected: [] },
});
const isRefetchingAfterBulk = useRef(false);
const {
setValue,
watch,
reset,
formState: { isDirty },
} = methods;
const selected = watch("selected");
const flatData = data
?.map((item) => item?.permissions)
.flat(1)
?.filter((item) => !(item?.isGlobal && item?.isDeny && item?.isAttached));
const allIds = flatData?.map((item) => item?.id);
const selectedInScopeCount = useMemo(() => {
if (!allIds?.length || !selected?.length) return 0;
const allIdSet = new Set(allIds);
let count = 0;
for (const id of selected) {
Eif (allIdSet.has(id)) count += 1;
}
return count;
}, [allIds, selected]);
const isCheckedAll = useMemo(() => {
return Boolean(
allIds?.length && difference(allIds, selected).length === 0 && isDirty,
);
}, [allIds, selected, isDirty]);
const isCheckedIndeterminate = useMemo(() => {
return Boolean(
allIds?.length &&
selectedInScopeCount > 0 &&
selectedInScopeCount < allIds.length &&
isDirty,
);
}, [allIds, selectedInScopeCount, isDirty]);
const handleBulkSelect = ({
checked,
isIndeterminate,
allData,
callback, //callback is used to handle logic for indeterminate view
filterOnly,
}: BulkSelectArgs) => {
//if the checkbox is currently unchecked ir in indeterminate view, select all available data
if ((checked || isIndeterminate) && allData.length > 0) {
const filteredArray = difference(allData, selected);
setValue("selected", [...selected, ...filteredArray], {
shouldDirty: true,
shouldValidate: false,
});
callback?.(true);
//if the checkbox is already checked, and user uses search/filter to manipulate data OR if we are de-selecting groups
} else if ((filterOnly || hasSearchOrFilter) && allData?.length > 0) {
const filteredArrayAfterSearch = difference(selected, allData);
setValue("selected", filteredArrayAfterSearch, {
shouldDirty: true,
shouldValidate: false,
});
Eif (filterOnly && callback && !hasSearchOrFilter) {
//if we are removing data only by filtering and not completely use the callback to set the bulk checkbox state properly
callback(false);
}
//to clear all checkbox selection when there's no search/filter
} else E{
reset();
callback?.(false);
}
};
const handleSelectAll = (
_event: React.ChangeEvent<HTMLInputElement>,
checked: boolean,
) => {
Eif (allIds)
handleBulkSelect({
checked,
isIndeterminate: isCheckedIndeterminate,
allData: allIds,
});
};
const onSingleSelect = (checked: boolean, id: string) => {
let newSelectionVal = [];
if (checked) {
newSelectionVal = [...selected, id];
} else {
newSelectionVal = selected?.filter((val: string) => val !== id);
}
setValue("selected", newSelectionVal);
};
const handleBulkAction = async (value: BulkActionType) => {
Iif (!(selected.length > 0 && flatData)) return;
let payload: any = null;
if (value === "allow" || value === "deny") {
const isDenyAction = value === "deny";
// Permissions corresponding to the current selection
const selectedPermissions = flatData.filter((permission) =>
selected.includes(permission.id),
);
// When doing bulk allow/deny, any selected permission that is already attached
// should be treated as a "detach" operation instead of changing its effect.
const detachIds = selectedPermissions
.filter((permission) => permission.isAttached && !permission.isGlobal)
.map((permission) => permission.id);
// For the remaining permissions, keep the previous behaviour of only sending
// ids whose effect (allow/deny) will actually change.
let actionIds = filterIdsByPermissions(
selected,
flatData,
isDenyAction,
).filter((id) => !detachIds.includes(id));
Iif (detachIds.length || actionIds.length) {
payload = {};
if (detachIds.length) {
payload.detach = detachIds;
}
if (actionIds.length) {
payload[value] = actionIds;
}
}
} else Eif (value === "detach") {
if (isCheckedAll) {
payload = { resetToDefault: true };
} else {
const detachIds = filterIdsByPermissions(selected, flatData);
if (detachIds.length) {
payload = { detach: detachIds };
}
}
}
Iif (payload) {
isRefetchingAfterBulk.current = true;
await updatePermissions(payload, {
onSuccess: () => {
queryClient.invalidateQueries(QKEY_LIST_TEAM_MEMBER_PERMISSIONS);
reset();
},
});
} else {
reset();
}
};
useEffect(() => {
Iif (isRefetchingAfterBulk.current && !isFetching) {
isRefetchingAfterBulk.current = false;
}
}, [isFetching]);
const handleResetOnTypeChange = () => {
reset();
};
const isEditAllowed = useAccessControl({
resource: composePermission(
RESOURCE_BASE.MEMBER,
RESOURCE_BASE.ACCESS_POLICIES,
),
operation: OPERATIONS.UPDATE,
withPortal: true,
});
return {
methods,
isDirty,
handleSelectAll,
handleBulkSelect,
isCheckedAll,
isCheckedIndeterminate,
selected,
isEditAllowed,
isBulkLoading:
isBulkLoading || (isFetching && isRefetchingAfterBulk.current),
onSingleSelect,
handleBulkAction,
handleResetOnTypeChange,
};
};
function filterIdsByPermissions(
idsArray: string[],
permissionsArray: TPermission[],
isDeny?: boolean,
) {
return idsArray.filter((id) =>
permissionsArray.some((permission) => {
const defaultCheck = permission?.id === id;
Eif (isDeny !== undefined)
return defaultCheck && permission?.isDeny !== isDeny;
return defaultCheck && permission?.isAttached && !permission?.isGlobal;
}),
);
}
|