Fixes and error handling

This commit is contained in:
Joonas 2023-11-09 13:56:09 +02:00
parent c3dfc8e2c6
commit 0ad197ca6f
6 changed files with 23 additions and 9 deletions

2
.gitignore vendored
View File

@ -4,3 +4,5 @@ node_modules
/build
/public/build
.env
prisma/dev.db

View File

@ -30,12 +30,20 @@ export async function getPostById(id: number) {
return post;
}
export async function createPost(body: Post["text"], id: number) {
const errors = {};
function validatePostBody(body: string) {
if (!body) {
errors.body = "Post body can't be empty";
return "Post body can't be empty";
}
}
export async function createPost(body: Post["text"], id: number) {
const errors: {
body: string | undefined;
all: string | undefined;
} = {
body: validatePostBody(body),
all: undefined,
};
if (Object.values(errors).some(Boolean)) {
return errors;

View File

@ -78,7 +78,7 @@ export async function editUser(data: object, id: number) {
id: id,
},
data: {
name: data.username,
username: data.username,
},
});
@ -86,7 +86,7 @@ export async function editUser(data: object, id: number) {
errors.all = "Something went wrong";
}
return errors;
return { username: username.username, errors: errors };
case "name":
if (!data.name) {
errors.name = "Please enter a valid name";
@ -109,7 +109,7 @@ export async function editUser(data: object, id: number) {
errors.all = "Something went wrong";
}
return errors;
return { errors };
default:
break;
}

View File

@ -9,7 +9,7 @@ import { Button } from "~/components/Button";
import { Card } from "~/components/Card";
import { FormInput, FormLabel } from "~/components/Form";
import { SubTitle, Title, Text } from "~/components/Typography";
import { editUser } from "~/models/user.server";
import { editUser, getUserByName } from "~/models/user.server";
import { commitSession, getSession } from "~/utils/session.server";
export async function action({ request }: ActionFunction) {
@ -22,12 +22,16 @@ export async function action({ request }: ActionFunction) {
.formData()
.then((data) => Object.fromEntries(data));
const errors = await editUser(formData, session.get("userId"));
const { username, errors } = await editUser(formData, session.get("userId"));
if (Object.values(errors).some(Boolean)) {
return errors;
}
if (username) {
session.set("username", username);
}
session.flash(
"globalMessage",
`Field ${formData.intent} changed successfully!`

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 17 KiB