Compare commits
8 Commits
aff1586c9b
...
f71dccf166
| Author | SHA1 | Date | |
|---|---|---|---|
| f71dccf166 | |||
| cc73fc4af2 | |||
| 76a5c0b454 | |||
| 2b7a92097c | |||
| c9f8c69e42 | |||
| bc41823dc3 | |||
| 6c2047033b | |||
| 6c3f6c8d03 |
@@ -5,8 +5,9 @@ from uuid import UUID
|
||||
|
||||
from beanie import PydanticObjectId
|
||||
from pydantic import BaseModel, Field
|
||||
from pydantic.json_schema import SkipJsonSchema
|
||||
|
||||
from firm.core.models import CrudDocument, RichtextSingleline, RichtextMultiline, DictionaryEntry
|
||||
from firm.core.models import CrudDocument, RichtextSingleline, RichtextMultiline, DictionaryEntry, ForeignKey
|
||||
from firm.core.filter import Filter, FilterSchema
|
||||
from firm.entity.models import Entity
|
||||
|
||||
@@ -25,27 +26,10 @@ class ContractDraftStatus(str, Enum):
|
||||
|
||||
|
||||
class DraftParty(BaseModel):
|
||||
entity_id: PydanticObjectId = Field(
|
||||
foreignKey={
|
||||
"reference": {
|
||||
"resource": "entities",
|
||||
"schema": "Entity",
|
||||
}
|
||||
},
|
||||
default="",
|
||||
title="Partie"
|
||||
)
|
||||
entity_id: PydanticObjectId = ForeignKey("entities", "Entity", default="", title="Partie")
|
||||
entity: SkipJsonSchema[Entity] = Field(default=None, exclude=True, )
|
||||
part: str = Field(title="Rôle")
|
||||
representative_id: PydanticObjectId = Field(
|
||||
foreignKey={
|
||||
"reference": {
|
||||
"resource": "entities",
|
||||
"schema": "Entity",
|
||||
}
|
||||
},
|
||||
default="",
|
||||
title="Représentant"
|
||||
)
|
||||
representative_id: PydanticObjectId = ForeignKey("entities", "Entity", default="", title="Représentant")
|
||||
|
||||
class Config:
|
||||
title = 'Partie'
|
||||
@@ -74,14 +58,10 @@ class ProvisionGenuine(BaseModel):
|
||||
|
||||
class ContractProvisionTemplateReference(BaseModel):
|
||||
type: Literal['template'] = ContractProvisionType.template
|
||||
provision_template_id: PydanticObjectId = Field(
|
||||
foreignKey={
|
||||
"reference": {
|
||||
"resource": "templates/provisions",
|
||||
"schema": "ProvisionTemplate",
|
||||
"displayedFields": ['title', 'body']
|
||||
},
|
||||
},
|
||||
provision_template_id: PydanticObjectId = ForeignKey(
|
||||
"templates/provisions",
|
||||
"ProvisionTemplate",
|
||||
displayed_fields=['title', 'body'],
|
||||
props={"parametrized": True},
|
||||
default="",
|
||||
title="Template de clause"
|
||||
@@ -173,6 +153,9 @@ class ContractDraft(CrudDocument):
|
||||
update = ContractDraftUpdateStatus(status=status)
|
||||
await self.update(db, self, update)
|
||||
|
||||
def compute_label(self) -> str:
|
||||
return f"{self.name} - {self.title}"
|
||||
|
||||
class Contract(CrudDocument):
|
||||
"""
|
||||
Contrat publié. Les contrats ne peuvent pas être modifiés.
|
||||
|
||||
@@ -114,6 +114,20 @@ def RichtextSingleline(*args, **kwargs):
|
||||
return Field(*args, **kwargs)
|
||||
|
||||
|
||||
def ForeignKey(resource, schema, displayed_fields=None, *args, **kwargs):
|
||||
kwargs["foreignKey"] = {
|
||||
"reference": {
|
||||
"resource": resource,
|
||||
"schema": schema,
|
||||
}
|
||||
}
|
||||
|
||||
if displayed_fields:
|
||||
kwargs["foreignKey"]["reference"]["displayedFields"] = displayed_fields
|
||||
|
||||
return Field(*args, **kwargs)
|
||||
|
||||
|
||||
class DictionaryEntry(BaseModel):
|
||||
key: str
|
||||
value: str = ""
|
||||
|
||||
@@ -4,6 +4,7 @@ import { RegistryFieldsType, RegistryWidgetsType, RJSFSchema, UiSchema } from "@
|
||||
import CrudTextWidget from "./widgets/crud-text-widget";
|
||||
import UnionEnumField from "./fields/union-enum";
|
||||
import { ResourceContext } from "../contexts/ResourceContext";
|
||||
import { ReactNode } from "react";
|
||||
|
||||
type BaseFormProps = {
|
||||
schema: RJSFSchema,
|
||||
@@ -12,6 +13,7 @@ type BaseFormProps = {
|
||||
onChange?: (data: any) => void,
|
||||
uiSchema?: UiSchema,
|
||||
formData?: any,
|
||||
children?: ReactNode
|
||||
}
|
||||
|
||||
export const customWidgets: RegistryWidgetsType = {
|
||||
@@ -23,7 +25,7 @@ export const customFields: RegistryFieldsType = {
|
||||
}
|
||||
|
||||
export const BaseForm: React.FC<BaseFormProps> = (props) => {
|
||||
const { schema, uiSchema, resourceBasePath, formData, onSubmit, onChange } = props;
|
||||
const { schema, uiSchema, resourceBasePath, formData, children, onSubmit, onChange } = props;
|
||||
|
||||
return (
|
||||
<ResourceContext.Provider value={{basePath: resourceBasePath}} >
|
||||
@@ -37,6 +39,7 @@ export const BaseForm: React.FC<BaseFormProps> = (props) => {
|
||||
widgets={customWidgets}
|
||||
fields={customFields}
|
||||
onChange={(e, id) => onChange != undefined && onChange(e.formData)}
|
||||
children={children}
|
||||
/>
|
||||
</ResourceContext.Provider>
|
||||
)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { ReactNode, useEffect, useState } from "react";
|
||||
import { CircularProgress } from "@mui/material";
|
||||
import { useForm } from "@refinedev/core";
|
||||
import { UiSchema } from "@rjsf/utils";
|
||||
@@ -12,11 +12,12 @@ type CrudFormProps = {
|
||||
resource: string,
|
||||
id?: string,
|
||||
onSuccess?: (data: any) => void,
|
||||
defaultValue?: any
|
||||
defaultValue?: any,
|
||||
children?: ReactNode
|
||||
}
|
||||
|
||||
export const CrudForm: React.FC<CrudFormProps> = (props) => {
|
||||
const { schemaName, uiSchema, resourceBasePath="" ,resource, id, onSuccess, defaultValue } = props;
|
||||
const { schemaName, uiSchema, resourceBasePath="" ,resource, id, onSuccess, defaultValue, children } = props;
|
||||
|
||||
const { onFinish, query, formLoading } = useForm({
|
||||
resource: resourceBasePath == "" ? resource : `${resourceBasePath}/${resource}`,
|
||||
@@ -57,6 +58,7 @@ export const CrudForm: React.FC<CrudFormProps> = (props) => {
|
||||
onSubmit={
|
||||
(data: any) => onFinish(data)
|
||||
}
|
||||
children={children}
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
@@ -1,4 +1,11 @@
|
||||
import { Route, Routes } from "react-router";
|
||||
import { CircularProgress } from "@mui/material";
|
||||
import React, { useContext, useState } from "react";
|
||||
import { useOne } from "@refinedev/core";
|
||||
import { BaseForm } from "../../lib/crud/components/base-form";
|
||||
import { ForeignKeyReference, ForeignKeySchema } from "../../lib/crud/components/widgets/foreign-key";
|
||||
|
||||
import { FirmContext } from "../../contexts/FirmContext";
|
||||
import List from "./base-page/List";
|
||||
import Edit from "./base-page/Edit";
|
||||
import New from "./base-page/New";
|
||||
@@ -29,6 +36,79 @@ const EditDraft = () => {
|
||||
return <Edit<Draft> resource={`contracts/drafts`} schemaName={"ContractDraft"} />
|
||||
}
|
||||
|
||||
const CreateDraft = () => {
|
||||
return <New<Draft> resource={`contracts/drafts`} schemaName={"ContractDraft"} />
|
||||
type ForeignKeySubSchema = ForeignKeySchema & {
|
||||
properties: { [key: string]: { foreignKey: { reference: ForeignKeyReference } } }
|
||||
}
|
||||
|
||||
const CreateDraft = () => {
|
||||
const [chosenDraft, setChosenDraft] = useState<string|null>(null)
|
||||
const { currentFirm } = useContext(FirmContext);
|
||||
const resourceBasePath = `firm/${currentFirm.instance}/${currentFirm.firm}`
|
||||
const templateFieldSchema: ForeignKeySubSchema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
template_id: {
|
||||
type: "string",
|
||||
title: "Find a template",
|
||||
foreignKey: {
|
||||
reference: {
|
||||
resource: "templates/contracts",
|
||||
schema: "ContractTemplate"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const templateForm = (
|
||||
<BaseForm
|
||||
schema={templateFieldSchema}
|
||||
formData={{template_id: chosenDraft}}
|
||||
resourceBasePath={resourceBasePath}
|
||||
onChange={(data) => {
|
||||
const { template_id } = data;
|
||||
setChosenDraft(template_id);
|
||||
}}
|
||||
>
|
||||
|
||||
</BaseForm>
|
||||
)
|
||||
|
||||
if (chosenDraft !== null) {
|
||||
return (
|
||||
<>
|
||||
{templateForm}
|
||||
<CreateDraftFromTemplate template_id={chosenDraft}/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
{templateForm}
|
||||
<New<Draft> resource={`contracts/drafts`} schemaName={"ContractDraft"} />
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
const CreateDraftFromTemplate = (props: { template_id: string }) => {
|
||||
const { template_id } = props;
|
||||
const { currentFirm } = useContext(FirmContext);
|
||||
const resourceBasePath = `firm/${currentFirm.instance}/${currentFirm.firm}`
|
||||
const resource = "templates/contracts"
|
||||
const { data, isLoading } = useOne({
|
||||
resource: `${resourceBasePath}/${resource}`,
|
||||
id: template_id
|
||||
});
|
||||
|
||||
if (isLoading || data === undefined) {
|
||||
return <CircularProgress />
|
||||
}
|
||||
|
||||
let template = { ...data.data };
|
||||
template.provisions = data.data.provisions.map((item: any) => {
|
||||
return { provision: {type: "template", provision_template_id: item.provision_template_id} }
|
||||
})
|
||||
|
||||
return <New<Draft> resource={`contracts/drafts`} schemaName={"ContractDraft"} defaultValue={ template }/>
|
||||
}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import { CrudForm } from "../../../lib/crud/components/crud-form";
|
||||
import { UiSchema } from "@rjsf/utils";
|
||||
import { useParams } from "react-router";
|
||||
import { useContext } from "react";
|
||||
import { Button } from "@mui/material";
|
||||
import DeleteIcon from '@mui/icons-material/Delete';
|
||||
import SaveIcon from '@mui/icons-material/Save';
|
||||
import { FirmContext } from "../../../contexts/FirmContext";
|
||||
import { CrudForm } from "../../../lib/crud/components/crud-form";
|
||||
import Stack from "@mui/material/Stack";
|
||||
import { DeleteButton } from "@refinedev/mui";
|
||||
|
||||
type EditProps = {
|
||||
resource: string,
|
||||
@@ -17,13 +22,26 @@ const Edit = <T,>(props: EditProps) => {
|
||||
const { record_id } = useParams();
|
||||
|
||||
return (
|
||||
<>
|
||||
<CrudForm
|
||||
schemaName={schemaName}
|
||||
uiSchema={uiSchema}
|
||||
resourceBasePath={resourceBasePath}
|
||||
resource={resource}
|
||||
id={record_id}
|
||||
/>
|
||||
>
|
||||
<Stack
|
||||
direction="row"
|
||||
spacing={2}
|
||||
sx={{
|
||||
justifyContent: "space-between",
|
||||
alignItems: "center",
|
||||
}}>
|
||||
<Button type='submit' variant="contained" size="large"><SaveIcon />Save</Button>
|
||||
<DeleteButton variant="contained" size="large" color="error" recordItemId={record_id}/>
|
||||
</Stack>
|
||||
</CrudForm>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ const dataProvider: DataProvider = {
|
||||
};
|
||||
},
|
||||
create: async ({ resource, variables }) => {
|
||||
const response = await fetch(`${API_URL}/${resource}`, {
|
||||
const response = await fetch(`${API_URL}/${resource}/`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify(variables),
|
||||
headers: {
|
||||
|
||||
Reference in New Issue
Block a user