Skip to content

Commit

Permalink
FIX deletion api with version in apis list & refersh table after dele…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
quentinovega committed Jan 28, 2025
1 parent 250451e commit b9bb619
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export const TeamApiSettings = ({ api, currentTeam, versions }: TeamApiSettingsP
}
}

const schema = versions.length > 2 ? { ...confirm, ...next } : { ...confirm }
const schema = versions.length > 2 && api.isDefault ? { ...confirm, ...next } : { ...confirm }
const automaticNextCurrentVersion = versions.length === 2 ? versions.filter(v => v !== api.currentVersion)[0] : undefined

openFormModal({
Expand Down
89 changes: 70 additions & 19 deletions daikoku/javascript/src/components/backoffice/apis/TeamApis.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createColumnHelper } from '@tanstack/react-table';
import { useContext, useEffect, useRef } from 'react';
import { Link } from 'react-router-dom';
import { Link, useNavigate } from 'react-router-dom';
import { toast } from 'sonner';

import { I18nContext, ModalContext, useTeamBackOffice } from '../../../contexts';
Expand All @@ -9,14 +9,16 @@ import * as Services from '../../../services';
import { IApi, ITeamSimple, isError } from '../../../types';
import { Table, TableRef } from '../../inputs';
import { api as API, Can, Spinner, manage, read } from '../../utils';
import { constraints, format, type } from '@maif/react-forms';

export const TeamApis = () => {
const { isLoading, currentTeam, error } = useTeamBackOffice()

const { tenant } = useContext(GlobalContext);

const { translate } = useContext(I18nContext);
const { confirm } = useContext(ModalContext);
const { openFormModal } = useContext(ModalContext);
const navigate = useNavigate();

useEffect(() => {
if (currentTeam && !isError(currentTeam))
Expand All @@ -33,15 +35,15 @@ export const TeamApis = () => {
meta: { style: { textAlign: 'left' } },
cell: (info) => {
const api = info.row.original;
if (api.apis) {
return (
<div className="d-flex flex-row justify-content-between">
<span>{info.getValue()}</span>
<div className="badge badge-custom">API Group</div>
return (
<div className="d-flex flex-row justify-content-between">
<span>{info.getValue()}</span>
<div className='d-flex gap-1'>
{api.apis && <div className="badge badge-custom">{translate('apis.list.apigroup.badge.label')}</div>}
{api.isDefault && <div className="badge badge-custom">{translate('apis.list.currentVersion.badge.label')}</div>}
</div>
);
}
return <div>{info.getValue()}</div>;
</div>
);
},
}),
columnHelper.accessor("smallDescription", {
Expand Down Expand Up @@ -103,16 +105,65 @@ export const TeamApis = () => {
];

const deleteApi = (api: IApi) => {
confirm({ message: translate('delete.api.confirm'), okLabel: translate('Yes') })
.then((ok) => {
if (ok) {
Services.deleteTeamApi((currentTeam as ITeamSimple)._id, api._id)
.then(() => {
toast.success(translate({ key: 'delete.api.success', replacements: [api.name] }));
table.current?.update();
});

const team = currentTeam as ITeamSimple
Services.getAllApiVersions(team._id, api._id)
.then(versions => {
const confirm = {
confirm: {
type: type.string,
label: translate({ key: 'delete.item.confirm.modal.confirm.label', replacements: [api.name] }),
constraints: [
constraints.oneOf(
[api.name],
translate({ key: 'constraints.type.api.name', replacements: [api.name] })
),
],
},
}
});

const next = {
next: {
type: type.string,
label: translate("delete.api.confirm.modal.description.next.label"),
help: translate('delete.api.confirm.modal.description.next.help'),
format: format.select,
options: versions.filter(v => v !== api.currentVersion),
constraints: [
constraints.required(translate("constraints.required.value"))
]
}
}

const schema = versions.length > 2 && api.isDefault ? { ...confirm, ...next } : { ...confirm }
const automaticNextCurrentVersion = versions.length === 2 ? versions.filter(v => v !== api.currentVersion)[0] : undefined

//fixme: refresh le tableau apres suppression/update
return openFormModal({
title: translate('Confirm'),
description: <div className="alert alert-danger" role="alert">
<h4 className="alert-heading">{translate('Warning')}</h4>
<p>{translate("delete.api.confirm.modal.description.1")}</p>
<ul>
<li>{translate("delete.api.confirm.modal.description.2")}</li>
</ul>
{automaticNextCurrentVersion && <strong>{translate({ key: 'delete.api.confirm.modal.description.next.version', replacements: [automaticNextCurrentVersion] })}</strong>}
</div>,
schema: schema,
onSubmit: ({ next }) => {
Services.deleteTeamApi(team._id, api._id, next)
.then((r) => {
if (isError(r)) {
toast.error(r.error)
} else {
table.current?.update();
toast.success(translate('deletion successful'))
}
})
},
actionLabel: translate('Confirm')
})
})
};

if (isLoading) {
Expand Down
4 changes: 3 additions & 1 deletion daikoku/javascript/src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1421,5 +1421,7 @@
"delete.item.confirm.modal.confirm.label": "Type %s to confirm deletion",
"delete.api.confirm.modal.description.next.version": "Version %s will automatically be set as the API's current version",
"delete.api.confirm.modal.description.next.label": "New current version",
"delete.api.confirm.modal.description.next.help": "The version being deleted is the API's current version. Please select a replacement version"
"delete.api.confirm.modal.description.next.help": "The version being deleted is the API's current version. Please select a replacement version",
"apis.list.apigroup.badge.label": "API group",
"apis.list.currentVersion.badge.label": "Current version"
}
4 changes: 3 additions & 1 deletion daikoku/javascript/src/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -1422,5 +1422,7 @@
"delete.item.confirm.modal.confirm.label": "Saisissez %s pour confirmer la suppression",
"delete.api.confirm.modal.description.next.version": "La version %s sera automatiquement définie comme la version courante de l'API",
"delete.api.confirm.modal.description.next.label": "Nouvelle version courante",
"delete.api.confirm.modal.description.next.help": "La version que vous supprimez est la version courante de l'API. Veuillez sélectionner une version pour la remplacer"
"delete.api.confirm.modal.description.next.help": "La version que vous supprimez est la version courante de l'API. Veuillez sélectionner une version pour la remplacer",
"apis.list.apigroup.badge.label": "API group",
"apis.list.currentVersion.badge.label": "Version courante"
}

0 comments on commit b9bb619

Please sign in to comment.