Skip to content

Commit

Permalink
refactor: add cloudinary fxn to partners (#3393)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilyjablonski committed Apr 12, 2023
1 parent ed11180 commit 03cb224
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion sites/partners/src/lib/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { SetStateAction } from "react"
import { t, CloudinaryUpload, TimeFieldPeriod } from "@bloom-housing/ui-components"
import { t, TimeFieldPeriod } from "@bloom-housing/ui-components"
import { cloudinaryUrlFromId } from "@bloom-housing/shared-helpers"
import { CloudinaryUpload } from "./listings/CloudinaryUpload"
import dayjs from "dayjs"
import utc from "dayjs/plugin/utc"
dayjs.extend(utc)
Expand Down
50 changes: 50 additions & 0 deletions sites/partners/src/lib/listings/CloudinaryUpload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import axios from "axios"

interface CloudinaryUploadProps {
file: File
onUploadProgress: (progress: number) => void
cloudName: string
uploadPreset: string
tag?: string
signature?: string
apiKey?: string
timestamp?: number
}

export const CloudinaryUpload = async ({
file,
onUploadProgress,
cloudName,
uploadPreset,
signature,
apiKey,
timestamp,
tag = "browser_upload",
}: CloudinaryUploadProps) => {
const url = `https://api.cloudinary.com/v1_1/${cloudName}/upload`
const data = new FormData()
data.append("upload_preset", uploadPreset)
data.append("tags", tag)
data.append("file", file)
if (signature && timestamp && apiKey) {
data.append("signature", signature)
data.append("timestamp", `${timestamp}`)
data.append("api_key", apiKey)
}

if (!cloudName || cloudName == "" || !uploadPreset || uploadPreset == "") {
const err = "Please supply a cloud name and upload preset for Cloudinary"
alert(err)
throw err
}

const response = await axios.request({
method: "post",
url: url,
data: data,
onUploadProgress: (p) => {
onUploadProgress(parseInt(((p.loaded / p.total) * 100).toFixed(0), 10))
},
})
return response
}

0 comments on commit 03cb224

Please sign in to comment.