Skip to content

Commit

Permalink
added tenantpolicy
Browse files Browse the repository at this point in the history
  • Loading branch information
KelvinTegelaar committed Jan 6, 2025
1 parent 073e8f6 commit e07b5a0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/components/CippComponents/CippFormCondition.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export const CippFormCondition = (props) => {
}

switch (compareType) {
case "regex":
if (watcher?.match(new RegExp(compareValue))) {
return children;
}
return null;
case "is":
// Deep comparison for objects and arrays
if (isEqual(watcher, compareValue)) {
Expand Down
8 changes: 7 additions & 1 deletion src/components/CippComponents/CippFormTenantSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const CippFormTenantSelector = ({
allTenants = false,
type = "multiple",
name = "tenantFilter",
valueField = "defaultDomainName",
required = true,
disableClearable = true,
...other
Expand All @@ -30,7 +31,12 @@ export const CippFormTenantSelector = ({
url: allTenants ? "/api/ListTenants?AllTenantSelector=true" : "/api/ListTenants",
queryKey: allTenants ? "ListTenants-AllTenantSelector" : "ListTenants-notAllTenants",
labelField: (option) => `${option.displayName} (${option.defaultDomainName})`,
valueField: "defaultDomainName",
valueField: valueField,
addedField: {
defaultDomainName: "defaultDomainName",
displayName: "displayName",
customerId: "customerId",
},
}}
multiple={type === "single" ? false : true}
disableClearable={disableClearable}
Expand Down
5 changes: 5 additions & 0 deletions src/components/CippComponents/CippTenantSelector.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ export const CippTenantSelector = (props) => {
? tenantList.data.map(({ customerId, displayName, defaultDomainName }) => ({
value: defaultDomainName,
label: `${displayName} (${defaultDomainName})`,
addedField: {
defaultDomainName: "defaultDomainName",
displayName: "displayName",
customerId: "customerId",
},
}))
: []
}
Expand Down
34 changes: 33 additions & 1 deletion src/components/CippWizard/CippIntunePolicy.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CippWizardStepButtons } from "./CippWizardStepButtons";
import CippJsonView from "../CippFormPages/CippJSONView";
import CippFormComponent from "../CippComponents/CippFormComponent";
import { ApiGetCall } from "../../api/ApiCall";
import { useEffect, useState } from "react";
import { use, useEffect, useState } from "react";
import { useWatch } from "react-hook-form";
import { CippFormCondition } from "../CippComponents/CippFormCondition";

Expand All @@ -13,6 +13,8 @@ export const CippIntunePolicy = (props) => {
const CATemplates = ApiGetCall({ url: "/api/ListIntuneTemplates", queryKey: "IntuneTemplates" });
const [JSONData, setJSONData] = useState();
const watcher = useWatch({ control: formControl.control, name: "TemplateList" });
const jsonWatch = useWatch({ control: formControl.control, name: "RAWJson" });
const selectedTenants = useWatch({ control: formControl.control, name: "tenantFilter" });
useEffect(() => {
if (CATemplates.isSuccess && watcher?.value) {
const template = CATemplates.data.find((template) => template.GUID === watcher.value);
Expand Down Expand Up @@ -86,6 +88,36 @@ export const CippIntunePolicy = (props) => {
/>
</Grid>
</CippFormCondition>
<CippFormCondition
formControl={formControl}
field="RAWJson"
compareType="regex"
compareValue={/%(\w+)%/}
>
{(() => {
const rawJson = jsonWatch ? jsonWatch : "";
const placeholderMatches = [...rawJson.matchAll(/%(\w+)%/g)].map((m) => m[1]);
const uniquePlaceholders = Array.from(new Set(placeholderMatches));
if (uniquePlaceholders.length === 0 || selectedTenants.length === 0) {
return null;
}
console.log(selectedTenants);
return uniquePlaceholders.map((placeholder) => (
<Grid key={placeholder} item xs={6}>
{selectedTenants.map((tenant, idx) => (
<CippFormComponent
key={`${tenant.value}-${placeholder}-${idx}`}
type="textField"
name={`replacemap.${tenant.value}.${placeholder}`}
label={`Value for '${placeholder}' in Tenant '${tenant.addedFields.defaultDomainName}'`}
formControl={formControl}
validators={{ required: `Please provide a value for ${placeholder}` }}
/>
))}
</Grid>
));
})()}
</CippFormCondition>
</Stack>
<CippWizardStepButtons
currentStep={currentStep}
Expand Down
8 changes: 7 additions & 1 deletion src/components/CippWizard/CippTenantStep.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const CippTenantStep = (props) => {
const {
allTenants,
type = "single",
valueField = "defaultDomainName",
onNextStep,
formControl,
currentStep,
Expand All @@ -17,7 +18,12 @@ export const CippTenantStep = (props) => {
<Stack spacing={3}>
{preText}
<label>Select a tenant</label>
<CippFormTenantSelector formControl={formControl} allTenants={allTenants} type={type} />
<CippFormTenantSelector
valueField={valueField}
formControl={formControl}
allTenants={allTenants}
type={type}
/>
<CippWizardStepButtons
currentStep={currentStep}
onPreviousStep={onPreviousStep}
Expand Down
2 changes: 1 addition & 1 deletion src/pages/endpoint/MEM/add-policy/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Page = () => {
title: "Step 1",
description: "Tenant Selection",
component: CippTenantStep,
componentProps: { type: "multiple" },
componentProps: { type: "multiple", valueField: "customerId" },
},
{
title: "Step 2",
Expand Down

0 comments on commit e07b5a0

Please sign in to comment.