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

Implement Validation for Filtering #86

Merged
merged 15 commits into from
Oct 27, 2024
Merged

Implement Validation for Filtering #86

merged 15 commits into from
Oct 27, 2024

Conversation

gpalmer27
Copy link
Contributor

Description

Makes sure that the workTerm and workEnvironment that is inputted is valid and gives an error otherwise.

Motivation and Context

Closes #72

How has this been tested?

Screenshots (if appropriate):

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • My code follows the code style of this project.
  • I have moved the ticket to "In Review"
  • I have added reviewers to this PR
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

Copy link

vercel bot commented Oct 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
cooper ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 27, 2024 8:19pm
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
cooper-auth ⬜️ Skipped (Inspect) Oct 27, 2024 8:19pm
cooper-docs ⬜️ Skipped (Inspect) Oct 27, 2024 8:19pm

const [error, setError] = useState<string | undefined>(undefined);

useEffect(() => {
const isValidTerm = searchParams?.workTerm
Copy link
Collaborator

Choose a reason for hiding this comment

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

i'm fairly certain we don't need to check for random invalid search params, like if someone puts josh=best-pl-ever, we can safely ignore

Copy link
Collaborator

Choose a reason for hiding this comment

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

just a comment, not a change

return (
<>
<SearchFilter />
{/* TODO: Loading animations */}
{error && showAlert && (
<div
id="bad-input"
Copy link
Collaborator

Choose a reason for hiding this comment

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

i think we should make this a component so we can reuse this toast pop up in other places, @RishikeshNK thoughts?

Copy link
Collaborator

@RishikeshNK RishikeshNK Oct 15, 2024

Choose a reason for hiding this comment

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

Yup! I agree. We can move this to a separate component that takes in the message as a prop. @joshw2048

Copy link
Collaborator

@RishikeshNK RishikeshNK left a comment

Choose a reason for hiding this comment

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

Looks good! Added a few suggestions to make use of our existing validation library (Zod) which should eliminate the need for the useEffect and maybe using a shadcn component for the toast / modal.


import type {
import {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can fix this warning by doing something like this:

import type {
  ReviewType,
  WorkEnvironmentType,
  WorkTermType,
} from "@cooper/db/schema";
import { WorkEnvironment, WorkTerm } from "@cooper/db/schema";

@@ -22,6 +24,24 @@ export default function Roles({
workEnvironment?: WorkEnvironmentType;
};
}) {
const [error, setError] = useState<string | undefined>(undefined);

useEffect(() => {
Copy link
Collaborator

@RishikeshNK RishikeshNK Oct 15, 2024

Choose a reason for hiding this comment

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

We could get away without using a useEffect or possibly a useState for tracking the errors!

We can define a RolesSearchParam schema in the validators package using Zod. Could be something like the following:

const RolesSearchParam = z.object({
  cycle: z.nativeEnum(WorkTerm).optional(),
  term: z.nativeEnum(WorkEnvironment).optional(),
});

We can share this object between the web app and the list endpoint for reviews. For validation, we can do something like the following:

const validationResult = RolesSearchParam.safeParse(searchParams);

const reviews = api.review.list.useQuery({
  options: validationResult.success ? validationResult.data : {},
});

At the same time, we can also use the same success property to display the error modal / toast:

{!validationResult.success && (
        <ErrorAlert message="Invalid search parameters." />
      )}

If you want more specific error messages, you can modify the Zod schema like this:

const RolesSearchParam = z.object({
  cycle: z
    .nativeEnum(WorkTerm, {
      message: "Invalid cycle type",
    })
    .optional(),
  term: z
    .nativeEnum(WorkEnvironment, {
      message: "Invalid term type",
    })
    .optional(),
});

and get the errors using validationResult.error.

return (
<>
<SearchFilter />
{/* TODO: Loading animations */}
{error && showAlert && (
<div
id="bad-input"
Copy link
Collaborator

@RishikeshNK RishikeshNK Oct 15, 2024

Choose a reason for hiding this comment

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

Yup! I agree. We can move this to a separate component that takes in the message as a prop. @joshw2048

return (
<>
<SearchFilter />
{/* TODO: Loading animations */}
{error && showAlert && (
<div
Copy link
Collaborator

Choose a reason for hiding this comment

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

Totally optional but I'd recommend taking a look at shadcn/ui. Almost all of our reusable components are built on top of shadcn. They do have a toast that you can use!

If you're interested, check out this docs page. You can add the component from the root folder using pnpm ui-add toast and use that component here.

@vercel vercel bot temporarily deployed to Preview – cooper-docs October 27, 2024 19:54 Inactive
@vercel vercel bot temporarily deployed to Preview – cooper-auth October 27, 2024 19:54 Inactive
Copy link
Collaborator

@RishikeshNK RishikeshNK left a comment

Choose a reason for hiding this comment

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

FIRE 🪨

@vercel vercel bot temporarily deployed to Preview – cooper-auth October 27, 2024 20:18 Inactive
@vercel vercel bot temporarily deployed to Preview – cooper-docs October 27, 2024 20:18 Inactive
@gpalmer27 gpalmer27 merged commit de2260c into main Oct 27, 2024
8 checks passed
@gpalmer27 gpalmer27 deleted the validation branch October 27, 2024 20:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement validation for filtering
3 participants