Skip to content

Commit

Permalink
Transition processes page to server components
Browse files Browse the repository at this point in the history
  • Loading branch information
OhKai committed Dec 9, 2023
1 parent 5ec2e2e commit dccb04a
Show file tree
Hide file tree
Showing 48 changed files with 3,784 additions and 218 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Auth from '@/lib/serverAuthComponents';
import Auth from '@/components/auth';
import GeneralSettingsPage from './general-settings-page';

export default Auth(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Auth from '@/lib/serverAuthComponents';
import Auth from '@/components/auth';
import RolePage from './role-page';

export default Auth(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { FC, ReactNode, useEffect, useState } from 'react';
import dayjs from 'dayjs';
import germanLocale from 'antd/es/date-picker/locale/de_DE';
import { useRouter } from 'next/navigation';
import { AuthCan } from '@/lib/clientAuthComponents';
import { AuthCan } from '@/components/auth-can';

type PostRoleKeys = 'name' | 'description' | 'expiration';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Auth from '@/lib/serverAuthComponents';
import Auth from '@/components/auth';
import RolesPage from './role-page';

export default Auth(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { AuthCan } from '@/lib/clientAuthComponents';
import { AuthCan } from '@/components/auth-can';
import { ApiRequestBody, usePostAsset } from '@/lib/fetch-data';
import { PlusOutlined } from '@ant-design/icons';
import { Button, Form, App, Input, Modal } from 'antd';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Auth from '@/lib/serverAuthComponents';
import Auth from '@/components/auth';
import UsersPage from './users-page';

export default Auth(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import useModelerStateStore from '@/lib/use-modeler-state-store';
import { createNewProcessVersion } from '@/lib/helpers/processVersioning';
import VersionCreationButton from '@/components/version-creation-button';
import ProcessCreationButton from '@/components/process-creation-button';
import { AuthCan } from '@/lib/clientAuthComponents';
import { AuthCan } from '@/components/auth-can';

type ProcessProps = {
params: { processId: string };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Auth from '@/lib/serverAuthComponents';
import Auth from '@/components/auth';
import Processes from './_page';

export default Auth(
Expand Down
14 changes: 14 additions & 0 deletions src/management-system-v2/app/(dashboard)/processes/loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import Content from '@/components/content';
import { Skeleton, Space } from 'antd';

const ProcessesSkeleton = async () => {
return (
<Content title="Processes">
<Space direction="vertical" size="large" style={{ display: 'flex', height: '100%' }}>
<Skeleton active />
</Space>
</Content>
);
};

export default ProcessesSkeleton;
12 changes: 8 additions & 4 deletions src/management-system-v2/app/(dashboard)/processes/page.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { FC } from 'react';
import Processes from '@/components/processes';
import Content from '@/components/content';
import { Result, Space } from 'antd';
import NotLoggedInFallback from './not-logged-in-fallback';
import Auth from '@/lib/serverAuthComponents';
import { getProcesses } from '@/lib/data/legacy/process';
import Auth, { getCurrentUser } from '@/components/auth';

const ProcessesPage = async () => {
const { ability } = await getCurrentUser();
const processes = await getProcesses(ability);
//await new Promise((resolve) => setTimeout(resolve, 10000));

const ProcessesPage: FC = () => {
return (
<Content title="Processes">
<Space direction="vertical" size="large" style={{ display: 'flex', height: '100%' }}>
<Processes />
<Processes processes={processes} />
</Space>
</Content>
);
Expand Down
2 changes: 1 addition & 1 deletion src/management-system-v2/app/(dashboard)/profile/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Auth from '@/lib/serverAuthComponents';
import Auth from '@/components/auth';
import UserProfile from './user-profile';
import Content from '@/components/content';

Expand Down
2 changes: 1 addition & 1 deletion src/management-system-v2/app/(dashboard)/projects/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { FC } from 'react';
import ProjectStats from './project-stats';
import Content from '@/components/content';
import { Space } from 'antd';
import Auth from '@/lib/serverAuthComponents';
import Auth from '@/components/auth';

const Projects: FC = () => {
return (
Expand Down
2 changes: 1 addition & 1 deletion src/management-system-v2/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
import { App as AntDesignApp } from 'antd';
import { SessionProvider } from 'next-auth/react';
import { FetchAbility } from '@/lib/clientAuthComponents';
import { FetchAbility } from './auth-can';

const queryClient = new QueryClient();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use client';

import { ReactElement, ReactNode, useEffect, useMemo, FC, PropsWithChildren } from 'react';
import { useAbilityStore } from './abilityStore';
import { useAbilityStore } from '@/lib/abilityStore';
import { useSession } from 'next-auth/react';
import { Route } from 'next';
import { AbilityRule, ResourceActionType } from './ability/caslAbility';
import { AbilityRule, ResourceActionType } from '@/lib/ability/caslAbility';
import { PackRule } from '@casl/ability/extra';
import { useCsrfTokenStore } from './csrfTokenStore';
import { useCsrfTokenStore } from '@/lib/csrfTokenStore';
import { useRouter } from 'next/navigation';
import Ability from './ability/abilityHelper';
import Ability from '@/lib/ability/abilityHelper';

export type AuthCanProps = {
action: ResourceActionType | ResourceActionType[];
Expand Down Expand Up @@ -41,6 +41,9 @@ export const FetchAbility = () => {
return <></>;
};

// TODO: Weil client side werden evtl. sensible Daten an den Client geschickt.
// Auf server side ändern und eigene component für client side die aber nur für
// buttons etc. benutzt werden sollte
export const AuthCan: FC<PropsWithChildren<AuthCanProps>> = ({
action,
resource,
Expand Down
36 changes: 36 additions & 0 deletions src/management-system-v2/components/auth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import 'server-only';

import { ComponentProps, ComponentType, cache } from 'react';
import { getServerSession } from 'next-auth/next';
import { redirect } from 'next/navigation';
import { nextAuthOptions } from '@/app/api/auth/[...nextauth]/route';
import { AuthCan, AuthCanProps } from './auth-can';
import { getAbilityForUser } from '@/lib/authorization/authorization';

export const getCurrentUser = cache(async () => {
const session = await getServerSession(nextAuthOptions);
const ability = await getAbilityForUser(session?.user.id || '');

return { session, ability };
});

// HOC for server-side auth checking
const Auth = <P extends {}>(authOptions: AuthCanProps, Component: ComponentType<P>) => {
async function WrappedComponent(props: ComponentProps<ComponentType<P>>) {
const { session } = await getCurrentUser();

if (!session && authOptions.fallbackRedirect) {
redirect(authOptions.fallbackRedirect);
}

return (
<AuthCan {...authOptions}>
<Component {...props} />
</AuthCan>
);
}

return WrappedComponent;
};

export default Auth;
24 changes: 13 additions & 11 deletions src/management-system-v2/components/process-creation-button.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use client';

import React, { ReactNode, useState } from 'react';

import React, { ReactNode, useState, useTransition } from 'react';
import { Button } from 'antd';
import type { ButtonProps } from 'antd';
import ProcessModal from './process-modal';
import { usePostAsset } from '@/lib/fetch-data';
import { createProcess } from '@/lib/helpers/processHelpers';
import { addProcess } from '@/lib/data/processes';
import { useRouter } from 'next/navigation';

type ProcessCreationButtonProps = ButtonProps & {
createProcess?: (values: { name: string; description?: string }) => any;
Expand All @@ -24,17 +24,19 @@ const ProcessCreationButton: React.FC<ProcessCreationButtonProps> = ({
...props
}) => {
const [isProcessModalOpen, setIsProcessModalOpen] = useState(false);
const { mutateAsync: postProcess } = usePostAsset('/process');
const [isPending, startTransition] = useTransition();
const router = useRouter();

const createNewProcess = async (values: { name: string; description?: string }) => {
const { metaInfo, bpmn } = await createProcess(values);
try {
await postProcess({
body: { bpmn: bpmn, departments: [] },
});
} catch (err) {
console.log(err);
}
startTransition(async () => {
try {
await addProcess({ bpmn: bpmn, departments: [] });
} catch (error) {
console.error(error);
}
router.refresh();
});
};

return (
Expand Down
33 changes: 21 additions & 12 deletions src/management-system-v2/components/process-delete-single.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
import { Button, Checkbox, Modal } from 'antd';
import React, { Dispatch, FC, Key, SetStateAction, useCallback, useState } from 'react';
import React, {
Dispatch,
FC,
Key,
SetStateAction,
startTransition,
useCallback,
useState,
useTransition,
} from 'react';
import styles from './process-delete.module.scss';
import { useUserPreferences } from '@/lib/user-preferences';
import { useDeleteAsset, useInvalidateAsset } from '@/lib/fetch-data';
import { deleteProcesses } from '@/lib/data/processes';
import { useRouter } from 'next/navigation';

type ProcessDeleteModalType = {
setDeleteProcessIds: Dispatch<SetStateAction<string[]>> | Dispatch<SetStateAction<Key[]>>;
Expand All @@ -16,6 +27,8 @@ const ProcessDeleteSingleModal: FC<ProcessDeleteModalType> = ({
setSelection,
}) => {
const refreshData = useInvalidateAsset('/process');
const [isPending, startTransition] = useTransition();
const router = useRouter();

const [loading, setLoading] = useState(false);

Expand All @@ -29,18 +42,14 @@ const ProcessDeleteSingleModal: FC<ProcessDeleteModalType> = ({
},
});

const deleteSelectedProcesses = useCallback(() => {
processKeys.forEach((key: React.Key) => {
deleteProcess({
params: {
path: {
definitionId: key as string,
},
},
parseAs: 'text',
});
const deleteSelectedProcesses = () => {
startTransition(async () => {
await deleteProcesses(processKeys as string[]);
setDeleteProcessIds([]);
setSelection([]);
router.refresh();
});
}, [deleteProcess, processKeys]);
};

return (
<>
Expand Down
2 changes: 1 addition & 1 deletion src/management-system-v2/components/process-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { useDeleteAsset, useInvalidateAsset, usePostAsset } from '@/lib/fetch-da
import { useUserPreferences } from '@/lib/user-preferences';
import ProcessDeleteSingleModal from './process-delete-single';
import { useAbilityStore } from '@/lib/abilityStore';
import { AuthCan } from '@/lib/clientAuthComponents';
import { AuthCan } from '@/components/auth-can';
import { ProcessListProcess } from './processes';

type ProcessListProps = PropsWithChildren<{
Expand Down
Loading

0 comments on commit dccb04a

Please sign in to comment.