Skip to content

Commit

Permalink
fix: disable button if I don't have permissions (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
mosoriob authored May 16, 2024
1 parent a469221 commit f5c7485
Showing 1 changed file with 35 additions and 65 deletions.
100 changes: 35 additions & 65 deletions src/tapis-app/Apps/_components/Toolbar/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useNotifications } from 'tapis-app/_components/Notifications';
import { useAppsSelect } from '../AppsContext';
import UploadModal from 'tapis-app/Apps/_components/Toolbar/UploadModal';
import ShareModal from './ShareModal';
import { useTapisConfig } from 'tapis-hooks';

type ToolbarButtonProps = {
text: string;
Expand Down Expand Up @@ -46,79 +47,48 @@ export const ToolbarButton: React.FC<ToolbarButtonProps> = ({
const Toolbar: React.FC = () => {
const [modal, setModal] = useState<string | undefined>(undefined);
const { selectedApps } = useAppsSelect();
const { pathname } = useLocation();
const systemId = pathname.split('/')[2];
const currentPath = pathname.split('/').splice(3).join('/');
const { download } = useDownload();
const { add } = useNotifications();

const { data } = usePermissions({ systemId, path: currentPath });
const permission = data?.result?.permission;

const onDownload = useCallback(() => {
selectedApps.forEach((file) => {
// const params: DownloadStreamParams = {
// systemId,
// path: file.path ?? '',
// destination: file.name ?? 'tapisfile',
// };
// const isZip = file.type === 'dir';
// if (isZip) {
// params.zip = true;
// params.destination = `${params.destination}.zip`;
// add({ icon: 'data-files', message: `Preparing download` });
// params.onStart = (response: Response) => {
// add({ icon: 'data-files', message: `Starting download` });
// };
// }
// download(params, {
// onError: isZip
// ? () => {
// add({
// icon: 'data-files',
// message: `Download failed`,
// status: 'ERROR',
// });
// }
// : undefined,
// });
});
}, [selectedApps, add, download, systemId]);
const { claims } = useTapisConfig();
const isCurrentUser = (username: string) =>
username === claims['tapis/username'];
const hasPermissions: boolean = selectedApps.every((app) =>
app.owner === undefined ? false : isCurrentUser(app.owner)
);

const toggle = () => {
setModal(undefined);
};

return (
<div id="file-operation-toolbar">
{pathname !== '/files' && (
<div className={styles['toolbar-wrapper']}>
<ToolbarButton
text="Create"
icon="add"
disabled={false}
onClick={() => {
setModal('upload');
}}
aria-label="Add"
/>
<ToolbarButton
text="Delete"
icon="trash"
disabled={selectedApps.length === 0}
onClick={() => setModal('delete')}
aria-label="Delete"
/>
<ToolbarButton
text="Share"
icon="globe"
disabled={selectedApps.length === 0}
onClick={() => setModal('share')}
/>
{modal === 'delete' && <DeleteModal toggle={toggle} />}
{modal === 'upload' && <UploadModal toggle={toggle} />}
{modal === 'share' && <ShareModal toggle={toggle} />}
</div>
)}
<div className={styles['toolbar-wrapper']}>
<ToolbarButton
text="Create"
icon="add"
disabled={false}
onClick={() => {
setModal('upload');
}}
aria-label="Add"
/>
<ToolbarButton
text="Delete"
icon="trash"
disabled={selectedApps.length === 0 || !hasPermissions}
onClick={() => setModal('delete')}
aria-label="Delete"
/>
<ToolbarButton
text="Share"
icon="globe"
disabled={selectedApps.length === 0 || !hasPermissions}
onClick={() => setModal('share')}
/>
{modal === 'delete' && <DeleteModal toggle={toggle} />}
{modal === 'upload' && <UploadModal toggle={toggle} />}
{modal === 'share' && <ShareModal toggle={toggle} />}
</div>
</div>
);
};
Expand Down

0 comments on commit f5c7485

Please sign in to comment.