Skip to content

Commit aa8e4c8

Browse files
committed
fix: uploading images saves to db
1 parent c5fed6f commit aa8e4c8

File tree

2 files changed

+16
-55
lines changed

2 files changed

+16
-55
lines changed

web/src/app/publishers/page.tsx

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,14 @@ const buildSubmitPayload = (
5959
bio: string
6060
avatar_url: string
6161
},
62-
selectedOrgId: string | null | undefined,
63-
avatarUrl: string | null
62+
selectedOrgId: string | null | undefined
6463
) => {
6564
return {
6665
id: formData.id,
6766
name: formData.name,
6867
email: formData.email || undefined,
6968
bio: formData.bio || undefined,
70-
avatar_url: avatarUrl || undefined,
69+
avatar_url: formData.avatar_url || undefined,
7170
org_id: selectedOrgId && selectedOrgId !== '' ? selectedOrgId : undefined,
7271
}
7372
}
@@ -92,8 +91,7 @@ const CreatePublisherPage = () => {
9291
bio: '',
9392
avatar_url: '',
9493
})
95-
const [avatarFile, setAvatarFile] = useState<File | null>(null)
96-
const [isUploadingAvatar, setIsUploadingAvatar] = useState(false)
94+
9795
const [errors, setErrors] = useState<Record<string, string>>({})
9896
const [isIdManuallyEdited, setIsIdManuallyEdited] = useState(false)
9997
const [hasRemovedAvatar, setHasRemovedAvatar] = useState(false)
@@ -188,50 +186,16 @@ const CreatePublisherPage = () => {
188186
}
189187

190188
const handleAvatarChange = async (file: File | null, url: string) => {
191-
setAvatarFile(file)
192189
setFormData((prev) => ({ ...prev, avatar_url: url }))
193190

194191
// Track if user explicitly removed the avatar
195-
if (!file && !url) {
192+
if (!url) {
196193
setHasRemovedAvatar(true)
197194
} else {
198195
setHasRemovedAvatar(false)
199196
}
200197
}
201198

202-
const uploadAvatar = async (): Promise<string | null> => {
203-
if (!avatarFile) return formData.avatar_url || null
204-
205-
setIsUploadingAvatar(true)
206-
try {
207-
const formData = new FormData()
208-
formData.append('avatar', avatarFile)
209-
210-
const response = await fetch('/api/upload/avatar', {
211-
method: 'POST',
212-
body: formData,
213-
})
214-
215-
if (!response.ok) {
216-
const error = await response.json()
217-
throw new Error(error.error || 'Failed to upload avatar')
218-
}
219-
220-
const { avatar_url } = await response.json()
221-
return avatar_url
222-
} catch (error) {
223-
toast({
224-
title: 'Error',
225-
description:
226-
error instanceof Error ? error.message : 'Failed to upload avatar',
227-
variant: 'destructive',
228-
})
229-
return null
230-
} finally {
231-
setIsUploadingAvatar(false)
232-
}
233-
}
234-
235199
const handleSubmit = async () => {
236200
const nameError = validatePublisherName(formData.name) || ''
237201
const idError = validatePublisherId(formData.id) || ''
@@ -255,17 +219,12 @@ const CreatePublisherPage = () => {
255219

256220
setIsLoading(true)
257221
try {
258-
// Upload avatar first if there's a new file
259-
const avatarUrl = await uploadAvatar()
260-
261222
const response = await fetch('/api/publishers', {
262223
method: 'POST',
263224
headers: {
264225
'Content-Type': 'application/json',
265226
},
266-
body: JSON.stringify(
267-
buildSubmitPayload(formData, selectedOrgId, avatarUrl)
268-
),
227+
body: JSON.stringify(buildSubmitPayload(formData, selectedOrgId)),
269228
})
270229

271230
if (!response.ok) {
@@ -450,7 +409,7 @@ const CreatePublisherPage = () => {
450409
onBioChange={(e) => setFormData({ ...formData, bio: e.target.value })}
451410
onAvatarChange={handleAvatarChange}
452411
isLoading={isLoading}
453-
isUploadingAvatar={isUploadingAvatar}
412+
isUploadingAvatar={false}
454413
/>
455414
),
456415
isValid: () => {
@@ -462,7 +421,6 @@ const CreatePublisherPage = () => {
462421

463422
return (
464423
!isLoading &&
465-
!isUploadingAvatar &&
466424
!hasValidationErrors &&
467425
!hasInvalidId &&
468426
!hasOrgError &&
@@ -542,12 +500,10 @@ const CreatePublisherPage = () => {
542500
onClick={handleSubmit}
543501
disabled={!currentStepData.isValid()}
544502
>
545-
{(isLoading || isUploadingAvatar) && (
503+
{isLoading && (
546504
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
547505
)}
548-
{isUploadingAvatar
549-
? 'Uploading...'
550-
: 'Create Publisher Profile'}
506+
Create Publisher Profile
551507
</Button>
552508
)}
553509
</div>

web/src/components/ui/avatar-upload.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@ export function AvatarUpload({
3535
return
3636
}
3737

38-
const url = URL.createObjectURL(file)
39-
setPreviewUrl(url)
40-
onChange(file, url)
38+
// Convert file to data URL
39+
const reader = new FileReader()
40+
reader.onload = (e) => {
41+
const dataUrl = e.target?.result as string
42+
setPreviewUrl(dataUrl)
43+
onChange(null, dataUrl) // Pass null for file since we're using data URL
44+
}
45+
reader.readAsDataURL(file)
4146
},
4247
[onChange]
4348
)

0 commit comments

Comments
 (0)