Fixes and error handling
This commit is contained in:
parent
c3dfc8e2c6
commit
0ad197ca6f
|
@ -4,3 +4,5 @@ node_modules
|
|||
/build
|
||||
/public/build
|
||||
.env
|
||||
|
||||
prisma/dev.db
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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!`
|
||||
|
|
BIN
prisma/dev.db
BIN
prisma/dev.db
Binary file not shown.
Binary file not shown.
Before Width: | Height: | Size: 17 KiB |
Loading…
Reference in New Issue