8000 Restore saved profile values on cancel by FayezBast · Pull Request #55558 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions js/apps/account-ui/src/personal-info/PersonalInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from "@patternfly/react-core";
import { ExternalLinkSquareAltIcon } from "@patternfly/react-icons";
import { TFunction } from "i18next";
import { useState } from "react";
import { useRef, useState } from "react";
import { ErrorOption, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";

Expand All @@ -35,14 +35,25 @@ import type { TFuncKey } from "../i18n-type";
import { useAccountAlerts } from "../utils/useAccountAlerts";
import { usePromise } from "../utils/usePromise";

function toFormValues(profile: UserRepresentation): UserRepresentation {
return {
...profile,
attributes: Object.fromEntries(
Object.entries(profile.attributes || {}).map(([k, v]) => [beerify(k), v]),
),
};
}

export const PersonalInfo = () => {
const { t } = useTranslation();
const context = useEnvironment<AccountEnvironment>();
const [userProfileMetadata, setUserProfileMetadata] =
useState<UserProfileMetadata>();
const [supportedLocales, setSupportedLocales] = useState<string[]>([]);
const [isSaving, setIsSaving] = useState(false);
const form = useForm<UserRepresentation>({ mode: "onChange" });
const { handleSubmit, reset, setValue, setError } = form;
const { handleSubmit, reset, setError } = form;
const saveInProgress = useRef(false);
const { addAlert } = useAccountAlerts();

usePromise(
Expand All @@ -54,14 +65,17 @@ export const PersonalInfo = () => {
([personalInfo, supportedLocales]) => {
setUserProfileMetadata(personalInfo.userProfileMetadata);
setSupportedLocales(supportedLocales);
reset(personalInfo);
Object.entries(personalInfo.attributes || {}).forEach(([k, v]) =>
setValue(`attributes[${beerify(k)}]`, v),
);
reset(toFormValues(personalInfo));
},
);

const (user: UserRepresentation) => {
if (saveInProgress.current) {
return;
}
saveInProgress.current = true;
setIsSaving(true);

try {
const attributes = Object.fromEntries(
Object.entries(user.attributes || {}).map(([k, v]) => [
Expand All @@ -70,6 +84,10 @@ export const PersonalInfo = () => {
]),
);
await savePersonalInfo(context, { ...user, attributes });
const savedUser = await getPersonalInfo({ context })
.then(toFormValues)
.catch(() => user);
reset(savedUser, { keepValues: true });
const locale = attributes["locale"]?.toString();
if (locale) {
window.dispatchEvent(
Expand All @@ -87,6 +105,9 @@ export const PersonalInfo = () => {
setError(name as string, error as ErrorOption),
((key: TFuncKey, param?: object) => t(key, param as any)) as TFunction,
);
} finally {
saveInProgress.current = false;
setIsSaving(false);
}
};

Expand Down Expand Up @@ -147,13 +168,15 @@ export const PersonalInfo = () => {
type="submit"
id="save-btn"
variant="primary"
isDisabled={isSaving}
>
{t("save")}
</Button>
<Button
data-testid="cancel"
id="cancel-btn"
variant="link"
isDisabled={isSaving}
=> reset()}
>
{t("cancel")}
Expand Down
11 changes: 9 additions & 2 deletions js/apps/account-ui/test/personal-info/personal-info.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@ import { adminClient } from "../support/admin-client.ts";
import userProfileRealm from "../realms/user-profile-realm.json" with { type: "json" };

test.describe("Personal info", () => {
test("sets basic information", async ({ page }) => {
test("restores saved basic information on cancel", async ({ page }) => {
await using testBed = await createTestBed();

await login(page, testBed.realm);

await page.getByTestId("email").fill("edewit@somewhere.com");
await page.getByTestId("email").fill("EdeWit@Somewhere.com");
await page.getByTestId("firstName").fill("Erik");
await page.getByTestId("lastName").fill("de Wit");
await page.getByTestId("save").click();

await assertLastAlert(page, "Your account has been updated.");

await page.getByTestId("email").fill("unsaved@somewhere.com");
await page.getByTestId("lastName").fill("Unsaved");
await page.getByTestId("cancel").click();

await expect(page.getByTestId("email")).toHaveValue("edewit@somewhere.com");
await expect(page.getByTestId("lastName")).toHaveValue("de Wit");
});
});

Expand Down
0