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 review comment to use safeParse and add TODO comment. #91

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
3 changes: 1 addition & 2 deletions one-pager-maker/src/RouteAuthGuard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const RouteAuthGuard = (props: Props) => {
const location = useLocation()
const { data: result, status: findStatus, error } = userApi.useFindUserByUIDQuery(userState?.data.user?.uid ?? "");

console.log("Auth!")
if (userState.status === 'pending') {
return (
<main className={'w-screen h-screen flex flex-col justify-center items-center'}>
Expand Down Expand Up @@ -44,7 +43,7 @@ export const RouteAuthGuard = (props: Props) => {
</main>
)
}
console.log(result)

if (findStatus === 'success' && !result) {
// Login success but not found user ID in database
return <SetId />
Expand Down
13 changes: 5 additions & 8 deletions one-pager-maker/src/pages/SetId.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
} from '@mui/material';
import { useNavigate } from 'react-router-dom';
import { userApi } from "../api/userApi.ts";
import { assertZodSchema } from "../utils/asserts.ts";
import { idSchema } from '../entity/user/userType.ts';
import { useAppSelector } from '../redux/hooks.ts';

Expand All @@ -31,9 +30,9 @@ const SetId = () => {
const updateUser = useCallback(() => {

// user id validation
try {
assertZodSchema(idSchema, formData.userId)
} catch {
const result = idSchema.safeParse(formData.userId);

if (!result.success) {
alert('User ID is allowed only alphanumeric characters, underscores (_), and hyphens (-).');
return
}
Expand All @@ -42,10 +41,6 @@ const SetId = () => {
alert('User ID is already registerd.');
return
}
if (result.error) {
alert('Server Internal Error. Please retry.');
return
}

// Update user
const user = {
Expand All @@ -66,6 +61,7 @@ const SetId = () => {
</div>
<Box >
<div className={"flex flex-col w-96 gap-5 pb-3"}>
{/* TODO: improve UX https://github.com/Greek-Academy/one-pager-maker/pull/75#discussion_r1614507584 */}
<TextField
required
id="userId"
Expand All @@ -75,6 +71,7 @@ const SetId = () => {
value={formData.userId}
onChange={handleFormData}
/>
{/* TODO: improve UX https://github.com/Greek-Academy/one-pager-maker/pull/75#discussion_r1614511182 */}
<Button
className={"normal-case h-12"}
variant="contained"
Expand Down
11 changes: 4 additions & 7 deletions one-pager-maker/src/pages/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { createUserWithEmailAndPassword, signInWithEmailAndPassword } from 'fire
import { auth } from '../firebase';
import { useNavigate } from 'react-router-dom';
import { userApi } from "../api/userApi.ts";
import { assertZodSchema } from "../utils/asserts.ts";
import { idSchema } from '../entity/user/userType.ts';

const SignUp = () => {
Expand All @@ -30,21 +29,19 @@ const SignUp = () => {
const createUser = useCallback(() => {

// user id validation
try {
assertZodSchema(idSchema, formData.userId)
} catch {
const result = idSchema.safeParse(formData.userId);

if (!result.success) {
alert(`User ID is allowed only alphanumeric characters, underscores (_), and hyphens (-).`);
return
}

if (result.data) {
alert(`User ID "${formData.userId}" is already registerd.`);
return
} else if (result.error) {
alert(`Server Internal Error. Please retry.`);
return
}

// TODO: similar code. refactor!
// Regist user
createUserWithEmailAndPassword(
auth,
Expand Down