Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

storage works #108

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/server/prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ async function main() {
id: '339cf78e-d13f-4069-b1f7-dee0c64afb31',
firstName: 'Kai',
lastName: 'Zheng',
email: '[email protected]',
email: '[email protected]',
positionId: CHIEF_FIN_OFFICER_UUID,
},
{
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/components/FormInstance.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const FormInstance = ({
const router = useRouter();
const toast = useToast();
const { user } = useAuth();
const { mockBlob: formBlob } = useStorage(formInstance);
const { formBlob } = useStorage(formInstance);

const signFormInstanceMutation = useMutation({
mutationFn: async ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const CreateFormInstanceModal: React.FC<CreateFormInstanceModalProps> = ({
const [isFormTypeDropdownOpen, setIsFormTypeDropdownOpen] = useState(false);
const [selectedFormTemplate, setSelectedFormTemplate] =
useState<FormTemplateEntity | null>(null);
const { mockBlob: formBlob } = useStorage(selectedFormTemplate);
const { formBlob } = useStorage(selectedFormTemplate);
const [formTypeSelected, setFormTypeSelected] = useState(false);
const [signaturePositions, setSignaturePositions] = useState<
(Option | null)[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { SignatureField } from './SignatureField';
import { TempSignatureField } from './types';
import { v4 as uuidv4 } from 'uuid';
import { queryClient } from '@web/pages/_app';
import { mockStorage } from '@web/services/storage.service';
import { storage } from '@web/services/storage.service';

const variants = {
notDragging: {
Expand Down Expand Up @@ -106,7 +106,7 @@ export const CreateFormTemplateModal = ({
handleModalClose();
if (pdfFile) {
// change to storage.uploadBlob when storage is set up
await mockStorage.uploadBlob(
await storage.uploadBlob(
pdfFile,
response.name.replaceAll(' ', '_') + '_' + uuid,
);
Expand Down
28 changes: 7 additions & 21 deletions apps/web/src/hooks/useStorage.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,27 @@
import { FormInstanceEntity, FormTemplateEntity } from '@web/client';
import { useEffect, useState } from 'react';
import { mockStorage } from './../services/storage.service';
import { storage } from './../services/storage.service';

// hook to fetch form blob from storage
export const useStorage = (
form: FormInstanceEntity | FormTemplateEntity | null,
) => {
const [formBlob, setFormBlob] = useState<Blob | null>(null);
const [mockBlob, setMockBlob] = useState<Blob | null>(null);

// This needs to be uncommented once storage is set up
// useEffect(() => {
// async function fetchFormBlob() {
// const blob = (await storage.downloadBlob(form?.formDocLink!)) as Blob;
// const arrayBuffer = await blob.arrayBuffer();
// setFormBlob(new Blob([arrayBuffer], { type: 'application/pdf' }));
// }

// async function fetchMockBlob() {}

// if (form) {
// fetchFormBlob();
// }
// }, [form]);

useEffect(() => {
async function fetchMockBlob() {
const blob = (await mockStorage.downloadBlob('mockBlobLink')) as Blob;
async function fetchFormBlob() {
const blob = (await storage.downloadBlob(form?.formDocLink!)) as Blob;
const arrayBuffer = await blob.arrayBuffer();
setMockBlob(new Blob([arrayBuffer], { type: 'application/pdf' }));
setFormBlob(new Blob([arrayBuffer], { type: 'application/pdf' }));
}

fetchMockBlob();
if (form) {
fetchFormBlob();
}
}, [form]);

return {
formBlob,
mockBlob,
};
};
24 changes: 1 addition & 23 deletions apps/web/src/services/storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,27 +25,5 @@ class BlobStorage {
}
}

class MockBlobStorage {
async uploadBlob(file: File, blobName: string) {
console.log('Uploading Blob', blobName);
}

async downloadBlob(blobLink: string) {
console.log('Downloading Blob', blobLink);

async function downloadBlob(blobLink: string) {
const response = await fetch(blobLink);
const blob = await response.blob();
return blob;
}

const blob = await downloadBlob(
'https://s29.q4cdn.com/175625835/files/doc_downloads/test.pdf',
);
return blob;
}
}

const storage = new BlobStorage(process.env.STORAGE_BLOB_URL as string);
const mockStorage = new MockBlobStorage();
export { storage, mockStorage };
export { storage };
Loading