bugfixing

This commit is contained in:
MuslemRahimi 2024-06-05 12:37:44 +02:00
parent 226d21c6e7
commit a4526a7caa
4 changed files with 44 additions and 30 deletions

View File

@ -31,7 +31,6 @@
if (['.mp4', '.webm'].some(format => files[0]?.name?.includes(format)) ) if (['.mp4', '.webm'].some(format => files[0]?.name?.includes(format)) )
{ {
videoInput = URL.createObjectURL(files[0]); videoInput = URL.createObjectURL(files[0]);
console.log(videoInput)
} }
else { else {
@ -127,10 +126,11 @@ function handleDrop(e) {
const handleCancel= () => { const handleCancel= () => {
inputValue = ''; inputValue = '';
showVideo = false; showVideo = false;
} }
let isHovering = false;
let isHovering = false;
$: { $: {
@ -175,10 +175,10 @@ $: {
<span class="mt-2 mb-4 text-gray-200">Drop here to upload</span> <span class="mt-2 mb-4 text-gray-200">Drop here to upload</span>
{/if} {/if}
<span class="text-xs sm:text-sm text-gray-400 m-auto mb-5 "> <span class="text-xs sm:text-sm text-gray-400 m-auto mb-5 ">
We support jpg/jpeg, png, webp, mp4 and gif. We support jpg/jpeg, png, webp and mp4.
</span> </span>
<span class="text-xs sm:text-sm text-gray-400 m-auto mb-5 "> <span class="text-xs sm:text-sm text-gray-400 m-auto mb-5 ">
File must be smaller than 10MB. File must be smaller than 5MB.
</span> </span>
<label for={id} class="cursor-pointer rounded-full bg-blue-700 text-sm text-white font-bold w-auto p-3"> <label for={id} class="cursor-pointer rounded-full bg-blue-700 text-sm text-white font-bold w-auto p-3">
Choose File Choose File
@ -193,7 +193,7 @@ $: {
{id} {id}
name={id} name={id}
value={inputValue} value={inputValue}
accept="image/gif" accept="image"
on:change={showPreview} on:change={showPreview}
on:input={handleInput} on:input={handleInput}
autocomplete="off" autocomplete="off"

View File

@ -84,18 +84,15 @@ export const createPostImageSchema = z.object({
.trim(), .trim(),
tagTopic: z.string(), tagTopic: z.string(),
tagline: z.string(), tagline: z.string(),
atLeastOneTag: z atLeastOneTag: z.string({ required_error: 'At least 1 tag is required' }).min(1, { message: 'At least 1 tag is required' }),
.string({ required_error: 'At least 1 tag is required' })
.min(1, { message: 'At least 1 tag is required' }),
thumbnail: z thumbnail: z
.instanceof(File) .instanceof(File)
.superRefine((val, ctx) => { .superRefine((val, ctx) => {
if (val) { if (val) {
if (val.size > 10242880) { if (val.size > 5121440) {
ctx.addIssue({ ctx.addIssue({
code: z.ZodIssueCode.custom, code: z.ZodIssueCode.custom,
message: 'File must be less than 10MB', message: 'File must be less than 5MB',
}); });
} }
@ -107,12 +104,11 @@ export const createPostImageSchema = z.object({
ctx.addIssue({ ctx.addIssue({
code: z.ZodIssueCode.custom, code: z.ZodIssueCode.custom,
message: message:
'Unsupported file type. Supported formats: jpeg, jpg, png, webp, svg, gif', 'Unsupported file type. Supported formats: jpeg, jpg, png, webp, svg',
}); });
} }
} }
}), }),
postType: z.string({ required_error: 'PostType is required.' }), postType: z.string({ required_error: 'PostType is required.' }),
user: z.string({ required_error: 'User is required.' }), user: z.string({ required_error: 'User is required.' }),
}); });

View File

@ -83,15 +83,16 @@ export const load = async ({ locals}) => {
export const actions = { export const actions = {
createPostText: async ({ request, locals }) => { createPostText: async ({ request, locals }) => {
let newPost = '';
const body = await request.formData(); const body = await request.formData();
const thumb = body.get('thumbnail');
body.delete('thumbnail'); body.delete('thumbnail');
body.append('user', locals?.user?.id); body.append('user', locals?.user?.id);
let {formData, errors} = await validateData( body, createPostTextSchema); let {formData, errors} = await validateData( body, createPostTextSchema);
formData.description = addClassesToHtml(marked(formData?.description)) formData.description = addClassesToHtml(marked(formData?.description))
console.log(formData)
formData.tagTopic = JSON.parse(formData.tagTopic)[0] formData.tagTopic = JSON.parse(formData.tagTopic)[0]
formData.upvote = 1 formData.upvote = 1
@ -114,7 +115,7 @@ export const actions = {
try { try {
let newPost = await locals.pb.collection('posts').create(serialize(formData)); newPost = await locals.pb.collection('posts').create(serialize(formData));
// add the tagTopic manually because serialize does not work on arrays // add the tagTopic manually because serialize does not work on arrays
@ -126,23 +127,31 @@ export const actions = {
//FormData for alreadyVoted //FormData for alreadyVoted
let formDataAlreadyVoted = new FormData(); let formDataAlreadyVoted = new FormData();
formDataAlreadyVoted.append('post', newPost.id); formDataAlreadyVoted.append('post', newPost?.id);
formDataAlreadyVoted.append('user', newPost.user); formDataAlreadyVoted.append('user', newPost?.user);
formDataAlreadyVoted.append('type', 'upvote'); formDataAlreadyVoted.append('type', 'upvote');
//console.log(formDataAlreadyVoted) //console.log(formDataAlreadyVoted)
await locals.pb.collection('alreadyVoted').create(formDataAlreadyVoted); await locals.pb.collection('alreadyVoted').create(formDataAlreadyVoted);
} catch (err) { } catch (err) {
console.log('Error: ', err); console.log('Error: ', err);
error(err.status, err.message); error(err.status, err.message);
} }
redirect(303, '/community'); if(newPost?.id?.length !== 0) {
redirect(303, '/community/post/'+newPost?.id);
} else {
redirect(303, '/community');
}
}, },
createPostImage: async ({ request, locals }) => { createPostImage: async ({ request, locals }) => {
let newPost = '';
const body = await request.formData(); const body = await request.formData();
const thumb = body.get('thumbnail'); const thumb = body.get('thumbnail');
@ -150,8 +159,7 @@ export const actions = {
body.delete('thumbnail'); body.delete('thumbnail');
} }
body.append('user', locals?.user?.id); body.append('user', locals?.user?.id);
let {formData, errors} = await validateData( body, createPostImageSchema); let {formData, errors} = await validateData( body, createPostImageSchema);
@ -204,7 +212,6 @@ export const actions = {
type: image.type, type: image.type,
lastModified: image.lastModified, lastModified: image.lastModified,
}); });
} catch (err) { } catch (err) {
@ -218,7 +225,7 @@ export const actions = {
try { try {
let newPost = await locals.pb.collection('posts').create(serialize(formData)); newPost = await locals.pb.collection('posts').create(serialize(formData));
// add the tagTopic manually because serialize does not work on arrays // add the tagTopic manually because serialize does not work on arrays
@ -242,12 +249,19 @@ export const actions = {
error(err.status, err.message); error(err.status, err.message);
} }
redirect(303, '/community');
if(newPost?.id?.length !== 0) {
redirect(303, '/community/post/'+newPost?.id);
} else {
redirect(303, '/community');
}
}, },
createPostLink: async ({ request, locals }) => { createPostLink: async ({ request, locals }) => {
let newPost = '';
const body = await request.formData(); const body = await request.formData();
const url = body.get('link') const url = body.get('link')
let image; let image;
@ -295,7 +309,7 @@ export const actions = {
catch(e) catch(e)
{ {
//console.log(e) console.log(e)
} }
@ -344,7 +358,7 @@ export const actions = {
}) })
try { try {
let newPost = await locals.pb.collection('posts').create(serialize(formData)); newPost = await locals.pb.collection('posts').create(serialize(formData));
// add the tagTopic manually because serialize does not work on arrays // add the tagTopic manually because serialize does not work on arrays
@ -367,7 +381,11 @@ export const actions = {
error(err.status, err.message); error(err.status, err.message);
} }
redirect(303, '/community'); if(newPost?.id?.length !== 0) {
redirect(303, '/community/post/'+newPost?.id);
} else {
redirect(303, '/community');
}
}, },

View File

@ -336,7 +336,7 @@
let previousPage : string = base || '/community/'; let previousPage : string = base || '/community/';
afterNavigate(({from}) => { afterNavigate(({from}) => {
previousPage = from?.url.pathname || '/community/' previousPage = from?.url.pathname?.startsWith('/community/create-post') ? '/community' : ( from?.url.pathname || '/community')
}) })