Modifier les collaborateurs working

This commit is contained in:
Maxime Lalo 2024-07-22 17:29:47 +02:00
parent 88671ebf37
commit 88922cece1
2 changed files with 130 additions and 205 deletions

View File

@ -3,59 +3,19 @@
.root { .root {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
min-height: 100%; width: 472px;
align-items: flex-start; margin: auto;
width: fit-content; margin-top: 24px;
gap: var(--spacing-xl, 32px);
.back-arrow {
margin-bottom: 24px;
}
.form { .form {
width: 100%; display: flex;
flex-direction: column;
.content { gap: var(--spacing-xl, 32px);
margin-top: 32px;
>:not(:last-child) {
margin-bottom: 24px;
}
}
.sub-content {
margin-top: 32px;
margin-bottom: 24px;
}
.button-container { .button-container {
width: 100%;
display: flex; display: flex;
text-align: center; gap: var(--spacing-md, 16px);
margin-top: 24px;
.cancel-button {
display: flex;
margin-right: 12px;
}
@media (max-width: $screen-m) {
display: flex;
flex-direction: column-reverse;
.cancel-button {
margin-left: 0;
margin-top: 12px;
>* {
flex: 1;
}
}
>* {
width: 100%;
}
}
} }
} }
} }

View File

@ -7,122 +7,71 @@ import MultiSelect from "@Front/Components/DesignSystem/MultiSelect";
import RadioBox from "@Front/Components/DesignSystem/RadioBox"; import RadioBox from "@Front/Components/DesignSystem/RadioBox";
import Typography, { ETypo } from "@Front/Components/DesignSystem/Typography"; import Typography, { ETypo } from "@Front/Components/DesignSystem/Typography";
import BackArrow from "@Front/Components/Elements/BackArrow"; import BackArrow from "@Front/Components/Elements/BackArrow";
import DefaultNotaryDashboard from "@Front/Components/LayoutTemplates/DefaultNotaryDashboard";
import Module from "@Front/Config/Module"; import Module from "@Front/Config/Module";
import User from "le-coffre-resources/dist/Notary/User"; import User from "le-coffre-resources/dist/Notary/User";
import { OfficeFolder } from "le-coffre-resources/dist/Notary";
import Link from "next/link"; import Link from "next/link";
import { NextRouter, useRouter } from "next/router"; import { useRouter } from "next/router";
import BasePage from "../../Base";
import classes from "./classes.module.scss"; import classes from "./classes.module.scss";
import { ValidationError } from "class-validator"; import { ValidationError } from "class-validator";
import backgroundImage from "@Assets/images/background_refonte.svg";
type IPropsClass = { import DefaultDoubleSidePage from "@Front/Components/LayoutTemplates/DefaultDoubleSidePage";
selectedFolderUid: string; import { useCallback, useEffect, useState } from "react";
router: NextRouter;
};
type IState = {
selectedFolder: OfficeFolder | null;
selectedOption?: ERadioBoxValue;
availableCollaborators: User[];
defaultCheckedAllOffice: boolean;
selectedCollaborators: readonly IOption[];
loading: boolean;
validationError?: ValidationError[];
};
enum ERadioBoxValue { enum ERadioBoxValue {
ALL = "all", ALL = "all",
SELECTION = "selection", SELECTION = "selection",
} }
class UpdateFolderCollaboratorsClass extends BasePage<IPropsClass, IState> { export default function UpdateFolderCollaborators() {
constructor(props: IPropsClass) { const router = useRouter();
super(props); let { folderUid } = router.query;
this.state = {
selectedFolder: null,
availableCollaborators: [],
selectedCollaborators: [],
defaultCheckedAllOffice: false,
selectedOption: ERadioBoxValue.SELECTION,
loading: true,
};
this.onSelectedFolder = this.onSelectedFolder.bind(this);
this.onSelectedOptionAllOffice = this.onSelectedOptionAllOffice.bind(this);
this.onSelectedOptionSpecific = this.onSelectedOptionSpecific.bind(this);
this.onFormSubmit = this.onFormSubmit.bind(this);
this.onChangeSelectedCollaborators = this.onChangeSelectedCollaborators.bind(this);
}
public override render(): JSX.Element {
const foldersInformationPath = Module.getInstance().get().modules.pages.Folder.pages.FolderInformation.props.path;
const backwardPath = foldersInformationPath.replace("[folderUid]", this.props.selectedFolderUid);
const selectOptions: IOption[] = this.state.availableCollaborators.map((collaborator) => {
return {
label: collaborator.contact?.first_name + " " + collaborator.contact?.last_name,
value: collaborator.uid,
};
});
return ( const [availableCollaborators, setAvailableCollaborators] = useState<User[]>([]);
<DefaultNotaryDashboard title={"Ajouter client(s)"} onSelectedFolder={this.onSelectedFolder}> const [selectedCollaborators, setSelectedCollaborators] = useState<readonly IOption[]>([]);
<div className={classes["root"]}> const [defaultCheckedAllOffice, setDefaultCheckedAllOffice] = useState<boolean>(false);
<div className={classes["back-arrow"]}> const [selectedOption, setSelectedOption] = useState<ERadioBoxValue>(ERadioBoxValue.SELECTION);
<BackArrow url={backwardPath} /> const [loading, setLoading] = useState<boolean>(true);
</div> const [validationError, setValidationError] = useState<ValidationError[]>([]);
<Typography typo={ETypo.TITLE_H1}>Modifier les collaborateurs</Typography>
{!this.state.loading && ( const onFormSubmit = useCallback(
<Form className={classes["form"]} onSubmit={this.onFormSubmit}> async (e: React.FormEvent<HTMLFormElement> | null, values: { [key: string]: string }) => {
<div className={classes["content"]}> if (!folderUid || typeof folderUid !== "string") return;
<RadioBox try {
name="office" let collaboratorsUid: User[] = availableCollaborators;
value={ERadioBoxValue.ALL} if (selectedOption === ERadioBoxValue.SELECTION) {
defaultChecked={this.state.defaultCheckedAllOffice} collaboratorsUid = selectedCollaborators.map((collaborator) =>
onChange={this.onSelectedOptionAllOffice}> User.hydrate<User>({ uid: collaborator.value as string }),
Tout l'office
</RadioBox>
<RadioBox
name="office"
value={ERadioBoxValue.SELECTION}
defaultChecked={!this.state.defaultCheckedAllOffice}
onChange={this.onSelectedOptionSpecific}>
Sélectionner des collaborateurs
</RadioBox>
</div>
{this.state.selectedOption === ERadioBoxValue.SELECTION && (
<div className={classes["sub-content"]}>
<MultiSelect
onChange={this.onChangeSelectedCollaborators}
options={selectOptions}
placeholder="Collaborateurs"
defaultValue={this.state.selectedCollaborators}
validationError={this.state.validationError?.find((error) => error.property === "stakeholders")}
/>
</div>
)}
<div className={classes["button-container"]}>
<Link href={backwardPath} className={classes["cancel-button"]}>
<Button variant={EButtonVariant.PRIMARY} styletype={EButtonstyletype.OUTLINED}>
Annuler
</Button>
</Link>
<Button type="submit">Enregistrer</Button>
</div>
</Form>
)}
</div>
</DefaultNotaryDashboard>
); );
} }
await Folders.getInstance().put(folderUid, { stakeholders: collaboratorsUid });
public override async componentDidMount() { router.push(
await this.getFolderAvailableCollaborators(this.props.selectedFolderUid); Module.getInstance().get().modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", folderUid),
);
} catch (error) {
if (!Array.isArray(error)) return;
setValidationError(error as ValidationError[]);
} }
},
[availableCollaborators, folderUid, router, selectedCollaborators, selectedOption],
);
private async getFolderAvailableCollaborators(folderUid: string) { const onSelectedOptionAllOffice = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
if (event.target.value !== ERadioBoxValue.ALL) return;
setSelectedOption(ERadioBoxValue.ALL);
}, []);
const onChangeSelectedCollaborators = useCallback((selectedCollaborators: readonly IOption[]) => {
setSelectedCollaborators(selectedCollaborators);
}, []);
const onSelectedOptionSpecific = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
if (event.target.value !== ERadioBoxValue.SELECTION) return;
setSelectedOption(ERadioBoxValue.SELECTION);
}, []);
const getFolderInfos = useCallback(async () => {
if (!folderUid || typeof folderUid !== "string") return;
const query = { const query = {
q: { q: {
office: true, office: true,
@ -158,66 +107,82 @@ class UpdateFolderCollaboratorsClass extends BasePage<IPropsClass, IState> {
const availableCollaborators = await Users.getInstance().get(userQuery); const availableCollaborators = await Users.getInstance().get(userQuery);
this.setState({ setLoading(false);
loading: false, setAvailableCollaborators(availableCollaborators);
availableCollaborators, setSelectedCollaborators(preSelectedCollaborators);
selectedCollaborators: preSelectedCollaborators, setDefaultCheckedAllOffice(preSelectedCollaborators.length === availableCollaborators.length);
defaultCheckedAllOffice: preSelectedCollaborators.length === availableCollaborators.length, setSelectedOption(
selectedOption:
preSelectedCollaborators.length === availableCollaborators.length ? ERadioBoxValue.ALL : ERadioBoxValue.SELECTION, preSelectedCollaborators.length === availableCollaborators.length ? ERadioBoxValue.ALL : ERadioBoxValue.SELECTION,
}); );
} catch (error) { } catch (error) {
this.props.router.push(Module.getInstance().get().modules.pages["404"].props.path); router.push(Module.getInstance().get().modules.pages["404"].props.path);
return; return;
} }
} }, [folderUid, router]);
private onSelectedOptionAllOffice(event: React.ChangeEvent<HTMLInputElement>) { useEffect(() => {
if (event.target.value !== ERadioBoxValue.ALL) return; getFolderInfos();
this.setState({ }, [getFolderInfos]);
selectedOption: ERadioBoxValue.ALL,
const foldersInformationPath = Module.getInstance().get().modules.pages.Folder.pages.FolderInformation.props.path;
const backwardPath = foldersInformationPath.replace("[folderUid]", folderUid as string);
const selectOptions: IOption[] = availableCollaborators.map((collaborator) => {
return {
label: collaborator.contact?.first_name + " " + collaborator.contact?.last_name,
value: collaborator.uid,
};
}); });
}
private onChangeSelectedCollaborators(selectedCollaborators: readonly IOption[]) { return (
this.setState({ selectedCollaborators }); <DefaultDoubleSidePage title={"Dossier"} image={backgroundImage} type="background" showHeader={true}>
} <div className={classes["root"]}>
<div className={classes["back-arrow"]}>
<BackArrow url={backwardPath} />
</div>
<Typography typo={ETypo.TITLE_H1}>Modifier les collaborateurs</Typography>
private onSelectedOptionSpecific(event: React.ChangeEvent<HTMLInputElement>) { {!loading && (
if (event.target.value !== ERadioBoxValue.SELECTION) return; <Form className={classes["form"]} onSubmit={onFormSubmit}>
this.setState({ <div className={classes["content"]}>
selectedOption: ERadioBoxValue.SELECTION, <RadioBox
}); name="office"
} value={ERadioBoxValue.ALL}
defaultChecked={defaultCheckedAllOffice}
onChange={onSelectedOptionAllOffice}>
Tout l'office
</RadioBox>
<RadioBox
name="office"
value={ERadioBoxValue.SELECTION}
defaultChecked={!defaultCheckedAllOffice}
onChange={onSelectedOptionSpecific}>
Sélectionner des collaborateurs
</RadioBox>
</div>
private onSelectedFolder(folder: OfficeFolder): void { {selectedOption === ERadioBoxValue.SELECTION && (
this.setState({ selectedFolder: folder }); <div className={classes["sub-content"]}>
} <MultiSelect
onChange={onChangeSelectedCollaborators}
options={selectOptions}
placeholder="Collaborateurs"
defaultValue={selectedCollaborators}
validationError={validationError?.find((error) => error.property === "stakeholders")}
/>
</div>
)}
private async onFormSubmit(e: React.FormEvent<HTMLFormElement> | null, values: { [key: string]: string }) { <div className={classes["button-container"]}>
try { <Link href={backwardPath} className={classes["cancel-button"]}>
let collaboratorsUid: User[] = this.state.availableCollaborators; <Button variant={EButtonVariant.PRIMARY} styletype={EButtonstyletype.OUTLINED}>
if (this.state.selectedOption === ERadioBoxValue.SELECTION) { Annuler
collaboratorsUid = this.state.selectedCollaborators.map((collaborator) => </Button>
User.hydrate<User>({ uid: collaborator.value as string }), </Link>
<Button type="submit">Enregistrer</Button>
</div>
</Form>
)}
</div>
</DefaultDoubleSidePage>
); );
} }
await Folders.getInstance().put(this.props.selectedFolderUid, { stakeholders: collaboratorsUid });
this.props.router.push(
Module.getInstance()
.get()
.modules.pages.Folder.pages.FolderInformation.props.path.replace("[folderUid]", this.props.selectedFolderUid),
);
} catch (error) {
if (!Array.isArray(error)) return;
this.setState({ validationError: error });
}
}
}
export default function UpdateFolderCollaborators() {
const router = useRouter();
let { folderUid } = router.query;
folderUid = folderUid as string;
return <UpdateFolderCollaboratorsClass selectedFolderUid={folderUid} router={router} />;
}