Version 0.7.0
This commit is contained in:
parent
64ad8498f5
commit
6b6bbdc142
112 changed files with 3828 additions and 188 deletions
|
|
@ -3,8 +3,14 @@ import {
|
|||
createRoleApi,
|
||||
updateRoleApi,
|
||||
removeRoleApi,
|
||||
assignRoleApi,
|
||||
unassignRoleApi,
|
||||
fetchPermissionsApi,
|
||||
IUpdateRoleRequest,
|
||||
} from "../../api/role";
|
||||
import { deleteRole, setRole } from "../../store/role";
|
||||
import { deleteRole, setPermissions, setRole } from "../../store/role";
|
||||
import { state } from "../../store/state";
|
||||
import { fetchUserCommunityRoles } from "../user";
|
||||
|
||||
const fetchRole = async (id: string) => {
|
||||
const data = await fetchRoleApi({
|
||||
|
|
@ -22,6 +28,7 @@ const createRole = async (name: string, communityId: string) => {
|
|||
const data = await createRoleApi({
|
||||
name: name,
|
||||
communityId: communityId,
|
||||
permissions: [],
|
||||
});
|
||||
|
||||
if (typeof data.error === "string") {
|
||||
|
|
@ -31,11 +38,8 @@ const createRole = async (name: string, communityId: string) => {
|
|||
setRole(data);
|
||||
};
|
||||
|
||||
const updateRole = async (id: string, name?: string) => {
|
||||
const data = await updateRoleApi({
|
||||
id: id,
|
||||
name: name,
|
||||
});
|
||||
const updateRole = async (updateRoleData: IUpdateRoleRequest) => {
|
||||
const data = await updateRoleApi(updateRoleData);
|
||||
|
||||
if (typeof data.error === "string") {
|
||||
return;
|
||||
|
|
@ -56,4 +60,91 @@ const removeRole = async (id: string) => {
|
|||
deleteRole(data.id);
|
||||
};
|
||||
|
||||
export { fetchRole, createRole, updateRole, removeRole };
|
||||
const assignRole = async (id: string, userId: string) => {
|
||||
const data = await assignRoleApi({
|
||||
id: id,
|
||||
userId: userId,
|
||||
});
|
||||
|
||||
if (typeof data.error === "string") {
|
||||
return;
|
||||
}
|
||||
|
||||
fetchUserCommunityRoles(data.userId, data.communityId);
|
||||
};
|
||||
|
||||
const unassignRole = async (id: string, userId: string) => {
|
||||
const data = await unassignRoleApi({
|
||||
id: id,
|
||||
userId: userId,
|
||||
});
|
||||
|
||||
if (typeof data.error === "string") {
|
||||
return;
|
||||
}
|
||||
|
||||
fetchUserCommunityRoles(data.userId, data.communityId);
|
||||
};
|
||||
|
||||
const fetchPermissions = async () => {
|
||||
const data = await fetchPermissionsApi();
|
||||
|
||||
if (typeof data.error === "string") {
|
||||
return;
|
||||
}
|
||||
|
||||
setPermissions(data.permissions);
|
||||
};
|
||||
|
||||
const getUserBestRoleId = (
|
||||
userId: string,
|
||||
communityId: string,
|
||||
): string | undefined => {
|
||||
const user = state.user.users[userId];
|
||||
const community = state.community.communities[communityId];
|
||||
if (!user || !community) {
|
||||
return;
|
||||
}
|
||||
|
||||
const communityRoleIds = community.roles;
|
||||
if (!communityRoleIds) {
|
||||
return;
|
||||
}
|
||||
const communityRoles = communityRoleIds
|
||||
.map((roleId) => {
|
||||
const role = state.role.roles[roleId];
|
||||
if (!role) {
|
||||
return {
|
||||
id: roleId,
|
||||
};
|
||||
}
|
||||
|
||||
return role;
|
||||
})
|
||||
.filter((role) => role.id !== "" && role.showInMembers);
|
||||
|
||||
let bestRoleOrder = 999999;
|
||||
let bestRoleId: string | undefined = undefined;
|
||||
communityRoles.forEach((role) => {
|
||||
if (
|
||||
(role.order ?? 999999) < bestRoleOrder &&
|
||||
(user.roles ?? {})[communityId].includes(role.id)
|
||||
) {
|
||||
bestRoleOrder = role.order ?? 999999;
|
||||
bestRoleId = role.id;
|
||||
}
|
||||
});
|
||||
|
||||
return bestRoleId;
|
||||
};
|
||||
|
||||
export {
|
||||
fetchRole,
|
||||
createRole,
|
||||
updateRole,
|
||||
removeRole,
|
||||
assignRole,
|
||||
unassignRole,
|
||||
fetchPermissions,
|
||||
getUserBestRoleId,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue