Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: edit channel edge cases #640

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib/components/Channel/Chat/DrawerEditChannel.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
bind:this={fileuploader}
on:change={fileupload}
type="file"
accept="image/png, image/jpeg"
name="thumbnail"
class="file-input file-input-bordered file-input-primary w-full mt-5" />
<input
Expand Down
2 changes: 2 additions & 0 deletions src/lib/components/Profile/DrawerEditProfile.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
</label>
<input
type="file"
accept="image/png, image/jpeg"
name="banner"
id="banner"
class="file-input file-input-bordered file-input-primary w-full" />
Expand All @@ -136,6 +137,7 @@
</label>
<input
type="file"
accept="image/png, image/jpeg"
name="avatar"
id="avatar"
class="file-input file-input-bordered file-input-primary w-full" />
Expand Down
40 changes: 6 additions & 34 deletions src/routes/channel/[channelId]/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { patch, putImage } from '$lib/api'
import { putImage } from '$lib/api'
import type { Actions } from './$types'

const dataURLtoFile = (dataurl: string, filename: string) => {
Expand All @@ -16,10 +16,6 @@ const dataURLtoFile = (dataurl: string, filename: string) => {
export const actions = {
'edit-channel': async ({ request, locals }) => {
const data: FormData = await request.formData()
const newChannel = {}
addPropertyIfDefined(data, 'description', newChannel)
addPropertyIfDefined(data, 'title', newChannel)
addPropertyIfDefined(data, 'category', newChannel)
const thumbnail = data.get('thumbnail') as File
const imageSrc = data.get('imageSrc') as string
const channelId = data.get('channelId') as string
Expand All @@ -28,36 +24,12 @@ export const actions = {
? thumbnail
: dataURLtoFile(imageSrc, 'thumbnail-image')
console.log(file)
if (file !== null && file.size > 0) {
const urlLocation = await putImage(
`channels/thumbnail?channelId=${channelId}&bucketName=thumbnails&originalName=${channelId}-thumbnail`,
file,
{
userId: locals.user.userId,
token: locals.user.token
}
)
console.log(urlLocation)
if (file !== null && file.size > 0 && file.type !== '') {
await putImage(`channels/thumbnail?channelId=${channelId}&bucketName=thumbnails`, file, {
userId: locals.user.userId,
token: locals.user.token
})
}

const updatedChannel = await patch(`channels?channelId=${channelId}`, newChannel, {
userId: locals.user.userId,
token: locals.user.token
})

console.log(updatedChannel)

return { success: true }
}
} satisfies Actions

const addPropertyIfDefined = (
data: FormData,
property: string,
newChannel: { [key: string]: unknown }
) => {
const propertyValue = data.get(property)
if (propertyValue !== null && propertyValue !== undefined) {
newChannel[property] = propertyValue
}
}
4 changes: 2 additions & 2 deletions src/routes/profile/[username]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const actions = {

if (data.get('avatar') !== null && avatar.size > 0) {
const urlLocation = await putImage(
`users/current/avatar?bucketName=avatars&originalName=${locals.user.userId}-avatar`,
`users/current/avatar?bucketName=avatars`,
data.get('avatar'),
{
userId: locals.user.userId,
Expand All @@ -43,7 +43,7 @@ export const actions = {

if (data.get('banner') !== null && banner.size > 0) {
const urlLocation = await putImage(
`users/current/banner?bucketName=banners&originalName=${locals.user.userId}-banner`,
`users/current/banner?bucketName=banners`,
data.get('banner'),
{
userId: locals.user.userId,
Expand Down