1.2.0 • Published 3 months ago

@os-team/profile v1.2.0

Weekly downloads
-
License
UNLICENSED
Repository
gitlab
Last release
3 months ago

@os-team/profile NPM version BundlePhobia

The profile module for os-team's web apps.

Installation

Install the package using the following command:

yarn add @os-team/profile

Install peer dependencies:

npx install-peerdeps @os-team/profile

Create the public/locales/en/profile.json file with the following content:

{
  "update": "Save",
  "updated": "Saved",
  "menu": "Profile",
  "setAvatar": "Upload an avatar",
  "changeAvatar": "Change the avatar",
  "deleteAvatar": "Delete the avatar",
  "setName": "Specify the name",
  "changeName": "Change the name",
  "changePassword": "Change the password",
  "signOut": "Sign out",
  "updateNameModal": {
    "title": "Set your name",
    "firstName": {
      "label": "First name",
      "placeholder": "Enter the first name"
    },
    "lastName": {
      "label": "Last name",
      "placeholder": "Enter the last name"
    }
  },
  "updatePasswordModal": {
    "title": "Change your password",
    "button": "Change",
    "success": "Password has been changed",
    "currentPassword": {
      "label": "Current password",
      "placeholder": "Enter the current password"
    },
    "newPassword": {
      "label": "New password",
      "placeholder": "Enter a new password"
    },
    "passwordStrengthNames": {
      "weak": "Weak",
      "good": "Good",
      "strong": "Strong",
      "powerful": "Powerful"
    }
  },
  "sessions": {
    "title": "Active sessions",
    "description": "Every time someone logs into your account, a new session is created, which is displayed here. Terminate all suspicious sessions.",
    "current": "Your current session",
    "other": "Other sessions",
    "destroyAllOther": "Terminate all other sessions",
    "destroyed": "The session has been terminated",
    "destroyedAllOther": "All other sessions have been terminated",
    "deviceType": {
      "browser": "Browser",
      "handset": "Phone",
      "tablet": "Tablet",
      "tv": "TV",
      "desktop": "Desktop",
      "unknown": "Unknown device"
    },
    "lastSeen": "Last seen",
    "lastSeenOn": "Last seen on",
    "seconds": ["second", "seconds"],
    "minutes": ["minute", "minutes"],
    "hours": ["hour", "hours"],
    "ago": "ago",
    "monthAbbreviations": [
      "Jan",
      "Feb",
      "Mar",
      "Apr",
      "May",
      "Jun",
      "Jul",
      "Aug",
      "Sep",
      "Oct",
      "Nov",
      "Dec"
    ]
  }
}

Create the public/locales/ru/profile.json file with the following content:

{
  "update": "Сохранить",
  "updated": "Сохранено",
  "menu": "Профиль",
  "setAvatar": "Загрузить фото",
  "changeAvatar": "Изменить фото",
  "deleteAvatar": "Удалить фото",
  "setName": "Указать имя",
  "changeName": "Изменить имя",
  "changePassword": "Изменить пароль",
  "signOut": "Выйти",
  "updateNameModal": {
    "title": "Укажите ваше имя",
    "firstName": {
      "label": "Имя",
      "placeholder": "Введите свое имя"
    },
    "lastName": {
      "label": "Фамилия",
      "placeholder": "Введите свою фамилию"
    }
  },
  "updatePasswordModal": {
    "title": "Измените свой пароль",
    "button": "Изменить",
    "success": "Пароль изменен",
    "currentPassword": {
      "label": "Текущий пароль",
      "placeholder": "Введите текущий пароль"
    },
    "newPassword": {
      "label": "Новый пароль",
      "placeholder": "Введите новый пароль"
    },
    "passwordStrengthNames": {
      "weak": "Слабый",
      "good": "Хороший",
      "strong": "Надежный",
      "powerful": "Мощный"
    }
  },
  "sessions": {
    "title": "Активные сеансы",
    "description": "Каждый раз, когда кто-то входит в ваш аккаунт, создается новый сеанс, который отображается здесь. Завершите все подозрительные сеансы.",
    "current": "Ваш текущий сеанс",
    "other": "Другие сеансы",
    "destroyAllOther": "Завершить все другие сеансы",
    "destroyed": "Сеанс был завершен",
    "destroyedAllOther": "Все другие сеансы были завершены",
    "deviceType": {
      "browser": "Браузер",
      "handset": "Телефон",
      "tablet": "Планшет",
      "tv": "ТВ",
      "desktop": "Компьютер",
      "unknown": "Неизвестное устройство"
    },
    "lastSeen": "Был",
    "lastSeenOn": "Был",
    "seconds": ["секунду", "секунды", "секунд"],
    "minutes": ["минуту", "минуты", "минут"],
    "hours": ["час", "часа", "часов"],
    "ago": "назад",
    "monthAbbreviations": [
      "янв",
      "фев",
      "мар",
      "апр",
      "май",
      "июн",
      "июл",
      "авг",
      "сен",
      "окт",
      "ноя",
      "дек"
    ]
  }
}

