Skip to content

Commit

Permalink
feat: storage thru idb for AttachmentField
Browse files Browse the repository at this point in the history
  • Loading branch information
KenLSM committed Mar 7, 2023
1 parent 6645128 commit 9c92d23
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
37 changes: 36 additions & 1 deletion frontend/src/components/Field/Attachment/Attachment.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useMemo } from 'react'
import { useCallback, useEffect, useMemo, useRef } from 'react'
import { DropzoneProps, useDropzone } from 'react-dropzone'
import {
Box,
Expand All @@ -11,6 +11,7 @@ import {
useMultiStyleConfig,
} from '@chakra-ui/react'
import imageCompression from 'browser-image-compression'
import { DBSchema, openDB } from 'idb'
import omit from 'lodash/omit'
import simplur from 'simplur'

Expand All @@ -27,6 +28,7 @@ import {
getReadableFileSize,
} from './utils'

const STORE_NAME = '__attachment'
const IMAGE_UPLOAD_TYPES_TO_COMPRESS = ['image/jpeg', 'image/png']

export interface AttachmentProps extends UseFormControlProps<HTMLElement> {
Expand Down Expand Up @@ -69,6 +71,35 @@ export interface AttachmentProps extends UseFormControlProps<HTMLElement> {
colorScheme?: ThemeColorScheme
}

interface MyDB extends DBSchema {
[STORE_NAME]: { key: string; value: File }
}

const dbPromise = openDB<MyDB>('formsg1', 2, {
upgrade(db) {
db.createObjectStore(STORE_NAME)
console.log('db upgraded')
},
})

const foo = async (onChange: any) => {
const idb = await dbPromise
idb
.getAllKeys(STORE_NAME)
.catch(console.error)
.then(async (d) => {
if (d && d[0]) {
const dataum = await idb.get(STORE_NAME, d[0])
onChange(dataum)
}
})
}

const storeIntoIdb = async (file: File) => {
const idb = await dbPromise
idb.put(STORE_NAME, file, file.name)
}

export const Attachment = forwardRef<AttachmentProps, 'div'>(
(
{
Expand All @@ -85,6 +116,9 @@ export const Attachment = forwardRef<AttachmentProps, 'div'>(
},
ref,
) => {
useEffect(() => {
foo(onChange)
}, [])
// Merge given props with any form control props, if they exist.
const inputProps = useFormControl(props)
// id to set on the rendered max size FormFieldMessage component.
Expand Down Expand Up @@ -165,6 +199,7 @@ export const Attachment = forwardRef<AttachmentProps, 'div'>(
}

onChange(acceptedFile)
storeIntoIdb(acceptedFile)
},
[accept, maxSize, onChange, onError],
)
Expand Down
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@
"html-escaper": "^3.0.3",
"http-errors": "^2.0.0",
"http-status-codes": "^2.2.0",
"idb": "^7.1.1",
"intl-tel-input": "~12.4.0",
"jose": "^4.11.0",
"json-stringify-safe": "^5.0.1",
Expand Down

0 comments on commit 9c92d23

Please sign in to comment.