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

Refactor Switchers and upding isAdmin #378

Merged
merged 12 commits into from
Feb 24, 2025
Merged

Conversation

sodic
Copy link
Collaborator

@sodic sodic commented Feb 20, 2025

Description

Refactoring OpenSaas's user module

Contributor Checklist

Make sure to do the following steps if they are applicable to your PR:

@sodic sodic changed the title Filip refactor user module Refactor Switchers and upding isAdmin Feb 21, 2025
@sodic sodic marked this pull request as ready for review February 21, 2025 08:09
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I moved this file into a more appropriate folder.

For both switchers, I extracted isOn and setIsOn as props.

SwitcherOne could have only been used for updating isAdmin. It's now general

Copy link
Collaborator

@vincanger vincanger 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. Tested it locally and works. The opensaas-sh diff needs updating -- instructions are in the opensaas-sh README, but let me know if you need any help with that.

import { updateIsUserAdminById } from 'wasp/client/operations';
import { type User } from 'wasp/entities';

const AdminSwitch = ({ id, isAdmin }: Pick<User, 'id' | 'isAdmin'>) => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

For some reason I prefer the function AdminSwitch(...) {} way of defining components. One objective reason would be they are named functions then.

Copy link
Collaborator Author

@sodic sodic Feb 21, 2025

Choose a reason for hiding this comment

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

I agree 100% (some more reasons here), but I wanted to stick with the repo's convention (most if not all components are arrow functions).

That said, I see I've messed it up in other places, so I'll probably make those arrow functions too if that's alright.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not start at some point writing the named functions variant if we feel like it's better and make into a task to rewrite all the arrow functions? :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's up for discussion definitely, but IMO consistency is more important than the specific choice in this case.
If we did that, the codebase would exist in in a non-consistent state for who a long time (possibly even forever).

However, I did a quick search, and it seems we're already pretty inconsistent 😄
OpenSaas (and Wasp) are a mix of arrows and normal functions.

So, with that excuse, I'll rewrite all my refactors to use function 👍

@@ -109,7 +115,11 @@ const UsersTable = () => {
<option value=''>Select filters</option>
{['past_due', 'canceled', 'active', 'deleted', null].map((status) => {
if (!statusOptions.includes(status as SubscriptionStatus)) {
return <option value={status || ''}>{status ? status : 'has not subscribed'}</option>;
return (
<option key={status} value={status || ''}>
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we maybe want to use the more modern ?? operator here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I refactored this in #382

@@ -201,7 +205,13 @@ const FormElements = ({ user }: { user: AuthUser }) => {
<option value=''>Canada</option>
</select>
<span className='absolute top-1/2 right-4 z-10 -translate-y-1/2'>
<svg width='24' height='24' viewBox='0 0 24 24' fill='none' xmlns='http://www.w3.org/2000/svg'>
<svg
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not extract these SVGs as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Forgot, will do, thanks!

import { cn } from '../../../client/cn';
import { ChangeEventHandler } from 'react';

const SwitcherOne = ({
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice!

onChange,
}: {
isOn: boolean;
onChange: ChangeEventHandler<HTMLInputElement>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Usually, I've seen this defined as simply onChange: (value: boolean) => void and then in the input you use onChange={e => onChange(e.target.checked) }

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You're right, this is an abstraction leak. I'll change it up.

Copy link
Collaborator

@infomiho infomiho left a comment

Choose a reason for hiding this comment

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

Suggested smaller improvements

@sodic sodic merged commit 64dcb48 into main Feb 24, 2025
1 check passed
@sodic sodic deleted the filip-refactor-user-module branch February 24, 2025 16:01
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.

3 participants