Manage collaborators
This commit is contained in:
parent
8e4124457b
commit
bc37864914
@ -3,7 +3,7 @@
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev -p 5005",
|
||||
"dev": "next dev",
|
||||
"build": "next build",
|
||||
"start": "next start",
|
||||
"lint": "next lint",
|
||||
|
@ -5,6 +5,10 @@ export interface IPostSubscriptionsParams {
|
||||
emails: string[];
|
||||
}
|
||||
|
||||
export interface IPutSubscriptionsParams {
|
||||
seats: any;
|
||||
}
|
||||
|
||||
export default class Subscriptions extends BaseAdmin {
|
||||
private static instance: Subscriptions;
|
||||
private readonly baseURl = this.namespaceUrl.concat("/subscriptions");
|
||||
@ -34,7 +38,7 @@ export default class Subscriptions extends BaseAdmin {
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Create a Document
|
||||
* @description : Invite collaborators to a subscription
|
||||
*/
|
||||
public async post(body: IPostSubscriptionsParams) {
|
||||
const url = new URL(this.baseURl.concat(`/invite`));
|
||||
@ -45,4 +49,17 @@ export default class Subscriptions extends BaseAdmin {
|
||||
return Promise.reject(err);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @description : Update a Subscription
|
||||
*/
|
||||
public async put(uid: string, body: IPutSubscriptionsParams) {
|
||||
const url = new URL(this.baseURl.concat(`/${uid}`));
|
||||
try {
|
||||
return await this.putRequest(url, body as any);
|
||||
} catch (err) {
|
||||
this.onError(err);
|
||||
return Promise.reject(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,78 +8,40 @@ import React, { useCallback, useEffect, useState } from "react";
|
||||
import User, { Subscription } from "le-coffre-resources/dist/Admin";
|
||||
import JwtService from "@Front/Services/JwtService/JwtService";
|
||||
import Subscriptions from "@Front/Api/LeCoffreApi/Admin/Subscriptions/Subscriptions";
|
||||
import Users from "@Front/Api/LeCoffreApi/Admin/Users/Users";
|
||||
import { useRouter } from "next/router";
|
||||
|
||||
export default function SubscriptionManageCollaborators() {
|
||||
const router = useRouter();
|
||||
const [subscription, setSubscription] = useState<Subscription | null>(null);
|
||||
const [availableCollaborators, _setAvailableCollaborators] = useState<User[]>([
|
||||
{
|
||||
created_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
uid: "1",
|
||||
idNot: "1",
|
||||
updated_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
contact: {
|
||||
civility: "M",
|
||||
created_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
email: "jean.dupont@gmail.com",
|
||||
first_name: "Jean",
|
||||
last_name: "Dupont",
|
||||
updated_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
},
|
||||
},
|
||||
{
|
||||
created_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
uid: "2",
|
||||
idNot: "1",
|
||||
updated_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
contact: {
|
||||
civility: "M",
|
||||
created_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
email: "jean.dupont@gmail.com",
|
||||
first_name: "Jean",
|
||||
last_name: "Dupont",
|
||||
updated_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
created_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
uid: "3",
|
||||
idNot: "1",
|
||||
updated_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
contact: {
|
||||
civility: "M",
|
||||
created_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
email: "jean.dupont@gmail.com",
|
||||
first_name: "Jean",
|
||||
last_name: "Dupont",
|
||||
updated_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
},
|
||||
},
|
||||
{
|
||||
created_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
uid: "4",
|
||||
idNot: "1",
|
||||
updated_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
contact: {
|
||||
civility: "M",
|
||||
created_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
email: "jean.dupont@gmail.com",
|
||||
first_name: "Jean",
|
||||
last_name: "Dupont",
|
||||
updated_at: new Date("2021-09-29T14:00:00.000Z"),
|
||||
},
|
||||
},
|
||||
]);
|
||||
const [availableCollaborators, _setAvailableCollaborators] = useState<User[]>([]);
|
||||
const [selectedCollaborators, setSelectedCollaborators] = useState<string[]>([]);
|
||||
|
||||
const loadSubscription = useCallback(async () => {
|
||||
const jwt = JwtService.getInstance().decodeJwt();
|
||||
const subscription = await Subscriptions.getInstance().get({ where: { office: { uid: jwt?.office_Id } } });
|
||||
const subscription = await Subscriptions.getInstance().get({
|
||||
where: { office: { uid: jwt?.office_Id } },
|
||||
include: { seats: { include: { user: true } } },
|
||||
});
|
||||
if (!subscription[0]) return;
|
||||
console.log(subscription[0]);
|
||||
|
||||
subscription[0].seats?.forEach((seat) => setSelectedCollaborators((prev) => [...prev, seat.user.uid!]));
|
||||
setSubscription(subscription[0]);
|
||||
}, []);
|
||||
|
||||
const loadCollaborators = useCallback(async () => {
|
||||
const collaborators = await Users.getInstance().get({
|
||||
where: { office_membership: { uid: JwtService.getInstance().getUserJwtPayload()?.office_Id } },
|
||||
include: {
|
||||
contact: true,
|
||||
seats: true,
|
||||
},
|
||||
});
|
||||
console.log(collaborators);
|
||||
_setAvailableCollaborators(collaborators);
|
||||
}, []);
|
||||
|
||||
const handleChange = useCallback(
|
||||
(event: React.ChangeEvent<HTMLInputElement>) => {
|
||||
if (!subscription) return;
|
||||
@ -100,12 +62,18 @@ export default function SubscriptionManageCollaborators() {
|
||||
};
|
||||
|
||||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement> | null, values: { [key: string]: string }) => {
|
||||
const subcriptionToUpdate = {
|
||||
seats: selectedCollaborators.map((collaborator) => ({ user: { uid: collaborator } })),
|
||||
};
|
||||
await Subscriptions.getInstance().put(subscription?.uid!, subcriptionToUpdate);
|
||||
router.push("/subscription/manage");
|
||||
if (!e) return;
|
||||
e.preventDefault();
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
loadSubscription();
|
||||
loadCollaborators();
|
||||
}, [loadSubscription]);
|
||||
|
||||
return (
|
||||
@ -141,7 +109,7 @@ export default function SubscriptionManageCollaborators() {
|
||||
{selectedCollaborators.length} collaborateurs sélectionnés
|
||||
</Typography>
|
||||
<div className={classes["buttons-container"]}>
|
||||
<Button variant={EButtonVariant.PRIMARY} fullwidth>
|
||||
<Button type="submit" variant={EButtonVariant.PRIMARY} fullwidth>
|
||||
Enregistrer
|
||||
</Button>
|
||||
<Button variant={EButtonVariant.GHOST} fullwidth onClick={cancelAll} type="button">
|
||||
|
Loading…
x
Reference in New Issue
Block a user