diff --git a/daikoku/app/controllers/EntitiesController.scala b/daikoku/app/controllers/EntitiesController.scala index 6a52dc9c8..c828500cf 100644 --- a/daikoku/app/controllers/EntitiesController.scala +++ b/daikoku/app/controllers/EntitiesController.scala @@ -154,7 +154,7 @@ class EntitiesController( id = ApiId(IdGenerator.token(32)), tenant = ctx.tenant.id, team = TeamId("none"), - name = "New API", + name = "New API group", apis = Some(Set.empty), smallDescription = "A new API group", description = "A new API group", diff --git a/daikoku/javascript/src/components/backoffice/apis/TeamApiGroup.tsx b/daikoku/javascript/src/components/backoffice/apis/TeamApiGroup.tsx index 61d1b730b..6b5028d43 100644 --- a/daikoku/javascript/src/components/backoffice/apis/TeamApiGroup.tsx +++ b/daikoku/javascript/src/components/backoffice/apis/TeamApiGroup.tsx @@ -46,247 +46,249 @@ export const TeamApiGroup = () => { -const save = (group: IApi) => { - if (creation) { - return Services.createTeamApi(currentTeam!._id, group).then((createdGroup) => { - if (createdGroup.error) { - toast.error(translate(createdGroup.error)); - return createdGroup; - } else if (createdGroup.name) { - toast.success(translate({ key: 'group.created.success', replacements: [createdGroup.name] }) - ); + const save = (group: IApi) => { + if (creation) { + console.debug({ group }) + return Services.createTeamApi(currentTeam!._id, group) + .then((createdGroup) => { + if (createdGroup.error) { + toast.error(translate(createdGroup.error)); + return createdGroup; + } else if (createdGroup.name) { + toast.success(translate({ key: 'group.created.success', replacements: [createdGroup.name] }) + ); - navigate(`/${currentTeam!._humanReadableId}/settings/apigroups/${createdGroup._humanReadableId}/infos`); - } - }); - } else { - return Services.saveTeamApiWithId( - currentTeam!._id, - group, - group.currentVersion, - group._humanReadableId - ).then((res) => { - if (isError(res)) { - toast.error(translate(res.error)); - return res; - } else { - toast.success(translate('Group saved')); - reloadApiGroup() + navigate(`/${currentTeam!._humanReadableId}/settings/apigroups/${createdGroup._humanReadableId}/infos`); + } + }); + } else { + return Services.saveTeamApiWithId( + currentTeam!._id, + group, + group.currentVersion, + group._humanReadableId + ).then((res) => { + if (isError(res)) { + toast.error(translate(res.error)); + return res; + } else { + toast.success(translate('Group saved')); + reloadApiGroup() - if (res._humanReadableId !== group._humanReadableId) { - navigate(`/${currentTeam!._humanReadableId}/settings/apigroups/${res._humanReadableId}/infos`); + if (res._humanReadableId !== group._humanReadableId) { + navigate(`/${currentTeam!._humanReadableId}/settings/apigroups/${res._humanReadableId}/infos`); + } } - } - }); - } -}; + }); + } + }; -const setDefaultPlan = (apiGroup: IApi, plan: IUsagePlan, team: ITeamSimple) => { - if (apiGroup && apiGroup.defaultUsagePlan !== plan._id && plan.visibility !== 'Private') { - const updatedApi = { ...apiGroup, defaultUsagePlan: plan._id } - Services.saveTeamApiWithId( - team._id, - updatedApi, - apiGroup.currentVersion, - updatedApi._humanReadableId - ).then((response) => { - if (isError(response)) { - toast.error(translate(response.error)); - } else { - reloadApiGroup() - } - }) + const setDefaultPlan = (apiGroup: IApi, plan: IUsagePlan, team: ITeamSimple) => { + if (apiGroup && apiGroup.defaultUsagePlan !== plan._id && plan.visibility !== 'Private') { + const updatedApi = { ...apiGroup, defaultUsagePlan: plan._id } + Services.saveTeamApiWithId( + team._id, + updatedApi, + apiGroup.currentVersion, + updatedApi._humanReadableId + ).then((response) => { + if (isError(response)) { + toast.error(translate(response.error)); + } else { + reloadApiGroup() + } + }) + } } -} -const { translate } = useContext(I18nContext); -const { alert } = useContext(ModalContext); + const { translate } = useContext(I18nContext); + const { alert } = useContext(ModalContext); -const schema = (apiGroup: IApi, team: ITeamSimple): ({ [key: string]: any }) => ({ - name: { - type: type.string, - label: translate('Name'), - placeholder: translate('Name'), - constraints: [ - constraints.required(translate('constraints.required.name')), - constraints.test('name_already_exist', translate('api.already.exists'), (name, context) => Services.checkIfApiNameIsUnique(name, context.parent._id).then((r) => !r.exists)), - ], - }, - smallDescription: { - type: type.string, - format: format.text, - label: translate('Small desc.'), - }, - description: { - type: type.string, - format: format.markdown, - label: translate('Description'), - }, - state: { - type: type.string, - format: format.buttonsSelect, - label: translate('State'), - options: [ - { label: translate('Created'), value: 'created' }, - { label: translate('Published'), value: 'published' }, - { label: translate('Deprecated'), value: 'deprecated' }, - { label: translate('Blocked'), value: 'blocked' }], - defaultValue: 'created', - }, - tags: { - type: type.string, - array: true, - label: translate('Tags'), - expert: true, - }, - categories: { - type: type.string, - format: format.select, - isMulti: true, - createOption: true, - label: translate('Categories'), - optionsFrom: '/api/categories', - transformer: (t: string) => ({ - label: t, - value: t - }), - expert: true, - }, - visibility: { - type: type.string, - format: format.buttonsSelect, - label: translate('Visibility'), - options: [ - { label: translate('Public'), value: 'Public' }, - { label: translate('Private'), value: 'Private' }, - { - label: translate('PublicWithAuthorizations'), - value: 'PublicWithAuthorizations', + const schema = (apiGroup: IApi, team: ITeamSimple): ({ [key: string]: any }) => ({ + name: { + type: type.string, + label: translate('Name'), + placeholder: translate('Name'), + constraints: [ + constraints.required(translate('constraints.required.name')), + constraints.test('name_already_exist', translate('api.already.exists'), (name, context) => Services.checkIfApiNameIsUnique(name, context.parent._id).then((r) => !r.exists)), + ], + }, + smallDescription: { + type: type.string, + format: format.text, + label: translate('Small desc.'), + }, + description: { + type: type.string, + format: format.markdown, + label: translate('Description'), + }, + state: { + type: type.string, + format: format.buttonsSelect, + label: translate('State'), + options: [ + { label: translate('Created'), value: 'created' }, + { label: translate('Published'), value: 'published' }, + { label: translate('Deprecated'), value: 'deprecated' }, + { label: translate('Blocked'), value: 'blocked' }], + defaultValue: 'created', + }, + tags: { + type: type.string, + array: true, + label: translate('Tags'), + expert: true, + }, + categories: { + type: type.string, + format: format.select, + isMulti: true, + createOption: true, + label: translate('Categories'), + optionsFrom: '/api/categories', + transformer: (t: string) => ({ + label: t, + value: t + }), + expert: true, + }, + visibility: { + type: type.string, + format: format.buttonsSelect, + label: translate('Visibility'), + options: [ + { label: translate('Public'), value: 'Public' }, + { label: translate('Private'), value: 'Private' }, + { + label: translate('PublicWithAuthorizations'), + value: 'PublicWithAuthorizations', + }, + ], + }, + authorizedTeams: { + type: type.string, + format: format.select, + isMulti: true, + defaultValue: [], + visible: { + ref: 'visibility', + test: (v: string) => v !== 'Public', }, - ], - }, - authorizedTeams: { - type: type.string, - format: format.select, - isMulti: true, - defaultValue: [], - visible: { - ref: 'visibility', - test: (v: string) => v !== 'Public', + label: translate('Authorized teams'), + optionsFrom: '/api/me/teams', + transformer: (t: ITeamSimple) => ({ + label: t.name, + value: t._id + }), }, - label: translate('Authorized teams'), - optionsFrom: '/api/me/teams', - transformer: (t: ITeamSimple) => ({ - label: t.name, - value: t._id - }), - }, - apis: { - type: type.string, - label: translate({ key: 'API', plural: true }), - format: format.select, - isMulti: true, - optionsFrom: () => Services.teamApis(team._id) - .then((apis) => { - if (!isError(apis)) { - return apis.filter((api) => api._id !== apiGroup?._id && !api.apis) - } + apis: { + type: type.string, + label: translate({ key: 'API', plural: true }), + format: format.select, + isMulti: true, + optionsFrom: () => Services.teamApis(team._id) + .then((apis) => { + if (!isError(apis)) { + return apis.filter((api) => api._id !== apiGroup?._id && !api.apis) + } + }), + transformer: (api) => ({ + label: `${api.name} - ${api.currentVersion}`, + value: api._id }), - transformer: (api) => ({ - label: `${api.name} - ${api.currentVersion}`, - value: api._id - }), - }, -}); + }, + }); -const simpleOrExpertMode = (entry: string, expert: boolean) => { - return !!expert || !schema[entry]?.expert; -}; -const flow = [ - { - label: translate('Basic.informations'), - flow: ['name', 'state', 'smallDescription', 'apis'].filter((entry) => - simpleOrExpertMode(entry, expertMode) - ), - collapsed: false, - }, - { - label: translate('Description'), - flow: ['description'], - collapsed: true, - }, - { - label: translate('Tags and categories'), - flow: ['tags', 'categories'].filter((entry) => simpleOrExpertMode(entry, expertMode)), - collapsed: true, - }, - { - label: translate('Visibility'), - flow: ['visibility', 'authorizedTeams'].filter((entry) => - simpleOrExpertMode(entry, expertMode) - ), - collapsed: true, - }, -]; + const simpleOrExpertMode = (entry: string, expert: boolean) => { + return !!expert || !schema[entry]?.expert; + }; + const flow = [ + { + label: translate('Basic.informations'), + flow: ['name', 'state', 'smallDescription', 'apis'].filter((entry) => + simpleOrExpertMode(entry, expertMode) + ), + collapsed: false, + }, + { + label: translate('Description'), + flow: ['description'], + collapsed: true, + }, + { + label: translate('Tags and categories'), + flow: ['tags', 'categories'].filter((entry) => simpleOrExpertMode(entry, expertMode)), + collapsed: true, + }, + { + label: translate('Visibility'), + flow: ['visibility', 'authorizedTeams'].filter((entry) => + simpleOrExpertMode(entry, expertMode) + ), + collapsed: true, + }, + ]; -const { tab } = params; + const { tab } = params; -if (!creation && isLoading) { - return ; -} else if ((creation || !!apiGroup) && currentTeam) { - const _apiGroup = (creation || apiGroup) as IApi - return ( - -
- {creation ? (

{_apiGroup.name}

) : (
-

{_apiGroup.name}{additionalHeader ? ` - ${additionalHeader}` : ''}

-
)} - -
-
-
-
- {params.tab === 'infos' && (
-
-
)} - {params.tab === 'plans' && (
- setDefaultPlan(_apiGroup, plan, currentTeam)} - creation={!!creation} - expertMode={expertMode} - injectSubMenu={(component) => addMenu({ - blocks: { - links: { links: { plans: { childs: { menu: { component } } } } }, - }, - })} - openApiSelectModal={() => alert({ message: 'oops' })} - setHeader={(planName) => setAdditionalHeader(planName)} /> + if (!creation && isLoading) { + return ; + } else if ((creation || !!apiGroup) && currentTeam) { + const _apiGroup = (creation || apiGroup) as IApi + return ( + +
+ {creation ? (

{_apiGroup.name}

) : (
+

{_apiGroup.name}{additionalHeader ? ` - ${additionalHeader}` : ''}

+
)} + +
+
+
+
+ {params.tab === 'infos' && (
+ +
)} + {params.tab === 'plans' && (
+ setDefaultPlan(_apiGroup, plan, currentTeam)} + creation={!!creation} + expertMode={expertMode} + injectSubMenu={(component) => addMenu({ + blocks: { + links: { links: { plans: { childs: { menu: { component } } } } }, + }, + })} + openApiSelectModal={() => alert({ message: 'oops' })} + setHeader={(planName) => setAdditionalHeader(planName)} /> -
)} - {tab === 'settings' && } - {tab === 'stats' && !match && } - {tab === 'stats' && match && match.params.planId && ()} - {tab === 'subscriptions' && } {/* FIXME: a props APIGROUP has been removed...maybe add it in team api sub component */} +
)} + {tab === 'settings' && } + {tab === 'stats' && !match && } + {tab === 'stats' && match && match.params.planId && ()} + {tab === 'subscriptions' && } {/* FIXME: a props APIGROUP has been removed...maybe add it in team api sub component */} +
-
- - ); -} else { - return
Error while fetching api group details
-} + + ); + } else { + return
Error while fetching api group details
+ } };