-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added components for process creation; use them on multiple occasions
- Loading branch information
Showing
7 changed files
with
459 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
'use client'; | ||
|
||
import { FC, PropsWithChildren } from 'react'; | ||
import { Menu } from 'antd'; | ||
const { SubMenu, Item, ItemGroup } = Menu; | ||
import { EditOutlined, ProfileOutlined, FileAddOutlined, StarOutlined } from '@ant-design/icons'; | ||
import { usePathname, useRouter } from 'next/navigation'; | ||
import { useAuthStore } from '@/lib/iam'; | ||
import ProcessCreationButton from './process-creation-button'; | ||
|
||
const ProcessSider: FC<PropsWithChildren> = () => { | ||
const router = useRouter(); | ||
const activeSegment = usePathname().slice(1) || 'processes'; | ||
const ability = useAuthStore((state) => state.ability); | ||
|
||
return ( | ||
<> | ||
<ItemGroup key="processes" title="Processes"> | ||
{ability.can('view', 'Process') ? ( | ||
<SubMenu | ||
key="processes" | ||
title={ | ||
<span | ||
onClick={() => { | ||
router.push(`/processes`); | ||
}} | ||
> | ||
Process List | ||
</span> | ||
} | ||
className={activeSegment === 'processes' ? 'SelectedSegment' : ''} | ||
icon={ | ||
<EditOutlined | ||
onClick={() => { | ||
router.push(`/processes`); | ||
}} | ||
/> | ||
} | ||
> | ||
<Item | ||
key="newProcess" | ||
icon={<FileAddOutlined />} | ||
hidden={!ability.can('create', 'Process')} | ||
> | ||
<ProcessCreationButton | ||
wrapperElement={<span>New Process</span>} | ||
></ProcessCreationButton> | ||
</Item> | ||
<Item key="processFavorites" icon={<StarOutlined />}> | ||
Favorites | ||
</Item> | ||
</SubMenu> | ||
) : null} | ||
|
||
{ability.can('view', 'Template') ? ( | ||
<SubMenu key="templates" title="Templates" icon={<ProfileOutlined />}> | ||
<Item | ||
key="newTemplate" | ||
icon={<FileAddOutlined />} | ||
hidden={!ability.can('create', 'Template')} | ||
> | ||
New Template | ||
</Item> | ||
<Item key="templateFavorites" icon={<StarOutlined />}> | ||
Favorites | ||
</Item> | ||
</SubMenu> | ||
) : null} | ||
</ItemGroup> | ||
</> | ||
); | ||
}; | ||
|
||
export default ProcessSider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
src/management-system-v2/components/process-creation-button.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
'use client'; | ||
|
||
import React, { ReactNode, useState } 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'; | ||
|
||
type ProcessCreationButtonProps = ButtonProps & { | ||
createProcess?: (values: { name: string; description: string }) => any; | ||
wrapperElement?: ReactNode; | ||
}; | ||
|
||
/** | ||
* | ||
* Button to create Processes including a Modal for inserting needed values. Alternatively, a custom wrapper element can be used instead of a button. | ||
* Custom function for creation of process using inserted values can be applied | ||
*/ | ||
const ProcessCreationButton: React.FC<ProcessCreationButtonProps> = ({ | ||
createProcess: customCreateProcess, | ||
wrapperElement, | ||
...props | ||
}) => { | ||
const [isProcessModalOpen, setIsProcessModalOpen] = useState(false); | ||
const { mutateAsync: postProcess } = usePostAsset('/process'); | ||
|
||
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); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
{wrapperElement ? ( | ||
<div | ||
onClick={() => { | ||
setIsProcessModalOpen(true); | ||
}} | ||
> | ||
{wrapperElement} | ||
</div> | ||
) : ( | ||
<Button | ||
{...props} | ||
onClick={() => { | ||
setIsProcessModalOpen(true); | ||
}} | ||
></Button> | ||
)} | ||
<ProcessModal | ||
close={(values) => { | ||
setIsProcessModalOpen(false); | ||
|
||
if (values) { | ||
customCreateProcess ? customCreateProcess(values) : createNewProcess(values); | ||
} | ||
}} | ||
show={isProcessModalOpen} | ||
></ProcessModal> | ||
</> | ||
); | ||
}; | ||
|
||
export default ProcessCreationButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
'use client'; | ||
|
||
import React, { useState } from 'react'; | ||
|
||
import { Button, Modal, Form, Input } from 'antd'; | ||
|
||
import { FormInstance } from 'antd/es/form'; | ||
|
||
const ModalSubmitButton = ({ form, onSubmit }: { form: FormInstance; onSubmit: Function }) => { | ||
const [submittable, setSubmittable] = useState(false); | ||
|
||
// Watch all values | ||
const values = Form.useWatch([], form); | ||
|
||
React.useEffect(() => { | ||
form.validateFields({ validateOnly: true }).then( | ||
() => { | ||
setSubmittable(true); | ||
}, | ||
() => { | ||
setSubmittable(false); | ||
}, | ||
); | ||
}, [form, values]); | ||
|
||
return ( | ||
<Button | ||
type="primary" | ||
htmlType="submit" | ||
disabled={!submittable} | ||
onClick={() => { | ||
onSubmit(values); | ||
form.resetFields(); | ||
}} | ||
> | ||
Create Process | ||
</Button> | ||
); | ||
}; | ||
|
||
type ProcessModalProps = { | ||
show: boolean; | ||
close: (values?: { name: string; description: string }) => void; | ||
}; | ||
const ProcessModal: React.FC<ProcessModalProps> = ({ show, close }) => { | ||
const [form] = Form.useForm(); | ||
|
||
return ( | ||
<Modal | ||
title="Create new Process" | ||
open={show} | ||
onCancel={() => { | ||
close(); | ||
}} | ||
footer={[ | ||
<Button | ||
key="cancel" | ||
onClick={() => { | ||
close(); | ||
}} | ||
> | ||
Cancel | ||
</Button>, | ||
<ModalSubmitButton key="submit" form={form} onSubmit={close}></ModalSubmitButton>, | ||
]} | ||
> | ||
<Form form={form} name="name" wrapperCol={{ span: 24 }} autoComplete="off"> | ||
<Form.Item | ||
name="name" | ||
rules={[{ required: true, message: 'Please input the Process Name!' }]} | ||
> | ||
<Input placeholder="Process Name" /> | ||
</Form.Item> | ||
<Form.Item | ||
name="description" | ||
rules={[{ required: true, message: 'Please input the Process Description!' }]} | ||
> | ||
<Input.TextArea | ||
showCount | ||
maxLength={150} | ||
style={{ height: 100 }} | ||
placeholder="Process Description" | ||
/> | ||
</Form.Item> | ||
</Form> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default ProcessModal; |
Oops, something went wrong.