Skip to content
This repository has been archived by the owner on Mar 23, 2023. It is now read-only.

Commit

Permalink
feat: implement profile reset (#2811)
Browse files Browse the repository at this point in the history
  • Loading branch information
dated committed Sep 10, 2020
1 parent 030974d commit 330ae87
Show file tree
Hide file tree
Showing 18 changed files with 323 additions and 67 deletions.
3 changes: 1 addition & 2 deletions src/app/components/NavigationBar/NavigationBar.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ARK } from "@arkecosystem/platform-sdk-ark";
import { Environment, ProfileSetting } from "@arkecosystem/platform-sdk-profiles";
import { Environment } from "@arkecosystem/platform-sdk-profiles";
import { EnvironmentProvider } from "app/contexts";
import { httpClient } from "app/services";
import React from "react";
Expand All @@ -11,7 +11,6 @@ export default { title: "App / Components / NavigationBar" };

const env = new Environment({ coins: { ARK }, httpClient, storage: new StubStorage() });
const profile = env.profiles().create("Test profile");
profile.settings().set(ProfileSetting.ExchangeCurrency, "btc");

export const Default = () => {
const notifications = {
Expand Down
36 changes: 18 additions & 18 deletions src/data/platform-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ export const PlatformSdkChoices = {
{ label: "Spanish", value: "spanish" },
],
currencies: [
{ label: "BTC (Ƀ)", value: "btc" },
{ label: "ETH (Ξ)", value: "eth" },
{ label: "LTC (Ł)", value: "ltc" },
{ label: "AUD (A$)", value: "aud" },
{ label: "BRL (R$)", value: "brl" },
{ label: "CAD (C$)", value: "cad" },
{ label: "CHF (CHF)", value: "chf" },
{ label: "CNY (¥)", value: "cny" },
{ label: "EUR (€)", value: "eur" },
{ label: "GBP (£)", value: "gbp" },
{ label: "HKD (HK$)", value: "hkd" },
{ label: "IDR (IDR)", value: "idr" },
{ label: "INR (₹)", value: "inr" },
{ label: "JPY (¥)", value: "jpy" },
{ label: "KRW (₩)", value: "krw" },
{ label: "MXN (MX$)", value: "mxn" },
{ label: "RUB (₽)", value: "rub" },
{ label: "USD ($)", value: "usd" },
{ label: "BTC (Ƀ)", value: "BTC" },
{ label: "ETH (Ξ)", value: "ETH" },
{ label: "LTC (Ł)", value: "LTC" },
{ label: "AUD (A$)", value: "AUD" },
{ label: "BRL (R$)", value: "BRL" },
{ label: "CAD (C$)", value: "CAD" },
{ label: "CHF (CHF)", value: "CHF" },
{ label: "CNY (¥)", value: "CNY" },
{ label: "EUR (€)", value: "EUR" },
{ label: "GBP (£)", value: "GBP" },
{ label: "HKD (HK$)", value: "HKD" },
{ label: "IDR (IDR)", value: "IDR" },
{ label: "INR (₹)", value: "INR" },
{ label: "JPY (¥)", value: "JPY" },
{ label: "KRW (₩)", value: "KRW" },
{ label: "MXN (MX$)", value: "MXN" },
{ label: "RUB (₽)", value: "RUB" },
{ label: "USD ($)", value: "USD" },
],
languages: [{ label: "English", value: "en-US" }],
timeFormats: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type DeleteProfileProps = {

export const DeleteProfile = ({ isOpen, onClose, onCancel, onDelete, profileId }: DeleteProfileProps) => {
const { t } = useTranslation();

const { env, persist } = useEnvironmentContext();

const handleDelete = async () => {
Expand Down
41 changes: 41 additions & 0 deletions src/domains/profile/components/ResetProfile/ResetProfile.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Profile, ProfileSetting } from "@arkecosystem/platform-sdk-profiles";
import React from "react";
import { act, env, fireEvent, getDefaultProfileId, render, waitFor } from "utils/testing-library";

import { ResetProfile } from "./ResetProfile";

let profile: Profile;

describe("ResetProfile", () => {
beforeAll(() => {
profile = env.profiles().findById(getDefaultProfileId());
profile.settings().set(ProfileSetting.Theme, "dark");
env.persist();
});

it("should render", async () => {
const { getByTestId, asFragment } = render(<ResetProfile isOpen profile={profile} />);

await waitFor(() => expect(getByTestId("modal__inner")).toBeTruthy());
expect(getByTestId("ResetProfile__submit-button")).toBeInTheDocument();
expect(asFragment()).toMatchSnapshot();
});

it("should reset profile", async () => {
const onReset = jest.fn();

const { getByTestId } = render(<ResetProfile isOpen profile={profile} onReset={onReset} />);

const theme = profile.settings().get(ProfileSetting.Theme);

await waitFor(() => expect(getByTestId("modal__inner")).toBeTruthy());

act(() => {
fireEvent.click(getByTestId("ResetProfile__submit-button"));
});

await waitFor(() => expect(profile.settings().get(ProfileSetting.Theme)).not.toBe(theme));

expect(onReset).toHaveBeenCalled();
});
});
60 changes: 60 additions & 0 deletions src/domains/profile/components/ResetProfile/ResetProfile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Profile } from "@arkecosystem/platform-sdk-profiles";
import { images } from "app/assets/images";
import { Button } from "app/components/Button";
import { Icon } from "app/components/Icon";
import { Modal } from "app/components/Modal";
import { useEnvironmentContext } from "app/contexts";
import { toasts } from "app/services";
import React from "react";
import { useTranslation } from "react-i18next";

const DeleteBanner = images.common.DeleteBanner;

type ResetProfileProps = {
isOpen: boolean;
profile: Profile;
onClose?: any;
onCancel?: any;
onReset?: any;
};

export const ResetProfile = ({ isOpen, profile, onClose, onCancel, onReset }: ResetProfileProps) => {
const { t } = useTranslation();

const { persist } = useEnvironmentContext();

const handleReset = async () => {
profile.flush();

await persist();

toasts.success(t("PROFILE.MODAL_RESET_PROFILE.SUCCESS"));

onReset?.();
};

return (
<Modal
title={t("PROFILE.MODAL_RESET_PROFILE.TITLE")}
image={<DeleteBanner className="w-3/5 m-auto my-8" />}
description={t("PROFILE.MODAL_RESET_PROFILE.DESCRIPTION")}
isOpen={isOpen}
onClose={onClose}
>
<div className="flex justify-end mt-8 space-x-3">
<Button variant="plain" onClick={onCancel} data-testid="ResetProfile__cancel-button">
{t("COMMON.CANCEL")}
</Button>

<Button type="submit" onClick={handleReset} data-testid="ResetProfile__submit-button">
<Icon name="Reset" />
<span>{t("COMMON.RESET_DATA")}</span>
</Button>
</div>
</Modal>
);
};

ResetProfile.defaultProps = {
isOpen: false,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`ResetProfile should render 1`] = `
<DocumentFragment>
<div
class="fixed inset-0 z-50 w-full h-full overflow-y-scroll"
>
<div
class="fixed z-50 w-full h-full bg-black opacity-50"
data-testid="modal__overlay"
/>
<div
class="sc-AxiKw cTRlJa absolute top-0 left-0 right-0 z-50 flex flex-col p-10 mx-auto mt-24 mb-24 overflow-hidden rounded-xl bg-theme-background"
data-testid="modal__inner"
>
<div
class="absolute top-0 right-0 z-50 mt-5 mr-5 rounded bg-theme-primary-100 hover:text-white hover:bg-theme-neutral-900"
>
<button
class="sc-AxjAm goVygI w-11 h-11"
color="primary"
data-testid="modal__close-btn"
type="button"
>
<div
class="sc-AxirZ fFqjhX"
height="14"
width="14"
>
<svg>
cross-slim.svg
</svg>
</div>
</button>
</div>
<div
class="relative"
>
<h2
class="mb-0 text-3xl font-bold"
>
Reset Profile Data
</h2>
<div
class="flex-1"
>
<svg
class="w-3/5 m-auto my-8"
>
delete-banner.svg
</svg>
<div
class="mt-1 text-theme-neutral-dark"
>
Do you really want to reset this profile? Once reset, you will not be able to restore it.
</div>
<div
class="flex justify-end mt-8 space-x-3"
>
<button
class="sc-AxjAm fPpool"
color="primary"
data-testid="ResetProfile__cancel-button"
type="button"
>
Cancel
</button>
<button
class="sc-AxjAm kbZyZT"
color="primary"
data-testid="ResetProfile__submit-button"
type="submit"
>
<div
class="sc-AxirZ iQaTP"
height="1em"
width="1em"
>
<svg>
reset.svg
</svg>
</div>
<span>
Reset Data
</span>
</button>
</div>
</div>
</div>
</div>
</div>
</DocumentFragment>
`;
1 change: 1 addition & 0 deletions src/domains/profile/components/ResetProfile/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./ResetProfile";
5 changes: 5 additions & 0 deletions src/domains/profile/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ export const translations: { [key: string]: any } = {
TITLE: "Delete Profile",
DESCRIPTION: "Do you really want to delete this profile? Once deleted, you will not be able to restore it.",
},
MODAL_RESET_PROFILE: {
TITLE: "Reset Profile Data",
DESCRIPTION: "Do you really want to reset this profile? Once reset, you will not be able to restore it.",
SUCCESS: "Your profile was reset succesfully",
},
MODAL_REPOSITORIES: {
TITLE: "Repository",
BitBucket: "Bitbucket",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const baseSettings = {
ADVANCED_MODE: false,
AUTOMATIC_SIGN_OUT_PERIOD: 15,
BIP39_LOCALE: "english",
EXCHANGE_CURRENCY: "btc",
EXCHANGE_CURRENCY: "BTC",
LEDGER_UPDATE_METHOD: false,
LOCALE: "en-US",
MARKET_PROVIDER: "coincap",
Expand Down
14 changes: 4 additions & 10 deletions src/domains/profile/pages/CreateProfile/CreateProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,14 @@ export const CreateProfile = () => {
},
];

const submitForm = async ({ name, password, currency, isDarkMode, marketProvider }: any) => {
const handleSubmit = async ({ name, password, currency, isDarkMode, marketProvider }: any) => {
const formattedName = name.substring(0, nameMaxLength);

const profile = env.profiles().create(formattedName);

profile.settings().set(ProfileSetting.AdvancedMode, false);
profile.settings().set(ProfileSetting.AutomaticSignOutPeriod, 15);
profile.settings().set(ProfileSetting.Bip39Locale, PlatformSdkChoices.passphraseLanguages[2].value);
profile.settings().set(ProfileSetting.ExchangeCurrency, currency);
profile.settings().set(ProfileSetting.LedgerUpdateMethod, false);
profile.settings().set(ProfileSetting.Locale, PlatformSdkChoices.languages[0].value);
profile.settings().set(ProfileSetting.MarketProvider, marketProvider);
profile.settings().set(ProfileSetting.ScreenshotProtection, true);
profile.settings().set(ProfileSetting.ExchangeCurrency, currency);
profile.settings().set(ProfileSetting.Theme, isDarkMode ? "dark" : "light");
profile.settings().set(ProfileSetting.TimeFormat, PlatformSdkChoices.timeFormats[0].value);

if (avatarImage && !isSvg) {
profile.settings().set(ProfileSetting.Avatar, avatarImage);
Expand Down Expand Up @@ -92,7 +86,7 @@ export const CreateProfile = () => {

<Divider />

<Form context={form} onSubmit={submitForm} data-testid="CreateProfile__form">
<Form context={form} onSubmit={handleSubmit} data-testid="CreateProfile__form">
<div className="mt-8">
<h2>{t("SETTINGS.GENERAL.PERSONAL.TITLE")}</h2>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ exports[`CreateProfile should not upload avatar image 1`] = `
name="currency"
readonly=""
type="text"
value="btc"
value="BTC"
/>
<div
class="relative w-full cursor-pointer"
Expand Down Expand Up @@ -1189,7 +1189,7 @@ exports[`CreateProfile should store profile 1`] = `
name="currency"
readonly=""
type="text"
value="btc"
value="BTC"
/>
<div
class="relative w-full cursor-pointer"
Expand Down Expand Up @@ -1644,7 +1644,7 @@ exports[`CreateProfile should store profile with password 1`] = `
name="currency"
readonly=""
type="text"
value="btc"
value="BTC"
/>
<div
class="relative w-full cursor-pointer"
Expand Down Expand Up @@ -2099,7 +2099,7 @@ exports[`CreateProfile should upload and remove avatar image 1`] = `
name="currency"
readonly=""
type="text"
value="btc"
value="BTC"
/>
<div
class="relative w-full cursor-pointer"
Expand Down
20 changes: 20 additions & 0 deletions src/domains/setting/e2e/reset-profile.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Selector } from "testcafe";

import { buildTranslations } from "../../../app/i18n/helpers";
import { createFixture } from "../../../utils/e2e-utils";
import { goToSettings } from "./common";

const translations = buildTranslations();

createFixture(`Reset profile`).beforeEach(async (t) => await goToSettings(t));

test("should reset profile", async (t) => {
const name = await Selector("input[name=name]").value;
const automaticSignOutPeriod = await Selector("input[name=automaticSignOutPeriod]").value;

await t.click(Selector("button").withText(translations.COMMON.RESET_DATA));

// TODO: uncomment when rerender is implemented
// await t.expect(Selector("input[name=name]").value).eql(name);
// await t.expect(Selector("input[name=automaticSignOutPeriod]").value).notEql(automaticSignOutPeriod);
});
Loading

0 comments on commit 330ae87

Please sign in to comment.