add types and comment

This commit is contained in:
Rémi de Juvigny 2023-11-14 15:22:12 +01:00
parent b6345ffd2d
commit ae4ceab052
2 changed files with 33 additions and 15 deletions

View File

@ -131,10 +131,24 @@ const ProfilePage = () => {
UpdateUsersMeBody
>(
async (body) => {
const { confirmPassword: _confirmPassword, currentTheme, ...dataToSend } = body;
const { confirmPassword: _confirmPassword, currentTheme, ...bodyRest } = body;
let dataToSend = bodyRest;
if (dataToSend.password === '') {
delete dataToSend.password;
const isPasswordRequestBody = (
data: UpdateMe.Request['body']
): data is UpdateMe.PasswordRequestBody => {
return 'password' in data;
};
// The password fields are optional. If the user didn't touch them, don't send any password
// to the API, because an empty string would throw a validation error
if (isPasswordRequestBody(dataToSend) && dataToSend.password === '') {
const {
password: _password,
currentPassword: _currentPassword,
...passwordRequestBodyRest
} = dataToSend;
dataToSend = passwordRequestBodyRest;
}
const { data } = await put<UpdateMe.Response>('/admin/users/me', dataToSend);
@ -259,8 +273,7 @@ const ProfilePage = () => {
username,
preferedLanguage,
currentTheme,
password,
confirmPassword,
...passwordValues
},
handleChange,
isSubmitting,
@ -298,7 +311,7 @@ const ProfilePage = () => {
<PasswordSection
errors={errors}
onChange={handleChange}
values={{ password, confirmPassword }}
values={passwordValues}
/>
)}
<PreferencesSection

View File

@ -20,17 +20,22 @@ export declare namespace GetMe {
* PUT /users/me - Update the current admin user
*/
export declare namespace UpdateMe {
export interface BaseRequestBody {
email?: string;
firstname?: string;
lastname?: string;
username?: string;
preferedLanguage?: string;
}
export interface PasswordRequestBody extends BaseRequestBody {
currentPassword: string;
password: string;
}
export interface Request {
query: {};
body: {
email?: string;
firstname?: string;
lastname?: string;
username?: string;
password?: string;
currentPassword?: string;
preferedLanguage?: string;
};
body: BaseRequestBody | PasswordRequestBody;
}
export interface Response {