✨(frontend) create hook useTransRole
Create the hook useTransRole to get a translated role.
This commit is contained in:
@@ -0,0 +1 @@
|
|||||||
|
export * from './useTransRole';
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
|
import { Role } from '../types';
|
||||||
|
|
||||||
|
export const useTransRole = () => {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
const translatedRoles = {
|
||||||
|
[Role.ADMIN]: t('Administrator'),
|
||||||
|
[Role.READER]: t('Reader'),
|
||||||
|
[Role.OWNER]: t('Owner'),
|
||||||
|
[Role.EDITOR]: t('Editor'),
|
||||||
|
};
|
||||||
|
|
||||||
|
const transRole = (role: Role) => {
|
||||||
|
return translatedRoles[role];
|
||||||
|
};
|
||||||
|
|
||||||
|
return transRole;
|
||||||
|
};
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
export * from './api';
|
export * from './api';
|
||||||
export * from './components';
|
export * from './components';
|
||||||
|
export * from './hooks';
|
||||||
export * from './types';
|
export * from './types';
|
||||||
export * from './utils';
|
export * from './utils';
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import { Radio, RadioGroup } from '@openfun/cunningham-react';
|
import { Radio, RadioGroup } from '@openfun/cunningham-react';
|
||||||
import { useTranslation } from 'react-i18next';
|
|
||||||
|
|
||||||
import { Role } from '@/features/docs/doc-management';
|
import { Role, useTransRole } from '@/features/docs/doc-management';
|
||||||
|
|
||||||
interface ChooseRoleProps {
|
interface ChooseRoleProps {
|
||||||
currentRole: Role;
|
currentRole: Role;
|
||||||
@@ -16,21 +15,14 @@ export const ChooseRole = ({
|
|||||||
currentRole,
|
currentRole,
|
||||||
setRole,
|
setRole,
|
||||||
}: ChooseRoleProps) => {
|
}: ChooseRoleProps) => {
|
||||||
const { t } = useTranslation();
|
const transRole = useTransRole();
|
||||||
|
|
||||||
const translatedRoles = {
|
|
||||||
[Role.ADMIN]: t('Administrator'),
|
|
||||||
[Role.READER]: t('Reader'),
|
|
||||||
[Role.OWNER]: t('Owner'),
|
|
||||||
[Role.EDITOR]: t('Editor'),
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<RadioGroup>
|
<RadioGroup>
|
||||||
{Object.values(Role).map((role) => (
|
{Object.values(Role).map((role) => (
|
||||||
<Radio
|
<Radio
|
||||||
key={role}
|
key={role}
|
||||||
label={translatedRoles[role]}
|
label={transRole(role)}
|
||||||
value={role}
|
value={role}
|
||||||
name="role"
|
name="role"
|
||||||
onChange={(evt) => setRole(evt.target.value as Role)}
|
onChange={(evt) => setRole(evt.target.value as Role)}
|
||||||
|
|||||||
@@ -3,7 +3,11 @@ import React, { useEffect, useState } from 'react';
|
|||||||
import { useTranslation } from 'react-i18next';
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
import { Card, TextErrors } from '@/components';
|
import { Card, TextErrors } from '@/components';
|
||||||
import { Doc, Role, currentDocRole } from '@/features/docs/doc-management';
|
import {
|
||||||
|
Doc,
|
||||||
|
currentDocRole,
|
||||||
|
useTransRole,
|
||||||
|
} from '@/features/docs/doc-management/';
|
||||||
|
|
||||||
import { useDocAccesses } from '../api';
|
import { useDocAccesses } from '../api';
|
||||||
import { PAGE_SIZE } from '../conf';
|
import { PAGE_SIZE } from '../conf';
|
||||||
@@ -43,6 +47,7 @@ function formatSortModel(
|
|||||||
|
|
||||||
export const MemberGrid = ({ doc }: MemberGridProps) => {
|
export const MemberGrid = ({ doc }: MemberGridProps) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
const transRole = useTransRole();
|
||||||
const pagination = usePagination({
|
const pagination = usePagination({
|
||||||
pageSize: PAGE_SIZE,
|
pageSize: PAGE_SIZE,
|
||||||
});
|
});
|
||||||
@@ -57,13 +62,6 @@ export const MemberGrid = ({ doc }: MemberGridProps) => {
|
|||||||
ordering,
|
ordering,
|
||||||
});
|
});
|
||||||
|
|
||||||
const translatedRoles = {
|
|
||||||
[Role.ADMIN]: t('Administrator'),
|
|
||||||
[Role.READER]: t('Reader'),
|
|
||||||
[Role.OWNER]: t('Owner'),
|
|
||||||
[Role.EDITOR]: t('Editor'),
|
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Bug occurs from the Cunningham Datagrid component, when applying sorting
|
* Bug occurs from the Cunningham Datagrid component, when applying sorting
|
||||||
* on null values. Sanitize empty values to ensure consistent sorting functionality.
|
* on null values. Sanitize empty values to ensure consistent sorting functionality.
|
||||||
@@ -71,7 +69,7 @@ export const MemberGrid = ({ doc }: MemberGridProps) => {
|
|||||||
const accesses =
|
const accesses =
|
||||||
data?.results?.map((access) => ({
|
data?.results?.map((access) => ({
|
||||||
...access,
|
...access,
|
||||||
localizedRole: translatedRoles[access.role],
|
localizedRole: transRole(access.role),
|
||||||
email: access.user.email,
|
email: access.user.email,
|
||||||
})) || [];
|
})) || [];
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user