Implementing the partner map for author identifying
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { useTranslation } from "@refinedev/core";
|
||||
import { useContext } from "react";
|
||||
import { FirmContext } from "../contexts/FirmContext";
|
||||
|
||||
type CartoucheProps = {
|
||||
record: any
|
||||
@@ -11,11 +13,22 @@ const Cartouche = (props: CartoucheProps) => {
|
||||
<>
|
||||
<h2>{record.label}</h2>
|
||||
<ul>
|
||||
{ record.created_at && <li>{t("resource.created_at")}: {record.created_at} {t("resource.created_at")}: {record.created_by}</li> }
|
||||
{ record.updated_at && <li>{t("resource.updated_at")}: {record.updated_at} {t("resource.updated_by")}: {record.updated_by}</li> }
|
||||
{ record.created_at && <li>{t("resource.created_at")}: {record.created_at} {t("resource.created_at")}: <AuthorField partnerId={record.created_by} /></li> }
|
||||
{ record.updated_at && <li>{t("resource.updated_at")}: {record.updated_at} {t("resource.updated_by")}: <AuthorField partnerId={record.updated_by} /></li> }
|
||||
</ul>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default Cartouche;
|
||||
|
||||
const AuthorField = (props: {partnerId: string})=> {
|
||||
const { partnerId } = props;
|
||||
const { partnerMap } = useContext(FirmContext);
|
||||
const { translate: t } = useTranslation();
|
||||
|
||||
if (partnerMap && partnerMap.has(partnerId)) {
|
||||
return <>{ partnerMap.get(partnerId) }</>
|
||||
}
|
||||
return <>{t("REDACTED")}</>
|
||||
}
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
import React, { createContext, PropsWithChildren } from 'react';
|
||||
import { IFirm } from "../interfaces";
|
||||
import { useParams } from "react-router";
|
||||
import { useOne } from "@refinedev/core";
|
||||
import { CircularProgress } from "@mui/material";
|
||||
|
||||
type FirmContextType = {
|
||||
currentFirm: IFirm,
|
||||
partnerMap?: Map<string, string>
|
||||
}
|
||||
|
||||
export const FirmContext = createContext<FirmContextType>(
|
||||
@@ -12,13 +15,27 @@ export const FirmContext = createContext<FirmContextType>(
|
||||
|
||||
|
||||
export const FirmContextProvider: React.FC<PropsWithChildren> = ({ children }: PropsWithChildren) => {
|
||||
const { instance, firm } = useParams<IFirm>()
|
||||
const { instance, firm } = useParams<IFirm>();
|
||||
const { data, isError, error, isLoading } = useOne({resource: 'firm', id: `${instance}/${firm}/`, errorNotification: false});
|
||||
|
||||
if (instance === undefined || firm === undefined) {
|
||||
return "Error"
|
||||
}
|
||||
|
||||
if (isLoading) {
|
||||
return <CircularProgress />
|
||||
}
|
||||
|
||||
let value: FirmContextType = {
|
||||
currentFirm: {instance, firm}
|
||||
}
|
||||
if (!isError || error?.statusCode != 405) {
|
||||
value.currentFirm.entity = data?.data.entity;
|
||||
value.partnerMap = new Map(data?.data.partner_list.map((item: any) => [item.id, item.label]));
|
||||
}
|
||||
|
||||
return (
|
||||
<FirmContext.Provider value={{currentFirm: {instance, firm}}} >
|
||||
<FirmContext.Provider value={value} >
|
||||
{ children }
|
||||
</FirmContext.Provider>
|
||||
);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
export type IFirm = {
|
||||
instance: string,
|
||||
firm: string
|
||||
entity?: any
|
||||
}
|
||||
|
||||
type User = {
|
||||
|
||||
@@ -33,17 +33,8 @@ export const FirmRoutes = () => {
|
||||
|
||||
const FirmHome = () => {
|
||||
const { currentFirm } = useContext(FirmContext);
|
||||
const { data: firm, isError, error, isLoading } = useOne({resource: 'firm', id: `${currentFirm.instance}/${currentFirm.firm}/`, errorNotification: false})
|
||||
const { translate: t } = useTranslation();
|
||||
|
||||
if (isLoading) {
|
||||
return <h1>Loading...</h1>
|
||||
}
|
||||
|
||||
if (isError && error?.statusCode == 405) {
|
||||
return <FirmInitForm currentFirm={currentFirm} />
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<h1>This is la firme {currentFirm.instance} / {currentFirm.firm}</h1>
|
||||
|
||||
Reference in New Issue
Block a user