Skip to content

Commit

Permalink
feat: un-share files (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
MiguelMRojas authored Oct 27, 2023
1 parent 89ec493 commit 7633ca6
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/screens/dialogs/access-file-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[]>([]);
Expand Down Expand Up @@ -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 (
<Dialog
Expand Down Expand Up @@ -69,7 +87,7 @@ export const AccessManagementDialog = () => {
<span>{user}</span>
<button
className="hover-bg-red-600 mt-0.5 rounded-md bg-red-500 px-2 py-1 text-white"
onClick={() => handleUnshare()}
onClick={() => handleUnshare(user)}
>
Un-share
</button>
Expand Down
50 changes: 50 additions & 0 deletions src/services/files/unshare-file.service.ts
Original file line number Diff line number Diff line change
@@ -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<UnshareFileResponse> => {
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
};
}
};

0 comments on commit 7633ca6

Please sign in to comment.