edc_web_ui/components/
create_asset.rs

1use crate::contexts::use_edc_connector_context;
2use edc_connector_client::types::{asset::NewAsset, data_address::DataAddress};
3use patternfly_yew::prelude::*;
4use std::collections::HashMap;
5use yew::platform::spawn_local;
6use yew::prelude::*;
7
8#[function_component]
9pub fn CreateAsset() -> Html {
10  let edc_connector_context = use_edc_connector_context();
11
12  let identifier = use_state(|| "".to_string());
13  let name = use_state(|| "".to_string());
14  let base_url = use_state(|| "".to_string());
15  let content_type = use_state(|| "".to_string());
16  let proxy_path = use_state(|| false);
17  let proxy_query_params = use_state(|| false);
18  let proxy_method = use_state(|| false);
19  let proxy_body = use_state(|| false);
20  let headers = use_state(HashMap::<String, String>::new);
21
22  let onsubmit = use_callback(
23    (
24      edc_connector_context.clone(),
25      identifier.clone(),
26      name.clone(),
27      base_url.clone(),
28      content_type.clone(),
29      proxy_path.clone(),
30      proxy_query_params.clone(),
31      proxy_method.clone(),
32      proxy_body.clone(),
33      headers.clone(),
34    ),
35    |event: SubmitEvent,
36     (
37      edc_connector_context,
38      identifier,
39      name,
40      base_url,
41      content_type,
42      proxy_path,
43      proxy_query_params,
44      proxy_method,
45      proxy_body,
46      headers,
47    )| {
48      event.prevent_default();
49
50      let identifier = (**identifier).clone();
51      let name = (**name).clone();
52      let base_url = (**base_url).clone();
53      let content_type = (**content_type).clone();
54      let proxy_path = **proxy_path;
55      let proxy_query_params = **proxy_query_params;
56      let proxy_method = **proxy_method;
57      let proxy_body = **proxy_body;
58      let headers = (**headers).clone();
59      let edc_connector_context = edc_connector_context.clone();
60
61      spawn_local(async move {
62        let mut data_address_builder = DataAddress::builder()
63          .kind("HttpData")
64          .property("baseUrl", base_url)
65          .property("proxyPath", if proxy_path { "true" } else { "false" })
66          .property(
67            "proxyQueryParams",
68            if proxy_query_params { "true" } else { "false" },
69          )
70          .property("proxyMethod", if proxy_method { "true" } else { "false" })
71          .property("proxyBody", if proxy_body { "true" } else { "false" });
72
73        for (key, value) in &headers {
74          data_address_builder = data_address_builder.property(&format!("header:{key}"), value);
75        }
76
77        let data_address = data_address_builder.build().unwrap();
78
79        let new_asset = NewAsset::builder()
80          .id(&identifier)
81          .data_address(data_address)
82          .property("name", name)
83          .property("contenttype", content_type)
84          .build();
85
86        if let Some(client) = edc_connector_context.get_client() {
87          let _ = client.assets().create(&new_asset).await;
88        }
89      })
90    },
91  );
92
93  let onchange_identifier = {
94    let identifier = identifier.clone();
95
96    use_callback((), move |value, _| {
97      identifier.set(value);
98    })
99  };
100
101  let onchange_name = {
102    let name = name.clone();
103
104    use_callback((), move |value, _| {
105      name.set(value);
106    })
107  };
108
109  let onchange_base_url = {
110    let base_url = base_url.clone();
111
112    use_callback((), move |value, _| {
113      base_url.set(value);
114    })
115  };
116
117  let onchange_content_type = {
118    let content_type = content_type.clone();
119
120    use_callback((), move |value, _| {
121      content_type.set(value);
122    })
123  };
124
125  let onchange_proxy_path = {
126    let proxy_path = proxy_path.clone();
127
128    use_callback((), move |value, _| {
129      proxy_path.set(value);
130    })
131  };
132
133  let onchange_proxy_query_params = {
134    let proxy_query_params = proxy_query_params.clone();
135
136    use_callback((), move |value, _| {
137      proxy_query_params.set(value);
138    })
139  };
140
141  let onchange_proxy_method = {
142    let proxy_method = proxy_method.clone();
143
144    use_callback((), move |value, _| {
145      proxy_method.set(value);
146    })
147  };
148
149  let onchange_proxy_body = {
150    let proxy_body = proxy_body.clone();
151
152    use_callback((), move |value, _| {
153      proxy_body.set(value);
154    })
155  };
156
157  let disabled = false;
158
159  html!(
160    <Form {onsubmit}>
161      <FormGroup
162        label={"Identifier"}
163        required={true}
164        >
165        <TextInput
166          required={true}
167          value={(*identifier).to_string()}
168          onchange={onchange_identifier}
169          />
170      </FormGroup>
171
172      <FormGroup
173        label={"Name"}
174        required={true}
175        >
176        <TextInput
177          required={true}
178          value={(*name).to_string()}
179          onchange={onchange_name}
180          />
181      </FormGroup>
182
183      <FormGroup
184        label={"Base URL"}
185        required={true}
186        >
187        <TextInput
188          required={true}
189          value={(*base_url).to_string()}
190          onchange={onchange_base_url}
191          r#type={TextInputType::Url}
192          />
193      </FormGroup>
194
195      <FormGroup
196        label={"Content Type"}
197        >
198        <TextInput
199          value={(*content_type).to_string()}
200          onchange={onchange_content_type}
201          />
202      </FormGroup>
203
204      <FormGroup
205        label={"Proxy Path"}
206        >
207        <Switch
208          checked={*proxy_path}
209          onchange={onchange_proxy_path}
210          />
211      </FormGroup>
212
213      <FormGroup
214        label={"Proxy Query Parameters"}
215        >
216        <Switch
217          checked={*proxy_query_params}
218          onchange={onchange_proxy_query_params}
219          />
220      </FormGroup>
221
222      <FormGroup
223        label={"Proxy Method"}
224        >
225        <Switch
226          checked={*proxy_method}
227          onchange={onchange_proxy_method}
228          />
229      </FormGroup>
230
231      <FormGroup
232        label={"Proxy Body"}
233        >
234        <Switch
235          checked={*proxy_body}
236          onchange={onchange_proxy_body}
237          />
238      </FormGroup>
239
240      <ActionGroup>
241        <Button
242          variant={ButtonVariant::Primary}
243          label="Submit"
244          r#type={ButtonType::Submit}
245          {disabled}
246          />
247        <Button variant={ButtonVariant::Secondary} label="Reset" r#type={ButtonType::Reset}/>
248      </ActionGroup>
249    </Form>
250  )
251}