-
Notifications
You must be signed in to change notification settings - Fork 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add FormSummary component and integrate it into BasicForm; impl…
…ement FetchLabels for incident labels
- Loading branch information
Showing
4 changed files
with
174 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import React, { ReactElement } from "react"; | ||
import Detail from "../Detail/Detail"; | ||
import FormValues from "./Types/FormValues"; | ||
import GenericObject from "../../../Types/GenericObject"; | ||
import Fields from "./Types/Fields"; | ||
import FormFieldSchemaTypeUtil from "./Utils/FormFieldSchemaTypeUtil"; | ||
import FormFieldSchemaType from "./Types/FormFieldSchemaType"; | ||
import DetailField from "../Detail/Field"; | ||
import Field from "./Types/Field"; | ||
import FieldType from "../Types/FieldType"; | ||
|
||
export interface ComponentProps<T> { | ||
formValues: FormValues<T>; | ||
formFields: Fields<T>; | ||
} | ||
|
||
const FormSummary: <T extends GenericObject>( | ||
props: ComponentProps<T>, | ||
) => ReactElement = <T extends GenericObject>( | ||
props: ComponentProps<T>, | ||
): ReactElement => { | ||
const { formValues, formFields } = props; | ||
|
||
const getDetailForFormFields: <T extends GenericObject>( | ||
formValues: FormValues<T>, | ||
formFields: Fields<T>, | ||
) => ReactElement = <T extends GenericObject>( | ||
formValues: FormValues<T>, | ||
formFields: Fields<T>, | ||
): ReactElement => { | ||
return ( | ||
<Detail | ||
item={formValues as T} | ||
fields={ | ||
formFields.map((field: Field<T>) => { | ||
const detailField: DetailField<T> = { | ||
title: field.title || "", | ||
fieldType: field.getSummaryElement ? | ||
FieldType.Element : | ||
FormFieldSchemaTypeUtil.toFieldType( | ||
field.fieldType || FormFieldSchemaType.Text, | ||
), | ||
description: field.description || "", | ||
getElement: field.getSummaryElement as any, | ||
sideLink: field.sideLink, | ||
key: (Object.keys( | ||
field.field || {}, | ||
)[0]?.toString() || "") as keyof T, | ||
}; | ||
return detailField; | ||
}) as DetailField<GenericObject>[] | ||
} | ||
/> | ||
|
||
); | ||
}; | ||
|
||
return getDetailForFormFields(formValues, formFields); | ||
}; | ||
|
||
export default FormSummary; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import LabelElement from "./Label"; | ||
import TableColumnListComponent from "Common/UI/Components/TableColumnList/TableColumnListComponent"; | ||
import Label from "Common/Models/DatabaseModels/Label"; | ||
import React, { FunctionComponent, ReactElement, useEffect } from "react"; | ||
import ObjectID from "Common/Types/ObjectID"; | ||
import API from "Common/UI/Utils/API/API"; | ||
import ModelAPI from "Common/UI/Utils/ModelAPI/ModelAPI"; | ||
import Includes from "Common/Types/BaseDatabase/Includes"; | ||
import { LIMIT_PER_PROJECT } from "Common/Types/Database/LimitMax"; | ||
import SortOrder from "Common/Types/BaseDatabase/SortOrder"; | ||
import ListResult from "Common/UI/Utils/BaseDatabase/ListResult"; | ||
import ErrorMessage from "Common/UI/Components/ErrorMessage/ErrorMessage"; | ||
import ComponentLoader from "Common/UI/Components/ComponentLoader/ComponentLoader"; | ||
|
||
export interface ComponentProps { | ||
labelIds: Array<ObjectID>; | ||
} | ||
|
||
const FetchLabels: FunctionComponent<ComponentProps> = ( | ||
props: ComponentProps, | ||
): ReactElement => { | ||
|
||
const [isLoading, setIsLoading] = React.useState<boolean>(true); | ||
const [error, setError] = React.useState<string>(""); | ||
const [labels, setLabels] = React.useState<Array<Label>>([]); | ||
|
||
|
||
const fetchLabels = async () => { | ||
setIsLoading(true); | ||
setError(""); | ||
|
||
try{ | ||
const labels: ListResult<Label> = await ModelAPI.getList({ | ||
modelType: Label, | ||
query: { | ||
_id: new Includes(props.labelIds), | ||
}, | ||
skip: 0, | ||
limit: LIMIT_PER_PROJECT, | ||
select: { | ||
name: true, | ||
color: true, | ||
_id: true | ||
}, | ||
sort: { | ||
name: SortOrder.Ascending | ||
} | ||
}); | ||
|
||
setLabels(labels.data); | ||
|
||
}catch(err){ | ||
setError(API.getFriendlyMessage(err)); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
fetchLabels().catch((err) => { | ||
setError(API.getFriendlyMessage(err)); | ||
}); | ||
|
||
}, []); | ||
|
||
if(error){ | ||
return <ErrorMessage message={error} />; | ||
} | ||
|
||
|
||
if(isLoading){ | ||
return <ComponentLoader />; | ||
} | ||
|
||
return ( | ||
// {/** >4 because 3 labels are shown by default and then the more text is shown */} | ||
<TableColumnListComponent | ||
items={labels} | ||
moreText={labels.length > 4 ? "more labels" : "more label"} | ||
className={labels.length > 0 ? "-mb-1 -mt-1" : ""} | ||
getEachElement={(label: Label) => { | ||
return ( | ||
<div className={labels.length > 0 ? "my-2" : ""}> | ||
<LabelElement label={label} /> | ||
</div> | ||
); | ||
}} | ||
noItemsMessage="No labels attached." | ||
/> | ||
); | ||
}; | ||
|
||
export default FetchLabels; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters