- update to use onCall from firebase functions

This commit is contained in:
Amruth Pillai
2020-07-13 22:13:00 +05:30
parent f17471b94d
commit e9bc40afb5
8 changed files with 86 additions and 75 deletions

View File

@ -1,9 +1,11 @@
import firebase from 'gatsby-plugin-firebase';
import { clone } from 'lodash';
import React, { memo, useContext, useEffect, useState } from 'react';
import { FaPrint } from 'react-icons/fa';
import Button from '../../components/shared/Button';
import ModalContext from '../../contexts/ModalContext';
import { useSelector } from '../../contexts/ResumeContext';
import { b64toBlob } from '../../utils';
import BaseModal from '../BaseModal';
const ExportModal = () => {
@ -11,7 +13,6 @@ const ExportModal = () => {
const [open, setOpen] = useState(false);
const [isLoadingSingle, setLoadingSingle] = useState(false);
const [isLoadingMulti, setLoadingMulti] = useState(false);
const functionsUrl = 'https://us-central1-rx-resume.cloudfunctions.net';
const { emitter, events } = useContext(ModalContext);
@ -37,20 +38,22 @@ const ExportModal = () => {
const handleSinglePageDownload = async () => {
setLoadingSingle(true);
fetch(`${functionsUrl}/printSinglePageResume?id=${state.id}`, {
method: 'POST',
})
.then((response) => response.blob())
.then(openFile);
const printSinglePageResume = firebase
.functions()
.httpsCallable('printSinglePageResume');
const { data } = await printSinglePageResume({ id: state.id });
const blob = b64toBlob(data, 'application/pdf');
openFile(blob);
};
const handleMultiPageDownload = async () => {
setLoadingMulti(true);
fetch(`${functionsUrl}/printMultiPageResume?id=${state.id}`, {
method: 'POST',
})
.then((response) => response.blob())
.then(openFile);
const printMultiPageResume = firebase
.functions()
.httpsCallable('printMultiPageResume');
const { data } = await printMultiPageResume({ id: state.id });
const blob = b64toBlob(data, 'application/pdf');
openFile(blob);
};
const handleExportToJson = () => {

View File

@ -69,3 +69,23 @@ export const move = (
return result;
};
export const b64toBlob = (b64Data, contentType = '', sliceSize = 512) => {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, { type: contentType });
return blob;
};