Skip to content

Commit

Permalink
feat: upload wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ashup99 committed Jul 13, 2024
1 parent 520dc01 commit 5c6cfd8
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/upload/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const PORT = parseInt(process.env.PORT || '3000')
export const PORT = parseInt(process.env.PORT || '3002')
export const APP_ENV = process.env.APP_ENV
export const FILEBASE_ACCESS_KEY_ID = process.env.FILEBASE_ACCESS_KEY_ID
export const FILEBASE_SECRET_ACCESS_KEY = process.env.FILEBASE_SECRET_ACCESS_KEY
Expand Down
68 changes: 43 additions & 25 deletions packages/web/src/app/(protected)/doctor/documents/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ function Documents() {
const [selectedPatient, setSelectedPatient] = useState<PatientsListData>()
const [selectedPatientId, setSelectedPatientId] = useState<string>('')
const [errorMessage, setErrorMessage] = useState<string>('')
const [uploadLoading, setUploadLoading] = useState<boolean>(false)
let currentFileUploadData: File | null = null

const FETCH_PATIENTS = gql`
Expand All @@ -35,23 +36,21 @@ function Documents() {
}
`

const UPLOAD_FILE = gql`
mutation UploadFile($file: File!, $userId: ID!) {
uploadFile(file: $file, userId: $userId) {
user {
patientFiles {
createdAt
fileName
id
ipfsCid
updatedAt
}
}
}
const GENERATE_PRE_SIGNED_UPLOAD_URL = gql`
mutation GeneratePreSignedUploadUrl($userId: ID!) {
generatePreSignedUploadUrl(userId: $userId)
}
`
const [uploadRequest, { data: fileUploadData, loading: fileUploadLoading, error: fileUploadError }] =
useMutation(UPLOAD_FILE)
const [
generatePreSignedUploadUrlGql,
{
data: generatePreSignedUploadUrlData,
loading: generatePreSignedUploadUrlLoading,
error: generatePreSignedUploadUrlError
}
] = useMutation(GENERATE_PRE_SIGNED_UPLOAD_URL, {
context: { appTokenName: APP_NAME }
})

const { data: patientsListData, error: patientsListDataError } = useQuery(FETCH_PATIENTS, {
context: { appTokenName: APP_NAME }
Expand Down Expand Up @@ -113,11 +112,7 @@ function Documents() {
/>
</div>
<div className="grid mt-7">
<button
type="submit"
disabled={fileUploadLoading}
className="btn px-6 btn-sm normal-case btn-primary"
>
<button type="submit" disabled={uploadLoading} className="btn px-6 btn-sm normal-case btn-primary">
Upload File
</button>
{errorMessage && <ErrorText styleClass="mt-8">{errorMessage}</ErrorText>}
Expand All @@ -132,15 +127,38 @@ function Documents() {

const uploadFileDocument = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
setUploadLoading(true)
console.log('uploadFileDocument Hello world')
console.log({ currentFileUploadData })
console.log({ currentFileUploadData, selectedPatientId })
if (!currentFileUploadData) return
// try {
await uploadRequest({
variables: { file: currentFileUploadData, userId: selectedPatientId }
await generatePreSignedUploadUrlGql({
variables: { userId: selectedPatientId }
})
.then((res) => {
console.log({ res, fileUploadData })
.then(async (res) => {
const uploadUrlApi = res?.data?.generatePreSignedUploadUrl
if (uploadUrlApi) {
// let formData = new FormData()
// currentFileUploadData?.arrayBuffer().then((arrayBufferData)=>{
// const blobData=new Blob()
// })
// formData.append('file', currentFileUploadData)
// formData.append('password', 'John123')

await fetch(uploadUrlApi, {
body: currentFileUploadData,
method: 'POST'
})
.then((uploadRes) => {
console.log({ uploadRes })
})
.catch((uploadError) => {
console.log({ uploadError })
})
} else {
setErrorMessage('Upload URL is not valid')
}
console.log({ res, generatePreSignedUploadUrlData })
})
.catch((error) => {
console.error({ error })
Expand Down

0 comments on commit 5c6cfd8

Please sign in to comment.