diff --git a/src/components/pickers/RolePicker.tsx b/src/components/pickers/RolePicker.tsx new file mode 100644 index 0000000..98fbe3f --- /dev/null +++ b/src/components/pickers/RolePicker.tsx @@ -0,0 +1,67 @@ +import React, { useEffect, useState, useCallback } from "react"; +import Select from "../common/Select"; +import Option from "../common/option"; +import { GeneralIdRef } from "../../utils/GeneralIdRef"; +import roleService from "../../modules/manager/domains/serrvices/rolesService"; + +interface RolePickerProps { + name: string; + label: string; + error?: string; + value: any; + domain: GeneralIdRef; + onChange?: (name: string, value: GeneralIdRef) => void; + includeLabel?: boolean; +} + +export default function RolePicker({ + name, + label, + error, + value, + domain, + onChange, + includeLabel = true, +}: RolePickerProps) { + const [options, setOptions] = useState(undefined); + + useEffect(() => { + async function load() { + const pagedData = await roleService.getRoles(0, 10, "name", true, domain); + if (pagedData) { + const opts: Option[] = (pagedData.data as any[]).map((x) => ({ + _id: x.id, + name: x.name, + })); + setOptions(opts); + } + } + + load(); + }, []); + + const handleChange = useCallback( + (e: React.ChangeEvent) => { + const input = e.currentTarget; + const generalIdRef: GeneralIdRef = { + id: BigInt(input.value), + }; + + if (onChange) onChange(input.name, generalIdRef); + }, + [onChange], + ); + + return ( + - props.onChange(props.name, { id: BigInt(e.target.value) }) - } - /> - - -); - +// TODO: Replace with your real RaciPicker const RaciPicker = (props: any) => (
@@ -97,6 +87,8 @@ export const AssigneesOfITaskAssigneeEditor: React.FC = ( const guid = task.config.guid as string; + const domain = MakeGeneralIdRef(getCurrentUser()?.domainid); + return (
Select assigness (you can select either a role or a contact) @@ -128,9 +120,11 @@ export const AssigneesOfITaskAssigneeEditor: React.FC = ( updateAssignee(index, { ...assignee, role: val }) @@ -143,6 +137,7 @@ export const AssigneesOfITaskAssigneeEditor: React.FC = ( name="contact" label="Contact" value={assignee.contact} + domain={domain} error={fieldErrors?.[`${guid}.assignees[${index}].contact`]} onChange={(name: string, val: GeneralIdRef | null) => updateAssignee(index, { ...assignee, contact: val }) @@ -204,16 +199,24 @@ const runValidation = ( assignees.forEach((a, i) => { const noContactSelected = !a.contact || a.contact?.id === BigInt(0); + const noRoleSelected = !a.role || a.role?.id === BigInt(0); - if (noContactSelected) { - errors[`${guid}.assignees[${i}].contact`] = "A contact must be selected."; - } - - if (!a.role && noContactSelected) { - errors[`${guid}.assignees[${i}].role`] = - "Either role or contact must be selected."; + if (!noContactSelected && !noRoleSelected) { errors[`${guid}.assignees[${i}].contact`] = - "Either role or contact must be selected."; + "Cannot select both a contact and a role."; + errors[`${guid}.assignees[${i}].role`] = + "Cannot select both a contact and a role."; + } else { + if (!(!noContactSelected || !noRoleSelected)) { + if (noContactSelected) { + errors[`${guid}.assignees[${i}].contact`] = + "A contact must be selected."; + } + + if (noRoleSelected) { + errors[`${guid}.assignees[${i}].role`] = "A role must be selected."; + } + } } if (!a.raci) {