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

[#327] Implement CommentContext #347

Merged
merged 1 commit into from
May 4, 2024
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
100 changes: 100 additions & 0 deletions ui/src/context/comment/comment-context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
'use client';

import type { Dispatch, ReactNode, SetStateAction } from 'react';
import { createContext, useContext, useRef, useState } from 'react';

import { useToast } from '@/context/common/toast-context';

import type { Comment } from '@/lib/definitions/comment';
import { validateCommentFields } from '@/lib/utils/comment/validate-comment-fields';

interface ContextShape {
prepopulated?: Comment;
content: string;
setContent: Dispatch<SetStateAction<string>>;
contentRef: React.RefObject<HTMLTextAreaElement>;
movieName: string;
setMovieName: Dispatch<SetStateAction<string>>;
movieNameRef: React.RefObject<HTMLTextAreaElement>;
disabled: boolean;
setDisabled: Dispatch<SetStateAction<boolean>>;
validateAndGetData: () =>
| {
content: string;
movieName: string;
}
| undefined;
isEditing: boolean;
setIsEditing: Dispatch<SetStateAction<boolean>>;
}

const CommentCotext = createContext<ContextShape | null>(null);

export function CommentProvider({
children,
prepopulated,
}: {
children: ReactNode;
prepopulated?: Comment;
}) {
const [movieName, setMovieName] = useState(prepopulated?.movieName ?? '');
const [content, setContent] = useState(prepopulated?.content ?? '');

const [disabled, setDisabled] = useState(false);

const movieNameRef = useRef<HTMLTextAreaElement>(null);
const contentRef = useRef<HTMLTextAreaElement>(null);

const { emitToast } = useToast();

const validateAndGetData = () => {
const validatedFields = validateCommentFields({ movieName, content });

if (!validatedFields.success) {
if (validatedFields.errors.movieName) {
movieNameRef.current?.focus();
emitToast(validatedFields.errors.movieName, 'error');
} else if (validatedFields.errors.content) {
contentRef.current?.focus();
emitToast(validatedFields.errors.content, 'error');
}

setDisabled(false);
return;
}

return validatedFields.data;
};

const [isEditing, setIsEditing] = useState(prepopulated === undefined);

return (
<CommentCotext.Provider
value={{
prepopulated,
content,
setContent,
contentRef,
movieName,
setMovieName,
movieNameRef,
disabled,
setDisabled,
validateAndGetData,
isEditing,
setIsEditing,
}}
>
{children}
</CommentCotext.Provider>
);
}

export function useComment() {
const context = useContext(CommentCotext);
if (!context) {
throw new Error('useComment must be used within an CommentProvider');
}

return context;
}
2 changes: 1 addition & 1 deletion ui/src/lib/definitions/comment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Pagination } from '@/lib/definitions/common';

interface Comment {
export interface Comment {
id: number; //123,
userId: string; //"{UUID v4}",
nickname: string; // "신비로운 평론가 붉은 여우",
Expand Down
49 changes: 49 additions & 0 deletions ui/src/lib/utils/comment/validate-comment-fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
interface RawCommentField {
movieName: string;
content: string;
}

interface OnSuccess {
success: true;
data: {
movieName: string;
content: string;
};
}

interface OnFail {
success: false;
errors: Partial<{
movieName: string;
content: string;
}>;
}

export function validateCommentFields(rawData: RawCommentField): OnSuccess | OnFail {
const errors = new Map<string, string>();

const { movieName, content } = rawData;

if (movieName.length < 1) {
errors.set('movieName', '영화제목을 입력해주세요.');
}

if (content.length < 1) {
errors.set('content', '코멘트를 입력해주세요.');
}

if (errors.size > 0) {
return {
success: false,
errors: Object.fromEntries(errors),
};
}

return {
success: true,
data: {
movieName,
content,
},
};
}