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

Issue 1542/refactor tags hook #1558

Merged

Conversation

kaulfield23
Copy link
Contributor

@kaulfield23 kaulfield23 commented Sep 26, 2023

Description

This PR refactors the tagModel into useTag hook.

Screenshots

Changes

  • Create useTag and fix its relevant codes
  • Create useTagMutation and fix its relevant codes
  • Delete tagModel.ts

Notes to reviewer

Didn't delete tagRepo since assignTagToPerson and removeTagFromPerson are used in ViewDataModel.ts

Because I noticed that assignTagToPerson and removeTagFromPerson are used in other places, I decided to make a useTagMutation hook instead of put those methods in useTag.
This will be useful in the future, once tagRepo is removed, as you'll be able to use useTagMutation to other hooks.

Related issues

part of issue-1542

@rebecarubio rebecarubio self-assigned this Sep 26, 2023
Copy link
Contributor

@rebecarubio rebecarubio left a comment

Choose a reason for hiding this comment

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

Nice fix! This lgtm but I would like @richardolsson to double check, as we are getting familiar with this new pattern :)

Copy link
Member

@richardolsson richardolsson left a comment

Choose a reason for hiding this comment

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

Nice work in general! There are some changes that I think would make the code cleaner though. I've commented below.

Comment on lines 12 to 14
tagData: ZetkinTag | null;
tagError: unknown | null;
tagIsLoading: boolean;
Copy link
Member

Choose a reason for hiding this comment

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

The name of the hook already tells us that the hook will return a tag, so there is no need for these prefixes. I would call these properties:

Suggested change
tagData: ZetkinTag | null;
tagError: unknown | null;
tagIsLoading: boolean;
data: ZetkinTag | null;
error: unknown | null;
isLoading: boolean;

);
store.dispatch(tagAssigned([personId, tag]));
};
const getTag = () => {
Copy link
Member

Choose a reason for hiding this comment

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

This function is unnecessary. Inline it's contents inside the hook instead. You can just grab the return value from loadItemIfNecessary and use that further down the function where you are currently calling getTag().

Comment on lines 10 to 11
assignToPerson: (personId: number, value?: string) => void;
removeFromPerson: (personId: number) => Promise<void>;
Copy link
Member

Choose a reason for hiding this comment

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

I like how these mutation functions are returned.

Copy link
Contributor

@rebecarubio rebecarubio left a comment

Choose a reason for hiding this comment

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

lgtm 🚀

@rebecarubio rebecarubio merged commit fcf7956 into issue-1542/refactor-to-use-hooks Sep 28, 2023
4 checks passed
@rebecarubio rebecarubio deleted the issue-1542/refactor-tags-hook branch September 28, 2023 12:59
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