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

feat: sign in page frontend #121

Merged
merged 3 commits into from
Nov 28, 2024
Merged

Conversation

an920107
Copy link
Contributor

@an920107 an920107 commented Nov 2, 2024

Description

  • Complete the frontend of the sign in page

Summary by CodeRabbit

Summary by CodeRabbit

  • New Features

    • Enhanced login page with a "remember me" checkbox and improved error handling.
    • Implemented visually appealing input fields styled with Tailwind CSS.
    • Added localized labels for better maintainability and readability.
  • Bug Fixes

    • Improved error feedback for incorrect login credentials.

Screenshot Preview

login

Copy link
Contributor

coderabbitai bot commented Nov 2, 2024

Walkthrough

The changes in the pull request focus on the src/routes/login.tsx file, enhancing the login page's functionality and user interface. New state variables for managing a "remember me" checkbox and error display have been introduced. The login function now prevents default form submission and clears the password field on error. The structure of the login form has been updated to a more organized layout with Tailwind CSS styling, and a new Labels class has been added for localized strings, improving code maintainability.

Changes

File Change Summary
src/routes/login.tsx - Added state variable isRemember for "remember me" functionality.
- Added state variable isError for handling login errors.
- Updated login method to accept an event parameter and prevent default form submission.
- Reorganized the login form structure and applied Tailwind CSS for styling.
- Introduced a new Labels class for managing localized strings in the UI.

Poem

🐰 In the land of code, where bunnies play,
A login page blooms, brightening the day.
With "remember me" checked, and errors in sight,
We hop through the forms, making things right.
Tailwind’s soft touch, a sight to behold,
In the world of login, our stories unfold! 🌟


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (2)
src/routes/login.tsx (2)

Line range hint 21-33: Consider enhancing error handling and user experience

While the basic error handling is implemented, consider these improvements:

  1. Add a loading state during authentication
  2. Handle network errors separately from authentication errors
  3. Add input validation before submission

Here's a suggested implementation:

+ const [isLoading, setIsLoading] = useState(false);
  async function login(event: React.FormEvent<HTMLFormElement>) {
    event.preventDefault();
+   if (!email || !password) {
+     setIsError(true);
+     return;
+   }
+   setIsLoading(true);
+   setIsError(false);
    try {
      const { data: { session }, error } = await supabase.auth.signInWithPassword({ 
        email, 
        password 
      });
      if (error !== null) {
        setPassword('');
        setIsError(true);
      }
      if (session !== null) {
        window.location.href = redirectUrl
      }
+   } catch (error) {
+     setIsError(true);
+     setPassword('');
+   } finally {
+     setIsLoading(false);
+   }
  }

92-99: Consider implementing proper internationalization

The current Labels class uses hardcoded Chinese strings. Consider using a proper i18n solution:

  1. Consider using react-i18next or similar libraries for better internationalization support
  2. Move translations to separate JSON files
  3. Add language switching capability

Example implementation with react-i18next:

// locales/zh/common.json
{
  "login": {
    "account": "信箱或手機號碼",
    "password": "密碼",
    "remember": "保持我的登入狀態",
    "login": "登入",
    "invalidCredentials": "登入失敗"
  }
}

// Using translations
import { useTranslation } from 'react-i18next';

