Skip to content

Commit

Permalink
Resolve DOO-113 (#79)
Browse files Browse the repository at this point in the history
* added live collab for file upload

* fix linting problems
  • Loading branch information
AbdallaAbdelhadi authored Mar 3, 2024
1 parent adaa209 commit 935befc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
10 changes: 7 additions & 3 deletions client/src/components/lib/UploadFilesToFirebase.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import React, { useState } from 'react';
import React from 'react';
import { getDownloadURL, getStorage, ref, uploadBytes } from 'firebase/storage';
import { firebaseApp } from '../../firebaseDB/firebase';
import { useCanvasElementStore } from '@/stores/CanvasElementsStore';
import { useWebSocketStore } from '@/stores/WebSocketStore';

/**
* Defines a context menu option that allows users to attatch a file to a
* canvas element by uploading it to firebase
* @author Dana El Sherif
*/
const FileUpload = () => {
const [, setFile] = useState<File | null>(null);
const { selectedElementIds, updateAttachedFileUrl } = useCanvasElementStore([
'selectedElementIds',
'updateAttachedFileUrl',
]);
const { setWebsocketAction } = useWebSocketStore(['setWebsocketAction']);

const onFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const files = event.target.files;
const fileToUpload = files ? files[0] : null;
setFile(fileToUpload);

if (fileToUpload !== null && selectedElementIds.length > 0) {
const storage = getStorage(firebaseApp);
Expand All @@ -30,6 +30,10 @@ const FileUpload = () => {
})
.then((downloadURL) => {
updateAttachedFileUrl(selectedElementIds[0], downloadURL);
setWebsocketAction(
{ selectedElementIds, downloadURL },
'addAttachedFileUrl',
);
})
.catch(() => {
alert('Error uploading');
Expand Down
13 changes: 13 additions & 0 deletions client/src/hooks/useSocket.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const useSocket = () => {
]);

const {
removeAttachedFileUrl,
updateAttachedFileUrl,
setCanvasElementState,
addCanvasShape,
addCanvasFreehand,
Expand Down Expand Up @@ -67,6 +69,8 @@ export const useSocket = () => {
textStrings,
fileIds,
} = useCanvasElementStore([
'removeAttachedFileUrl',
'updateAttachedFileUrl',
'setCanvasElementState',
'addCanvasShape',
'addCanvasFreehand',
Expand Down Expand Up @@ -246,6 +250,15 @@ export const useSocket = () => {
addNewCollab: (newUser: SharedUser) => {
addUser(newUser);
},
addAttachedFileUrl: (params: {
selectedElementIds: string;
downloadURL: string;
}) => {
updateAttachedFileUrl(params.selectedElementIds[0], params.downloadURL);
},
removeAttachedFileUrl: (ids: string[]) => {
removeAttachedFileUrl(ids);
},
};

// Intialize socket
Expand Down
11 changes: 8 additions & 3 deletions client/src/stores/WebSocketStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export const Actions = [
'removeComment',
'changePermission',
'addNewCollab',
'addAttachedFileUrl',
'removeAttachedFileUrl',
] as const;
export type ActionsType = typeof Actions;

Expand Down Expand Up @@ -58,7 +60,8 @@ interface WebSocketState {
| string[]
| UpdatedTimeMessage
| { elemID: string; comment: Partial<Comment> }
| { collabID: string; permission: string };
| { collabID: string; permission: string }
| { selectedElementIds: string[]; downloadURL: string };
// The current active tenants
activeTenants: Record<string, User>;
// The current cursor positions keyed by user ID
Expand All @@ -77,7 +80,8 @@ interface WebSocketActions {
| string[]
| UpdatedTimeMessage
| { elemID: string; comment: Partial<Comment> }
| { collabID: string; permission: string },
| { collabID: string; permission: string }
| { selectedElementIds: string[]; downloadURL: string },
action: string,
) => void;
// Set the socket reference
Expand Down Expand Up @@ -119,7 +123,8 @@ const setWebsocketAction =
| string[]
| UpdatedTimeMessage
| { elemID: string; comment: Partial<Comment> }
| { collabID: string; permission: string },
| { collabID: string; permission: string }
| { selectedElementIds: string[]; downloadURL: string },
action: string,
) =>
set(() => {
Expand Down

0 comments on commit 935befc

Please sign in to comment.