generated from Luis/nextjs-python-web-template
Merge branch 'main' into frontend-2
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import { useGetAllEntities } from "@/api/entities/entities";
|
||||
import { Entity } from "@/api/model";
|
||||
import { AxiosError } from "axios";
|
||||
import React, {
|
||||
createContext,
|
||||
@@ -5,6 +7,7 @@ import React, {
|
||||
ReactNode,
|
||||
SetStateAction,
|
||||
useState,
|
||||
useEffect,
|
||||
} from "react";
|
||||
|
||||
type AppContextType = {
|
||||
@@ -18,7 +21,11 @@ type AppContextType = {
|
||||
|
||||
export const AppContext = createContext<AppContextType>({} as AppContextType);
|
||||
|
||||
type AppState = NonNullable<unknown>;
|
||||
type AppState = {
|
||||
allEntities: Entity[] | undefined;
|
||||
loadingEntities: boolean;
|
||||
entitiesKeyFunc: any;
|
||||
};
|
||||
|
||||
interface AppContextProviderProps {
|
||||
children: ReactNode;
|
||||
@@ -26,10 +33,28 @@ interface AppContextProviderProps {
|
||||
export const WithAppState = (props: AppContextProviderProps) => {
|
||||
const { children } = props;
|
||||
|
||||
const { data: entityData, swrKey: entitiesKeyFunc } = useGetAllEntities();
|
||||
|
||||
const isLoading = false;
|
||||
const error = undefined;
|
||||
|
||||
const [data, setAppState] = useState<AppState>({});
|
||||
const [data, setAppState] = useState<AppState>({
|
||||
allEntities: [],
|
||||
loadingEntities: true,
|
||||
entitiesKeyFunc,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (entityData) {
|
||||
setAppState((prevState) => ({
|
||||
...prevState,
|
||||
allEntities: entityData.data,
|
||||
entitiesKeyFunc,
|
||||
loadingEntities: false,
|
||||
}));
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [entityData]);
|
||||
|
||||
return (
|
||||
<AppContext.Provider
|
||||
|
||||
33
pkgs/ui/src/components/hooks/useFetch.tsx
Normal file
33
pkgs/ui/src/components/hooks/useFetch.tsx
Normal file
@@ -0,0 +1,33 @@
|
||||
import { useState, useEffect } from "react";
|
||||
import axios from "axios";
|
||||
import { BASE_URL } from "@/constants";
|
||||
|
||||
const useFetch = (url: string) => {
|
||||
const [data, setData] = useState([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState(null);
|
||||
|
||||
const fetch = () => {
|
||||
setLoading(true);
|
||||
axios
|
||||
.get(BASE_URL + url)
|
||||
.then((response) => {
|
||||
setData(response.data);
|
||||
})
|
||||
.catch((error) => {
|
||||
setError(error);
|
||||
})
|
||||
.finally(() => {
|
||||
setLoading(false);
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
fetch();
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [url]);
|
||||
|
||||
return { data, loading, error, fetch };
|
||||
};
|
||||
|
||||
export default useFetch;
|
||||
19
pkgs/ui/src/components/hooks/useGetEntityByNameOrDid.tsx
Normal file
19
pkgs/ui/src/components/hooks/useGetEntityByNameOrDid.tsx
Normal file
@@ -0,0 +1,19 @@
|
||||
import { useContext } from "react";
|
||||
import { AppContext } from "./useAppContext";
|
||||
|
||||
const useGetEntityByNameOrDid = (nameOrDid: string) => {
|
||||
const { data } = useContext(AppContext);
|
||||
const allEntities = data.allEntities;
|
||||
|
||||
if (!allEntities) {
|
||||
return { entity: undefined, isLoading: true };
|
||||
}
|
||||
|
||||
const entity = allEntities.find(
|
||||
(entity) => entity.name === nameOrDid || entity.did === nameOrDid,
|
||||
);
|
||||
|
||||
return { entity, isLoading: false };
|
||||
};
|
||||
|
||||
export default useGetEntityByNameOrDid;
|
||||
@@ -36,13 +36,13 @@ const menuEntityEntries: MenuEntry[] = [
|
||||
{
|
||||
icon: <PersonIcon />,
|
||||
label: "C1",
|
||||
to: "/client-1",
|
||||
to: "/client/C1",
|
||||
disabled: false,
|
||||
},
|
||||
{
|
||||
icon: <PersonIcon />,
|
||||
label: "C2",
|
||||
to: "/client-2",
|
||||
to: "/client/C2",
|
||||
disabled: false,
|
||||
},
|
||||
];
|
||||
|
||||
@@ -14,6 +14,8 @@ const SummaryDetails = ({
|
||||
entity,
|
||||
hasRefreshButton,
|
||||
hasAttachDetach,
|
||||
fake,
|
||||
onRefresh,
|
||||
}: ISummaryDetails) => {
|
||||
const cardContentRef = useRef(null);
|
||||
const hasDetails = entity.details && entity.details.length > 0;
|
||||
@@ -34,13 +36,17 @@ const SummaryDetails = ({
|
||||
Attach / Detach
|
||||
</Button>
|
||||
)}
|
||||
{hasRefreshButton && <Button variant="contained">Refresh</Button>}
|
||||
{hasRefreshButton && (
|
||||
<Button onClick={onRefresh} variant="contained">
|
||||
Refresh
|
||||
</Button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
{hasDetails && (
|
||||
<Card variant="outlined">
|
||||
<CardHeader
|
||||
subheader="Summary"
|
||||
subheader={fake ? "Summary (Fake Data)" : "Summary"}
|
||||
action={<CopyToClipboard contentRef={cardContentRef} />}
|
||||
/>
|
||||
<CardContent ref={cardContentRef}>
|
||||
|
||||
@@ -9,9 +9,12 @@ import Paper from "@mui/material/Paper";
|
||||
import { NoDataOverlay } from "@/components/noDataOverlay";
|
||||
import { StyledTableCell, StyledTableRow } from "./style";
|
||||
import { ICustomTable, CustomTableConfiguration } from "@/types";
|
||||
import { Checkbox } from "@mui/material";
|
||||
import { Checkbox, Skeleton } from "@mui/material";
|
||||
|
||||
const CustomTable = ({ configuration, data, loading, tkey }: ICustomTable) => {
|
||||
if (loading)
|
||||
return <Skeleton variant="rectangular" animation="wave" height={200} />;
|
||||
|
||||
const CustomTable = ({ configuration, data }: ICustomTable) => {
|
||||
// display empty icon in case there is no data
|
||||
if (!data || data.length === 0)
|
||||
return <NoDataOverlay label="No Activity yet" />;
|
||||
@@ -53,12 +56,18 @@ const CustomTable = ({ configuration, data }: ICustomTable) => {
|
||||
<TableBody>
|
||||
{data.map((data: any, rowIndex: number) => (
|
||||
<StyledTableRow key={rowIndex}>
|
||||
{configuration.map((column: CustomTableConfiguration) => {
|
||||
const cellValue: any = data[column.key];
|
||||
const cellKey = column.key;
|
||||
const renderComponent = column?.render;
|
||||
return renderTableCell(cellValue, cellKey, renderComponent);
|
||||
})}
|
||||
{configuration.map(
|
||||
(column: CustomTableConfiguration, columnIndex: number) => {
|
||||
const cellValue: any = data[column.key];
|
||||
const cellKey = tkey + ":" + column.key + ":" + rowIndex;
|
||||
const renderComponent = column?.render;
|
||||
return renderTableCell(
|
||||
cellValue,
|
||||
cellKey + ":" + columnIndex,
|
||||
renderComponent,
|
||||
);
|
||||
},
|
||||
)}
|
||||
</StyledTableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
|
||||
Reference in New Issue
Block a user