function LoginPage() {
  const { t } = useTranslation();
  // Use t('login.account') instead of Labels.account
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between a2b8fcf and 42bfd80.

📒 Files selected for processing (1)
  • src/routes/login.tsx (3 hunks)
🔇 Additional comments (1)
src/routes/login.tsx (1)

Line range hint 1-18: LGTM: Clean state management implementation

The new state variables for handling remember-me functionality and error states are well-implemented using React's useState hook.

src/routes/login.tsx Outdated Show resolved Hide resolved
src/routes/login.tsx Outdated Show resolved Hide resolved
@1989ONCE 1989ONCE requested review from yeahlowflicker and 1989ONCE and removed request for yeahlowflicker November 3, 2024 02:35
@daviddaytw daviddaytw added the enhancement New feature or request label Nov 4, 2024
1989ONCE
1989ONCE previously approved these changes Nov 14, 2024
yeahlowflicker
yeahlowflicker previously approved these changes Nov 22, 2024
@1989ONCE 1989ONCE dismissed stale reviews from yeahlowflicker and themself via b9317a1 November 27, 2024 06:37
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (4)
src/routes/login.tsx (4)

Line range hint 21-33: Enhance error handling and add loading state

The current implementation could benefit from:

  1. Input validation before submission
  2. Loading state during authentication
  3. More specific error handling

Consider implementing these improvements:

 function LoginPage() {
   const [email, setEmail] = useState('')
   const [password, setPassword] = useState('')
   const [isRemember, setIsRemember] = useState(false);
   const [isError, setIsError] = useState(false);
+  const [isLoading, setIsLoading] = useState(false);
+  const [errorMessage, setErrorMessage] = useState('');

   async function login(event: React.FormEvent<HTMLFormElement>) {
     event.preventDefault();
+    
+    // Basic validation
+    if (!email.trim() || !password.trim()) {
+      setErrorMessage(Labels.emptyFields);
+      return;
+    }
+    
+    setIsLoading(true);
+    setIsError(false);
+    setErrorMessage('');
+    
     try {
       const { data: { session }, error } = await supabase.auth.signInWithPassword({ 
         email, 
         password 
       });

       if (error) {
         setPassword('');
         setIsError(true);
+        setErrorMessage(error.message);
+        return;
       }

       if (session) {
         window.location.href = redirectUrl;
       }
     } catch (error) {
+      setErrorMessage(Labels.unexpectedError);
+      setIsError(true);
     } finally {
+      setIsLoading(false);
     }
   }

73-81: Improve error message implementation

The current error message implementation can be enhanced:

  1. Use proper ARIA attributes
  2. Simplify the conditional rendering
-      {
-        isError
-          ? (
-            <span className='text-red-500 font-bold text-2xl'>
-              {isError ? Labels.wrongAccountOrPassword : ''}
-            </span>
-          )
-          : <></>
-      }
+      {isError && (
+        <div
+          role="alert"
+          className='text-red-500 font-bold text-2xl'
+          aria-live="polite"
+        >
+          {errorMessage || Labels.wrongAccountOrPassword}
+        </div>
+      )}

82-87: Enhance submit button accessibility and loading state

The submit button should indicate loading state and be disabled during submission.

       <button
         type='submit'
-        className='w-full h-20 rounded-md bg-green-600 text-white font-extrabold text-5xl'
+        className='w-full h-20 rounded-md bg-green-600 text-white font-extrabold text-5xl disabled:opacity-50'
+        disabled={isLoading}
+        aria-busy={isLoading}
       >
-        {Labels.login}
+        {isLoading ? Labels.loggingIn : Labels.login}
       </button>

92-99: Consider enhancing the Labels implementation

While centralizing strings is good practice, consider:

  1. Moving Labels to a dedicated localization file
  2. Adding missing labels for loading and validation states

Create a new file src/locales/zh.ts:

export const zhTW = {
  account: '信箱或手機號碼',
  password: '密碼',
  remember: '保持我的登入狀態',
  login: '登入',
  loggingIn: '登入中...',
  wrongAccountOrPassword: '帳號或密碼錯誤',
  emptyFields: '請填寫所有欄位',
  unexpectedError: '發生未預期的錯誤',
};
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 42bfd80 and b9317a1.

📒 Files selected for processing (1)
  • src/routes/login.tsx (3 hunks)
🔇 Additional comments (1)
src/routes/login.tsx (1)

40-53: Add missing accessibility attributes to form inputs

The form inputs need proper accessibility attributes as mentioned in the previous review.

src/routes/login.tsx Show resolved Hide resolved
@1989ONCE 1989ONCE merged commit 94f5423 into NCUAppTeam:main Nov 28, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Development

Successfully merging this pull request may close these issues.

4 participants