Edit your layout like this:

import React, { useState } from 'react';
import {
  Layout as BaseLayout,
  Navigation,
  NavigationItem,
} from '@os-design/core';
import { useLocation, Link } from 'react-router-dom';
import {
  UserAvatarAddon,
  ProfileDrawer,
  ProfileNavigationItem,
  ProfileButton,
  SessionDrawer,
} from '@os-team/profile';
import HomeIcon from './icons/HomeIcon';

const Layout: React.FC = ({ children }) => {
  const { pathname } = useLocation();
  const { t } = useTranslation(['profile']);
  const [profileDrawerVisible, setProfileDrawerVisible] = useState(false);
  const [sessionDrawerVisible, setSessionDrawerVisible] = useState(false);

  return (
    <>
      <BaseLayout hasNavigation hasPageHeader>
        <Navigation
          sideBottom={
            <UserAvatarAddon onClick={() => setProfileDrawerVisible(true)} />
          }
        >
          <NavigationItem
            icon={<HomeIcon />}
            currentPage={pathname === '/home'}
            as={Link}
            to='/home'
          >
            Home
          </NavigationItem>
          <ProfileNavigationItem
            onClick={() => setProfileDrawerVisible(true)}
          />
        </Navigation>

        {children}
      </BaseLayout>

      <ProfileDrawer
        visible={profileDrawerVisible}
        onClose={() => setProfileDrawerVisible(false)}
        actions={
          <ProfileButton onClick={() => setSessionDrawerVisible(true)}>
            {t('profile:sessions.title')}
          </ProfileButton>
        }
      />

      <SessionDrawer
        visible={sessionDrawerVisible}
        onClose={() => setSessionDrawerVisible(false)}
      />
    </>
  );
};

export default Layout;

Note that you need to:

  1. Pass UserAvatarAddon to the sideBottom property of the Navigation. It is used to open the profile drawer if the screen size is greater than or equal to 786px.
  2. Add ProfileNavigationItem. It is used to open the profile drawer if the screen size is less than 786px.
  3. Add ProfileDrawer. It is used to display profile data.

Wrap your app in ProtectedWrapper like this:

import React, { Suspense } from 'react';
import { GlobalStyles } from '@os-design/core';
import { ThemeProvider } from '@os-design/theming';
import { BrowserRouter } from 'react-router-dom';
import { RelayEnvironmentProvider } from 'react-relay/hooks';
import { ProtectedWrapper } from '@os-team/profile';
import ErrorBoundary from '@os-design/error-boundary';
import AppRouter from './AppRouter';
import createRelayEnvironment from './utils/createRelayEnvironment';
import MainLoader from './components/MainLoader';
import Layout from './components/Layout';
import { bucketId } from './utils/getFileUrl';

require('lazysizes');
require('lazysizes/plugins/attrchange/ls.attrchange');
require('./i18next');

const relayEnvironment = createRelayEnvironment();

const App: React.FC = () => (
  <ThemeProvider>
    <GlobalStyles />

    <RelayEnvironmentProvider environment={relayEnvironment}>
      <BrowserRouter>
        <Suspense fallback={<MainLoader />}>
          <ProtectedWrapper bucketId={bucketId}>
            <ErrorBoundary fallback={({ error }) => <p>{error.message}</p>}>
              <Layout>
                <AppRouter />
              </Layout>
            </ErrorBoundary>
          </ProtectedWrapper>
        </Suspense>
      </BrowserRouter>
    </RelayEnvironmentProvider>
  </ThemeProvider>
);

export default App;

Note:

  1. Make sure that your ProtectedWrapper is wrapped with the Suspense component.
  2. Use ErrorBoundary inside the ProtectedWrapper, otherwise, if a user receives an error, the UI will redirect him to the auth page, because the ProtectedWrapper has its own ErrorBoundary that redirects the user.

If you need to get the profile data somewhere in your app, you can use the useProfile hook.

import { useProfile } from '@os-team/profile';

const { id, email, firstName, lastName, avatar } = useProfile();
1.2.0

3 months ago

1.1.16

3 months ago

1.1.15

5 months ago

1.1.14

5 months ago

1.1.9

10 months ago

1.1.8

10 months ago

1.1.12

8 months ago

1.1.11

9 months ago

1.1.10

9 months ago

1.1.13

7 months ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.50

1 year ago

1.0.48

2 years ago

1.0.49

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.45

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.40

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.33

3 years ago

1.0.35

2 years ago

1.0.34

3 years ago

1.0.32

3 years ago

1.0.31

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.29

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.30

3 years ago

1.0.24

3 years ago

1.0.22

3 years ago

1.0.23

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.21

3 years ago

1.0.20

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.12

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago