Skip to content

Instantly share code, notes, and snippets.

@samyarkd
Last active March 6, 2023 07:01
Show Gist options
  • Save samyarkd/dd1e5d2ff13f42b73cda3d4258150e55 to your computer and use it in GitHub Desktop.
Save samyarkd/dd1e5d2ff13f42b73cda3d4258150e55 to your computer and use it in GitHub Desktop.
import BasicTextEditor from '@/components/manageTemplates/BasicTextEditor'
import { useContractCategories } from '@/hooks/queries/admin'
import { useUpdateTemplate } from '@/hooks/queries/contracts'
import { useGetTemplate } from '@/hooks/queries/contracts/useGetTemplate'
import useBreadCrumbsTitle from '@/hooks/store/useBreadCrumbsTitle'
import LANG from '@/lang'
import AppLayout from '@/layout/Layout'
import { ContractTemplateData } from '@/types/contracts'
import {
categoryFormRule,
explanationFormRule,
maxLengthFormRule,
nameFormRule,
previewTextFormRule,
slugFormRule,
titleFormRule,
} from '@/utils/formRules'
import { InfoCircleOutlined } from '@ant-design/icons'
import { Button, Card, Form, Input, Select, Typography } from 'antd'
import Head from 'next/head'
import { useRouter } from 'next/router'
import { ReactElement, useEffect } from 'react'
import { InPortal } from 'react-reverse-portal'
const Edit = () => {
const contractCategories = useContractCategories('')
const [form] = Form.useForm()
const updateTemplate = useUpdateTemplate()
const router = useRouter()
const id = router.query.id as unknown as number
const getTemplate = useGetTemplate(id)
const { breadCrumbsTitle } = useBreadCrumbsTitle()
const updateTemplateSubmit = (data: ContractTemplateData) => {
updateTemplate.mutate({ id, data })
}
useEffect(() => {
if (getTemplate.isFetched) {
getTemplate.data?.clause
?.map((item) => {
return `<h2>${item.title}</h2>${item.plain_text}`
})
.join('')
form.setFieldsValue({
name: getTemplate.data?.name,
category_id: getTemplate.data?.category.id,
explain: getTemplate.data?.explain,
preview_title: getTemplate.data?.preview_title,
preview_text: getTemplate.data?.preview_text,
slug: getTemplate.data?.slug,
contract_clause: getTemplate.data?.clause
?.map((item) => {
return `<h2>${item.title}</h2><p>${item.plain_text}</p>`
})
.join(''),
})
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [getTemplate.isFetched])
useEffect(() => {
return () => {
console.log('test')
getTemplate.remove()
form.resetFields()
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
return (
<>
<Head>
<title>{LANG.pages.manageContractTemplates.editTemplateModal.title}</title>
</Head>
{breadCrumbsTitle && (
<InPortal node={breadCrumbsTitle}>
<div>
<span>{getTemplate.data?.name}</span>
</div>
</InPortal>
)}
<Card
loading={contractCategories.isFetching && getTemplate.isFetching}
title={
<span>
{LANG.pages.manageContractTemplates.editTemplateModal.title}{' '}
<Typography.Text type="secondary" className="font-normal">
({getTemplate.isFetched ? getTemplate.data?.name : ''})
</Typography.Text>
</span>
}
>
<Form
onFinish={updateTemplateSubmit}
form={form}
layout="vertical"
className="grid grid-cols-1 lg:grid-cols-2 gap-4"
>
<Form.Item>
<Form.Item rules={nameFormRule} name="name" label={LANG.utils.title}>
<Input />
</Form.Item>
<Form.Item
rules={categoryFormRule}
name="category_id"
label={LANG.pages.manageContractTemplates.tableColumns.category_name}
>
<Select
placeholder={LANG.pages.manageContractTemplates.tableColumns.category_name}
options={contractCategories.data?.map((option) => ({
label: option.name,
value: option.id,
}))}
/>
</Form.Item>
<Form.Item
name="explain"
rules={[...explanationFormRule, ...maxLengthFormRule]}
label={LANG.pages.manageContractTemplates.tableColumns.explanation}
>
<Input />
</Form.Item>
<Form.Item
className="mb-0"
name="slug"
tooltip={{
title: LANG.pages.manageContractTemplates.editTemplateModal.slugInfo,
icon: <InfoCircleOutlined />,
}}
rules={slugFormRule}
label={LANG.utils.slug}
>
<Input />
</Form.Item>
</Form.Item>
{/* Left column (Public template page) 👇 */}
<Form.Item>
<Form.Item
rules={titleFormRule}
name="preview_title"
label={LANG.pages.manageContractTemplates.editTemplateModal.publicExplanation}
>
<Input.TextArea rows={4} />
</Form.Item>
<Form.Item
name="preview_text"
rules={previewTextFormRule}
label={LANG.pages.manageContractTemplates.editTemplateModal.previewText}
>
{/* @ts-ignore */}
<BasicTextEditor
placeholder={LANG.pages.manageContractTemplates.editTemplateModal.placeholder}
/>
</Form.Item>
</Form.Item>
<Form.Item
className="lg:col-span-2"
name="contract_clause"
label={LANG.pages.manageContractTemplates.template.title}
>
{/* @ts-ignore */}
<BasicTextEditor
placeholder={LANG.pages.manageContractTemplates.template.placeholder}
/>
</Form.Item>
</Form>
<Button
loading={updateTemplate.isLoading}
onClick={() => form.submit()}
type="primary"
htmlType="submit"
>
{LANG.utils.save}
</Button>
</Card>
</>
)
}
Edit.getLayout = function getLayout(page: ReactElement) {
return <AppLayout>{page}</AppLayout>
}
export default Edit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment