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

Add option to copy new value to form in edit profile workbench #10885

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

maxidragon
Copy link
Member

@maxidragon maxidragon commented Feb 19, 2025

Screencast.From.2025-02-19.12-54-48.mp4

Copy link
Member

@gregorbg gregorbg left a comment

Choose a reason for hiding this comment

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

Here are some initial thoughts. Also, please try to open PRs with at least a very basic description of what's happening (as long as the diff is more than ~3 lines).
Screenshots or videos would also be appreciated wherever it makes sense (for example, changing the text of a button does not need a screenshot, but your current PR probably does.)

Comment on lines +93 to +97
useEffect(() => {
if (defaultValues) {
setEditedUserDetails((prev) => ({ ...prev, ...defaultValues }));
}
}, [defaultValues]);
Copy link
Member

Choose a reason for hiding this comment

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

This is not what useEffect was designed for. Is there really no other way?

Copy link
Member Author

Choose a reason for hiding this comment

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

I was chatting with Daniel and we came up with this solution - I can try to find a different solution, but there is a high chance that useEffect is necessary here

Copy link
Member

Choose a reason for hiding this comment

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

One way I can think of where we can avoid useEffect is - pass the editedUserDetails and setEditedUserDetails from the parent component. But since this form is used in two different places, I'm bit reluctant to use that way and will be more inclined towards using useEffect.

Copy link
Member

Choose a reason for hiding this comment

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

After thinking again, I got one more solution - for this I would like to rename defaultValues to forcedValues. The forcedValues will initially be {}.

When a field is populated, it will become like {name: "John Doe"}.

Now in <Form.Input.../>, we can have the value as {forcedValues?.name || <whatever is already there>} and disabled will be {forcedValues?.name || <whatever is already there>}. This way, if there is a forced value, that will be having highest priority and user won't be able to edit it as well.

While calling the API, instead of sending person as editedUserDetails, we might have to send {...editedUserDetails, ...forcedValues}.

This might be complicated, I'm not sure. @gregorbg WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

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

One way I can think of where we can avoid useEffect is - pass the editedUserDetails and setEditedUserDetails from the parent component. But since this form is used in two different places, I'm bit reluctant to use that way and will be more inclined towards using useEffect.

I've also considered this but I think this would make the component much more complicated and harder to use in other places, so I agree with you.

I have no strong preference regarding your second comment, both solutions would probably work but the second one looks more complicated than mine. Let's wait for Gregor's opinion, if you guys think that that would be better, I'm more than happy to implement it in that way.

@maxidragon
Copy link
Member Author

Here are some initial thoughts. Also, please try to open PRs with at least a very basic description of what's happening (as long as the diff is more than ~3 lines).
Screenshots or videos would also be appreciated wherever it makes sense (for example, changing the text of a button does not need a screenshot, but your current PR probably does.)

Edited the description, I've uploaded the video

@@ -59,6 +70,11 @@ function EditPersonRequestedChangesList({ requestedChanges }) {
function EditPersonTicketWorkbenchForWrt({ ticketDetails, actingStakeholderId, sync }) {
const { ticket } = ticketDetails;
const { save, saving } = useSaveAction();
const [defaultValues, setDefaultValues] = useState(
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to prefill this with default value? Instead if we have it empty and populate whenever the button is clicked, won't this work?

Comment on lines +93 to +97
useEffect(() => {
if (defaultValues) {
setEditedUserDetails((prev) => ({ ...prev, ...defaultValues }));
}
}, [defaultValues]);
Copy link
Member

Choose a reason for hiding this comment

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

One way I can think of where we can avoid useEffect is - pass the editedUserDetails and setEditedUserDetails from the parent component. But since this form is used in two different places, I'm bit reluctant to use that way and will be more inclined towards using useEffect.

Comment on lines +93 to +97
useEffect(() => {
if (defaultValues) {
setEditedUserDetails((prev) => ({ ...prev, ...defaultValues }));
}
}, [defaultValues]);
Copy link
Member

Choose a reason for hiding this comment

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

After thinking again, I got one more solution - for this I would like to rename defaultValues to forcedValues. The forcedValues will initially be {}.

When a field is populated, it will become like {name: "John Doe"}.

Now in <Form.Input.../>, we can have the value as {forcedValues?.name || <whatever is already there>} and disabled will be {forcedValues?.name || <whatever is already there>}. This way, if there is a forced value, that will be having highest priority and user won't be able to edit it as well.

While calling the API, instead of sending person as editedUserDetails, we might have to send {...editedUserDetails, ...forcedValues}.

This might be complicated, I'm not sure. @gregorbg WDYT?

@gregorbg
Copy link
Member

Hmm. The problem seems to be that the EditPersonForm holds its own state. That means, it was initially designed to have full control over its own values.
Now you're coming in and you suddenly want outside control over values that were not designed to have outside control. That's why this whole useEffect hack becomes necessary. I will have to do some playing around to see which other solutions might work

@gregorbg
Copy link
Member

Okay so here's the thing: If you desperately need this and you say WRT is suffocating under the workload unless we get this PR merged in the next 48 hours, then I'll review it in its current state.

Otherwise: You need to make clear that EditPersonForm is actually doing two things right now:

  1. Fetching person data, converting that backend information into a form state
  2. Rendering an actual form (that is, the inputs that the user can interact with) based on the state derived in (1).

The clean solution is to split these two aspects into two separate components. And then the "inner" component derived from point (2) gets passed the current form state and the updateFormValues callback as props. This will allow you to control the form state from the outside, and will let the buttons here directly manipulate the form state as well. No need for useEffect hooks anymore.

Does that idea make sense or would you like some more in-depth explanation?

@maxidragon
Copy link
Member Author

This is definitely not urgent and can be implemented as you suggested. I'll look into it later and let you know if I need a detailed explanation.

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