🕹 Manage root users

🕹 Manage root users
This commit is contained in:
Montassar Ghanmy
2023-08-28 09:23:52 +01:00
committed by GitHub
parent 832ab8ac90
commit 84270a31e2
29 changed files with 410 additions and 45 deletions
@@ -175,4 +175,11 @@ export class DriveApiClient {
return res;
}
static async updateLevel(companyId: string, id: string, update: any) {
return await Api.post<any, any>(
`/internal/services/documents/v1/companies/${companyId}/item/${id}${appendTdriveToken()}/level`,
update,
);
}
}
@@ -6,7 +6,7 @@ import { DriveApiClient } from '../api-client/api-client';
import { DriveItemAtom, DriveItemChildrenAtom } from '../state/store';
import { BrowseFilter, DriveItem, DriveItemVersion } from '../types';
import { SharedWithMeFilterState } from '../state/shared-with-me-filter';
import Languages from "features/global/services/languages-service";
import Languages from 'features/global/services/languages-service';
/**
* Returns the children of a drive item
@@ -21,7 +21,7 @@ export const useDriveActions = () => {
({ set, snapshot }) =>
async (parentId: string) => {
if (parentId) {
const filter:BrowseFilter = {
const filter: BrowseFilter = {
company_id: companyId,
mime_type: sharedFilter.mimeType.value,
};
@@ -110,5 +110,22 @@ export const useDriveActions = () => {
[refresh],
);
return { create, refresh, download, downloadZip, remove, update };
const updateLevel = useCallback(
async (id: string, userId: string, level: string) => {
try {
const updateBody = {
company_id: companyId,
user_id: userId,
level: level
}
await DriveApiClient.updateLevel(companyId, id, updateBody);
await refresh(id || '');
} catch (e) {
ToasterService.error(Languages.t('hooks.use-drive-actions.unable_update_file'));
}
},
[refresh],
);
return { create, refresh, download, downloadZip, remove, update, updateLevel };
};
@@ -19,7 +19,7 @@ export const useDriveItem = (id: string) => {
const item = useRecoilValue(DriveItemAtom(id));
const children = useRecoilValue(DriveItemChildrenAtom(id));
const [loading, setLoading] = useRecoilState(LoadingStateInitTrue('useDriveItem-' + id));
const { refresh: refreshItem, create, update: _update, remove: _remove } = useDriveActions();
const { refresh: refreshItem, create, update: _update, updateLevel: _updateLevel, remove: _remove } = useDriveActions();
const { uploadVersion: _uploadVersion } = useDriveUpload();
const refresh = useCallback(
@@ -57,6 +57,19 @@ export const useDriveItem = (id: string) => {
[id, setLoading, refresh, item?.item?.parent_id],
);
const updateLevel = useCallback(
async (userId: string, level: string) => {
setLoading(true);
try {
await _updateLevel(id, userId, level);
} catch (e) {
ToasterService.error('Unable to update user access.');
}
setLoading(false);
},
[id, setLoading, refresh, item?.item?.parent_id],
);
const uploadVersion = useCallback(
async (file: File) => {
setLoading(true);
@@ -87,6 +100,7 @@ export const useDriveItem = (id: string) => {
uploadVersion,
create,
update,
updateLevel,
remove,
refresh,
};
@@ -97,6 +97,28 @@ class UserAPIClientService {
return res;
}
async all(
companyId: string,
options?: { bufferize?: boolean; callback?: (res: UserType[]) => void },
): Promise<UserType[]> {
const res = await new Promise<UserType[]>(resolve =>
Api.post(
`/internal/services/users/v1/users/${companyId}/all`,
{
include_companies: true,
},
(res: { resources: UserType[] }): void => {
resolve(res.resources && res.resources.length ? res.resources : []);
},
),
);
if (options?.callback) options.callback(res);
return res;
}
async getCurrentUserCompanies(): Promise<CompanyType[]> {
return this.listCompanies(CurrentUser.get()?.id || '');
}
@@ -0,0 +1,17 @@
import { useEffect, useState } from 'react';
import useRouterCompany from '@features/router/hooks/use-router-company';
import { UserType } from '@features/users/types/user';
import UserAPIClient from '@features/users/api/user-api-client';
export const useUserCompanyList = (): UserType[] => {
const companyId = useRouterCompany();
const [users, setUsers] = useState<UserType[]>([]);
const refresh = async () => {
const updatedData = await UserAPIClient.all(companyId);
setUsers(updatedData);
};
useEffect(() => {
refresh();
}, []);
return users;
};