-
Notifications
You must be signed in to change notification settings - Fork 186
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
maxidragon
wants to merge
1
commit into
thewca:main
Choose a base branch
from
maxidragon:copy-changes-to-form
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import React from 'react'; | ||
import { Header, Message, Table } from 'semantic-ui-react'; | ||
import React, { useState } from 'react'; | ||
import { | ||
Button, Header, Message, Table, | ||
} from 'semantic-ui-react'; | ||
import EditPersonForm from '../../Panel/pages/EditPersonPage/EditPersonForm'; | ||
import useSaveAction from '../../../lib/hooks/useSaveAction'; | ||
import { actionUrls } from '../../../lib/requests/routes.js.erb'; | ||
|
@@ -30,7 +32,7 @@ function EditPersonValidations({ ticketDetails }) { | |
)); | ||
} | ||
|
||
function EditPersonRequestedChangesList({ requestedChanges }) { | ||
function EditPersonRequestedChangesList({ requestedChanges, copyToForm }) { | ||
return ( | ||
<> | ||
<Header as="h3">Requested changes</Header> | ||
|
@@ -40,14 +42,23 @@ function EditPersonRequestedChangesList({ requestedChanges }) { | |
<Table.HeaderCell>Field</Table.HeaderCell> | ||
<Table.HeaderCell>Old value</Table.HeaderCell> | ||
<Table.HeaderCell>New value</Table.HeaderCell> | ||
<Table.HeaderCell>Actions</Table.HeaderCell> | ||
</Table.Row> | ||
</Table.Header> | ||
<Table.Body> | ||
{requestedChanges?.map((change) => ( | ||
<Table.Row> | ||
<Table.Row key={change.field_name}> | ||
<Table.Cell>{I18n.t(`activerecord.attributes.user.${change.field_name}`)}</Table.Cell> | ||
<Table.Cell>{formatField(change.field_name, change.old_value)}</Table.Cell> | ||
<Table.Cell>{formatField(change.field_name, change.new_value)}</Table.Cell> | ||
<Table.Cell> | ||
<Button onClick={() => copyToForm(change.field_name, change.new_value)}> | ||
Copy to form | ||
</Button> | ||
<Button onClick={() => copyToForm(change.field_name, change.old_value)}> | ||
Undo | ||
</Button> | ||
</Table.Cell> | ||
</Table.Row> | ||
))} | ||
</Table.Body> | ||
|
@@ -59,6 +70,11 @@ function EditPersonRequestedChangesList({ requestedChanges }) { | |
function EditPersonTicketWorkbenchForWrt({ ticketDetails, actingStakeholderId, sync }) { | ||
const { ticket } = ticketDetails; | ||
const { save, saving } = useSaveAction(); | ||
const [defaultValues, setDefaultValues] = useState( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? |
||
ticket.metadata?.tickets_edit_person_fields.map((change) => ({ | ||
[change.field_name]: change.old_value, | ||
})).reduce((acc, obj) => ({ ...acc, ...obj }), {}), | ||
); | ||
|
||
const closeTicket = () => { | ||
save( | ||
|
@@ -72,6 +88,10 @@ function EditPersonTicketWorkbenchForWrt({ ticketDetails, actingStakeholderId, s | |
); | ||
}; | ||
|
||
const copyToForm = (field, value) => { | ||
setDefaultValues({ ...defaultValues, [field]: value }); | ||
}; | ||
|
||
if (saving) return <Loading />; | ||
|
||
return ( | ||
|
@@ -81,10 +101,12 @@ function EditPersonTicketWorkbenchForWrt({ ticketDetails, actingStakeholderId, s | |
/> | ||
<EditPersonRequestedChangesList | ||
requestedChanges={ticket.metadata?.tickets_edit_person_fields} | ||
copyToForm={copyToForm} | ||
/> | ||
<EditPersonForm | ||
wcaId={ticket.metadata.wca_id} | ||
onSuccess={closeTicket} | ||
defaultValues={defaultValues} | ||
/> | ||
</> | ||
); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 hereThere was a problem hiding this comment.
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 theeditedUserDetails
andsetEditedUserDetails
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 usinguseEffect
.There was a problem hiding this comment.
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
toforcedValues
. TheforcedValues
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>}
anddisabled
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
aseditedUserDetails
, we might have to send{...editedUserDetails, ...forcedValues}
.This might be complicated, I'm not sure. @gregorbg WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.