-
Notifications
You must be signed in to change notification settings - Fork 43
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
Feat/817 copy another user rights for a new user #899
base: main
Are you sure you want to change the base?
Conversation
We don't need to update backend for this, I would prefer fetching target user rights in frontend first, and sending these rights in the user creation request. Moreover, UI should either allow to copy user rights OR specify manual rights. Right now both are displayed, and if I select a user to copy AND specify manual rights, only manual rights are taken. |
There is still some issues with the form as it is:
|
izanami-frontend/src/pages/users.tsx
Outdated
}, | ||
}} | ||
onSubmit={(ctx) => { | ||
onSubmit={async (ctx) => { |
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.
There is no async / await syntax in Izanami right now, and I would like to keep it this way to keep things simple and avoid associated pitfalls.
Therefore could you use simple promise syntax ?
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.
Yes, I need to remove it since I use it to fetch userData in Submit function
izanami-frontend/src/pages/users.tsx
Outdated
rights: { | ||
label: () => "", | ||
label: "", |
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.
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 did not pay attention that the label value was changed, I will replace it
izanami-frontend/src/pages/users.tsx
Outdated
data.rights = res.rights; | ||
return createInvitation(data.email, data.admin, data.rights); | ||
}) | ||
.catch((error) => { |
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 catch simply rethrow the error, I don't understand what it's for.
Moreover it's present in this if branch, but absent from else.
izanami-frontend/src/pages/users.tsx
Outdated
createInvitation(data.email, data.admin, data.rights) | ||
); | ||
const inviteUserMutation = useMutation< | ||
{ invitationUrl?: string } | null, |
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 whole type is very confusing, invitationUrl is listed but never actually used, Error is too generic. I woulc prefer to just type data
as it was done before and let Typescript infer the rest.
cea9b3a
to
89fed1b
Compare
izanami-frontend/src/pages/users.tsx
Outdated
createInvitation(data.email, data.admin, data.rights) | ||
); | ||
|
||
const readUser = useMutation((user: string) => queryUser(user)); |
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.
Since queryUser doesn't change anything server side it should be a query. However I would prefer not having a query at all, and chaining it with createInvitation
as it was done before (sorry, I told you that userToCopy wasn't necessary anymore, however you'll need it to know it you need to queryUser or not).
Having this logic in mutation makes more sense to me, rather than bloating jsx with this logic.
izanami-frontend/src/pages/users.tsx
Outdated
setCreating(false); | ||
}) | ||
.catch((error) => { | ||
throw new Error(error); |
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.
As I told you in my previous review, this kind of code is not very usefull, since you just rethrow the error.
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.
What do you think about this version :
const inviteUserMutation = useMutation(
async (data: {
email: string;
admin: boolean;
rights: TRights;
userToCopy: string;
}) => {
if (data.userToCopy) {
try {
const res: TUser = await queryUser(data.userToCopy);
data = { ...data, rights: res.rights };
} catch (error) {
throw new Error("Error fetching user rights: " + error);
}
}
return createInvitation(data.email, data.admin, data.rights);
}
);
onSubmit={async (ctx) => {
const backendRights = rightStateArrayToBackendMap(ctx.rights);
let payload = {
rights: backendRights,
admin: ctx.admin,
email: ctx.email,
userToCopy: ctx.userToCopy?.value,
};
return inviteUserMutation
.mutateAsync(payload)
.then((response) => {
if (response && response.invitationUrl) {
setCreationUrl(response.invitationUrl);
}
setCreating(false);
})
.catch((error) => {
throw new Error(error);
});
}}
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.
Its better, except that your code is using async/await (see my previous comment about that) and that it remains a catch that just rethrow the error (as I mentionned twice already, I don't see the point).
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.
@ptitFicus , I commit a modification without Async and double throwing error.
No description provided.