Skip to content

Commit a5b592a

Browse files
committed
FIX #600 - remove duplicate values with team form && add some translations
1 parent c72d015 commit a5b592a

File tree

6 files changed

+92
-66
lines changed

6 files changed

+92
-66
lines changed

daikoku/javascript/src/components/adminbackoffice/teams/TeamList.tsx

+84-62
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Select, { components } from 'react-select';
1212
import { ModalContext, useTenantBackOffice } from '../../../contexts';
1313
import { I18nContext } from '../../../core';
1414
import * as Services from '../../../services';
15-
import { IOtoroshiSettings, ITeamFullGql, ITeamSimple, ResponseError, isError } from '../../../types';
15+
import { IAuthorizedEntities, IOtoroshiSettings, ISimpleOtoroshiSettings, ITeamFullGql, ITeamSimple, ResponseError, isError } from '../../../types';
1616
import { teamSchema } from '../../backoffice/teams/TeamEdit';
1717
import { AvatarWithAction, Can, tenant as TENANT, manage } from '../../utils';
1818

@@ -65,14 +65,90 @@ export const TeamList = () => {
6565

6666
const navigate = useNavigate();
6767

68+
const teamSchemaForAdmin = (team: ITeamSimple, _otoroshis: ISimpleOtoroshiSettings[]) => ({
69+
...teamSchema(team, translate),
70+
apisCreationPermission: {
71+
type: type.bool,
72+
defaultValue: false,
73+
label: translate('APIs creation permission'),
74+
help: translate('apisCreationPermission.help'),
75+
visible: !!tenant.creationSecurity
76+
},
77+
metadata: {
78+
type: type.object,
79+
label: translate('Metadata'),
80+
},
81+
authorizedOtoroshiEntities: {
82+
type: type.object,
83+
array: true,
84+
label: translate('authorizedOtoroshiEntities'),
85+
format: format.form,
86+
schema: {
87+
otoroshiSettingsId: {
88+
type: type.string,
89+
format: format.select,
90+
label: translate('Otoroshi instances'),
91+
optionsFrom: () => Promise.resolve(_otoroshis),
92+
transformer: (s: IOtoroshiSettings) => ({
93+
label: s.url,
94+
value: s._id
95+
}),
96+
constraints: [
97+
constraints.required(translate('constraints.required.value'))
98+
]
99+
},
100+
authorizedEntities: {
101+
type: type.object,
102+
visible: (props) => {
103+
return !!props.rawValues.authorizedOtoroshiEntities[props.informations?.parent?.index || 0].value.otoroshiSettingsId
104+
},
105+
deps: ['authorizedOtoroshiEntities.otoroshiSettingsId'],
106+
render: (props) => OtoroshiEntitiesSelector({ ...props, translate, targetKey: "authorizedOtoroshiEntities" }),
107+
label: translate('Authorized entities'),
108+
placeholder: translate('Authorized.entities.placeholder'),
109+
help: translate('authorized.entities.help'),
110+
defaultValue: { routes: [], services: [], groups: [] }
111+
},
112+
}
113+
}
114+
})
115+
116+
const sanitizeTeamAuthorizedEntities = (team: ITeamSimple) => {
117+
return Promise.resolve({
118+
...team,
119+
authorizedOtoroshiEntities: team.authorizedOtoroshiEntities.reduce<Array<{ otoroshiSettingsId: string, authorizedEntities: IAuthorizedEntities }>>((acc, curr) => {
120+
if (acc.some(x => x.otoroshiSettingsId === curr.otoroshiSettingsId)) {
121+
const authorizedEntities = acc.find(x => x.otoroshiSettingsId === curr.otoroshiSettingsId)!.authorizedEntities
122+
return [
123+
...acc.filter(x => x.otoroshiSettingsId !== curr.otoroshiSettingsId),
124+
{
125+
otoroshiSettingsId: curr.otoroshiSettingsId,
126+
authorizedEntities: {
127+
groups: [...new Set([...curr.authorizedEntities.groups, ...authorizedEntities.groups])],
128+
services: [...new Set([...curr.authorizedEntities.services, ...authorizedEntities.services])],
129+
routes: [...new Set([...curr.authorizedEntities.routes, ...authorizedEntities.routes])]
130+
}
131+
}]
132+
} else {
133+
return [...acc, curr]
134+
}
135+
}, [])
136+
})
137+
}
138+
68139
const createNewTeam = () => {
69-
Services.fetchNewTeam()
70-
.then((newTeam) => {
140+
Promise.all([
141+
Services.fetchNewTeam(),
142+
Services.allSimpleOtoroshis(tenant._id)
143+
])
144+
.then(([newTeam, otoroshis]) => {
145+
const _otoroshis = isError(otoroshis) ? [] : otoroshis
71146
openFormModal({
72147
title: translate('Create a new team'),
73148
actionLabel: translate('Create'),
74-
schema: teamSchema(newTeam, translate),
75-
onSubmit: (data: ITeamSimple) => Services.createTeam(data)
149+
schema: teamSchemaForAdmin(newTeam, _otoroshis),
150+
onSubmit: (data: ITeamSimple) => sanitizeTeamAuthorizedEntities(data)
151+
.then(team => Services.createTeam(team))
76152
.then(r => {
77153
if (r.error) {
78154
toastr.error(translate('Error'), r.error)
@@ -144,62 +220,10 @@ export const TeamList = () => {
144220
openFormModal({
145221
title: translate('Update team'),
146222
actionLabel: translate('Update'),
147-
schema: {
148-
...teamSchema(team, translate),
149-
apisCreationPermission: {
150-
type: type.bool,
151-
defaultValue: false,
152-
label: translate('APIs creation permission'),
153-
help: translate('apisCreationPermission.help'),
154-
visible: !!tenant.creationSecurity
155-
},
156-
metadata: {
157-
type: type.object,
158-
label: translate('Metadata'),
159-
},
160-
authorizedOtoroshiEntities: {
161-
type: type.object,
162-
array: true,
163-
label: translate('authorizedOtoroshiEntities'),
164-
format: format.form,
165-
schema: {
166-
otoroshiSettingsId: {
167-
type: type.string,
168-
format: format.select,
169-
label: translate('Otoroshi instances'),
170-
optionsFrom: () => {
171-
// const authorizedOto = props.getValue("authorizedOtoroshiEntities").map((o) => o.value.otoroshiSettingsId)
172-
// console.debug(otoroshis.filter(o => !authorizedOto.includes(o._id)), )
173-
return Promise.resolve(_otoroshis)
174-
},
175-
transformer: (s: IOtoroshiSettings) => ({
176-
label: s.url,
177-
value: s._id
178-
}),
179-
constraints: [
180-
constraints.required()
181-
]
182-
},
183-
authorizedEntities: {
184-
type: type.object,
185-
visible: (props) => {
186-
return !!props.rawValues.authorizedOtoroshiEntities[props.informations?.parent?.index || 0].value.otoroshiSettingsId
187-
},
188-
deps: ['authorizedOtoroshiEntities.otoroshiSettingsId'],
189-
render: (props) => OtoroshiEntitiesSelector({ ...props, translate, targetKey: "authorizedOtoroshiEntities" }),
190-
label: translate('Authorized entities'),
191-
placeholder: translate('Authorized.entities.placeholder'),
192-
help: translate('authorized.entities.help'),
193-
defaultValue: { routes: [], services: [], groups: [] }
194-
},
195-
},
196-
constraints: [
197-
constraints.max(_otoroshis.length, "oops")
198-
]
199-
}
200-
},
223+
schema: teamSchemaForAdmin(team, _otoroshis),
201224
onSubmit: (teamToUpdate) => {
202-
return Services.updateTeam(teamToUpdate)
225+
return sanitizeTeamAuthorizedEntities(teamToUpdate)
226+
.then(teamToUpdate => Services.updateTeam(teamToUpdate))
203227
.then(r => {
204228
if (r.error) {
205229
toastr.error(translate('Error'), r.error)
@@ -356,15 +380,13 @@ const OtoroshiEntitiesSelector = ({
356380

357381
useEffect(() => {
358382
if (groups && services && routes) {
359-
console.log({ groups, services, routes })
360383
setLoading(false);
361384
}
362385
}, [services, groups, routes]);
363386

364387
useEffect(() => {
365388
if (!!groups && !!services && !!routes && !!rawValues.authorizedOtoroshiEntities[informations?.parent?.index || 0].value) {
366389
const v = rawValues.authorizedOtoroshiEntities[informations?.parent?.index || 0].value
367-
console.log({ v })
368390
setValue([
369391
...v.authorizedEntities.groups.map((authGroup: any) => (groups as any).find((g: any) => g.value === authGroup)),
370392
...(v.authorizedEntities.services || []).map((authService: any) => (services as any).find((g: any) => g.value === authService)),

daikoku/javascript/src/components/frontend/unauthenticated/UnauthenticatedTopBar.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export const UnauthenticatedTopBar = () => {
99
return (
1010
<div className="dropdown-menu dropdown-menu-right">
1111
<a className="dropdown-item" href="/signup">
12-
<i className="fas fa-sign-out-alt" /> Sign up
12+
<i className="fas fa-sign-out-alt" />
13+
Sign up
1314
</a>
1415
</div>
1516
);

daikoku/javascript/src/components/utils/sidebar/panels/GuestPanel.tsx

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export const GuestPanel = () => {
2525
password: {
2626
type: type.string,
2727
label: translate('password.label'),
28+
placeholder: translate('password.label'),
2829
format: format.password,
2930
constraints: [constraints.required(translate('constraints.required.password'))],
3031
},
@@ -73,10 +74,10 @@ export const GuestPanel = () => {
7374
);
7475
}}
7576
/>
76-
<div className="d-flex flex-row mt-3">
77+
<div className="d-flex flex-row justify-content-between mt-3">
7778
{loginProvider == 'Local' && (
7879
<a className="text-center" href="/signup">
79-
{' '}Create an account.
80+
<Translation i18nkey="create.account.link.label" />
8081
</a>
8182
)}
8283
<a className="text-center" href="/reset">

daikoku/javascript/src/locales/en/translation.json

+1
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@
549549
"Username": "Username",
550550
"Login": "Login",
551551
"Forgot your password ?": "Forgot your password?",
552+
"create.account.link.label": "Create an account",
552553
"Api saved": "API saved",
553554
"api.created.success": "API %s created",
554555
"group.created.success": "API group %s created",

daikoku/javascript/src/locales/fr/translation.json

+1
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@
541541
"Username": "Nom d'utilisateur",
542542
"Login": "Se connecter",
543543
"Forgot your password ?": "Mot de passe oublié ?",
544+
"create.account.link.label": "Créer un compte",
544545
"Api saved": "API sauvegardée",
545546
"api.created.success": "API %s créée",
546547
"group.created.success": "API groupe %s créée",

daikoku/javascript/src/types/team.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export interface ITeamSimple {
1919
apiKeyVisibility: TeamPermission;
2020
apisCreationPermission?: boolean;
2121
verified: boolean;
22-
authorizedEntities: Array<{otoroshiSettings: string, authorizedEntities: IAuthorizedEntities}>
22+
authorizedOtoroshiEntities: Array<{otoroshiSettingsId: string, authorizedEntities: IAuthorizedEntities}>
2323
}
2424

2525
export interface ITeamVisibility {

0 commit comments

Comments
 (0)