Skip to main content

edc_web_components/components/create_policy/
list_of_rules.rs

1use super::Rule;
2use crate::components::create_policy::atomic_constraint_edit::ConstraintMode;
3use edc_connector_client::types::policy::{Action, Constraint};
4use patternfly_yew::prelude::*;
5use yew::prelude::*;
6
7pub type UpdatedRules = Vec<(Action, Vec<(ConstraintMode, Constraint)>)>;
8
9#[derive(Clone, PartialEq, Properties)]
10pub struct Props {
11  pub list: Vec<(Action, Vec<(ConstraintMode, Constraint)>)>,
12  pub onchange: Callback<UpdatedRules>,
13}
14
15#[component]
16pub fn ListOfRules(props: &Props) -> Html {
17  let add_rule = use_callback(
18    (props.list.clone(), props.onchange.clone()),
19    |_, (list, onchange)| {
20      let mut list_of_rules = (*list).clone();
21      list_of_rules.push((Action::Simple("use".to_string()), vec![]));
22      onchange.emit(list_of_rules);
23    },
24  );
25
26  let onchange = use_callback(
27    (props.list.clone(), props.onchange.clone()),
28    |(index, action, constraints), (list, onchange)| {
29      let mut list_of_rules = (*list).clone();
30      list_of_rules[index] = (action, constraints);
31      onchange.emit(list_of_rules);
32    },
33  );
34
35  let ondelete = use_callback(
36    (props.list.clone(), props.onchange.clone()),
37    |index, (list, onchange)| {
38      let mut list_of_rules = (*list).clone();
39      list_of_rules.remove(index);
40      onchange.emit(list_of_rules);
41    },
42  );
43
44  log::info!("{:?}", props.list);
45
46  let list_of_rules = props
47    .list
48    .iter()
49    .enumerate()
50    .map(|(index, (action, constraints))| {
51      let action = action.clone();
52      let constraints = constraints.clone();
53
54      html_nested!(
55        <StackItem>
56          <Card>
57            <CardTitle>{ format!("Rule {}", index + 1) }</CardTitle>
58            <CardBody>
59              <Rule
60                key={index}
61                {index}
62                {action}
63                {constraints}
64                onchange={onchange.clone()}
65                ondelete={ondelete.clone()}
66              />
67            </CardBody>
68          </Card>
69        </StackItem>
70      )
71    });
72
73  html!(
74    <Stack gutter=true>
75      { for list_of_rules }
76      <StackItem>
77        <Button icon={Icon::Plus} variant={ButtonVariant::Primary} onclick={add_rule}>
78          { "Add rule" }
79        </Button>
80      </StackItem>
81    </Stack>
82  )
83}