diff --git a/src/screens/dialogs/access-file-dialog.tsx b/src/screens/dialogs/access-file-dialog.tsx index 685fe55..0d77054 100644 --- a/src/screens/dialogs/access-file-dialog.tsx +++ b/src/screens/dialogs/access-file-dialog.tsx @@ -7,6 +7,7 @@ import { AuthContext } from "../../context/index"; import toast from "react-hot-toast"; +import { unshareFileService } from "../../services/files/unshare-file.service"; export const AccessManagementDialog = () => { const [usersWithAccess, setUsersWithAccess] = useState([]); @@ -38,7 +39,24 @@ export const AccessManagementDialog = () => { const handleSave = async () => {}; - const handleUnshare = async () => {}; + const handleUnshare = async (userName: string) => { + const unShareRequest = { + token: session?.token as string, + fileUUID: selectedFile?.uuid as string, + otherUsername: userName + }; + + const { success, msg } = await unshareFileService(unShareRequest); + if (!success) { + toast.error(msg); + return; + } + + setUsersWithAccess( + usersWithAccess.filter((username) => username !== userName) + ); + toast.success(msg); + }; return ( { {user} diff --git a/src/services/files/unshare-file.service.ts b/src/services/files/unshare-file.service.ts new file mode 100644 index 0000000..218411b --- /dev/null +++ b/src/services/files/unshare-file.service.ts @@ -0,0 +1,50 @@ +import { ENVIRONMENT } from "../../config/environment"; +import axios, { AxiosError } from "axios"; +type UnshareFileRequest = { + token: string; + fileUUID: string; + otherUsername: string; +}; + +type UnshareFileResponse = { + success: boolean; + msg: string; +}; + +export const unshareFileService = async ( + req: UnshareFileRequest +): Promise => { + const URL = `${ENVIRONMENT.PROXY_BASE_URL}/file/unshare`; + + try { + const unshareFileResponse = await axios.post( + URL, + { + fileUUID: req.fileUUID, + otherUsername: req.otherUsername + }, + { + headers: { + Authorization: `Bearer ${req.token}` + } + } + ); + const { data } = unshareFileResponse; + + return { + success: true, + msg: data.msg + }; + } catch (error) { + let errorMsg = "There was an error while trying to un-share the file"; + + if (error instanceof AxiosError) { + errorMsg = error.response?.data.msg || errorMsg; + } + + return { + success: false, + msg: errorMsg + }; + } +};