edc-web-components 0.3.0

A Rust Web Components with patternfly-yew client for EDC
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
use crate::components::{DatasetCard, StringListEdit};
use crate::contexts::use_edc_connector_context;
use crate::models::{Creator, DataspaceDataset, Thumbnail};
use edc_connector_client::EdcConnectorApiVersion;
use edc_connector_client::types::properties::ToValue;
use edc_connector_client::types::{asset::NewAsset, data_address::DataAddress};
use patternfly_yew::prelude::*;
use std::collections::HashMap;
use yew::platform::spawn_local;
use yew::prelude::*;

#[derive(Clone, Debug, PartialEq, Properties)]
pub struct CreateAssetProps {
  #[prop_or_default]
  pub company_name: Option<String>,
  #[prop_or_default]
  pub company_logo_url: Option<String>,
  #[prop_or_default]
  pub on_create: Callback<()>,
}

#[component]
pub fn CreateAsset(props: &CreateAssetProps) -> Html {
  let edc_connector_context = use_edc_connector_context();

  let name = use_state(String::new);
  let version = use_state(String::new);
  let description = use_state(String::new);
  let thumbnail_url = use_state(String::new);
  let keywords = use_state(Vec::<String>::new);
  let base_url = use_state(String::new);
  let company_name = use_state(|| props.company_name.clone().unwrap_or_default());
  let company_logo_url = use_state(|| props.company_logo_url.clone().unwrap_or_default());
  let content_type = use_state(|| "application/json".to_string());
  let proxy_path = use_state(|| false);
  let proxy_query_params = use_state(|| false);
  let proxy_method = use_state(|| false);
  let proxy_body = use_state(|| false);
  let headers = use_state(HashMap::<String, String>::new);

  let onsubmit = use_callback(
    (
      edc_connector_context.clone(),
      (
        name.clone(),
        version.clone(),
        description.clone(),
        thumbnail_url.clone(),
        keywords.clone(),
      ),
      base_url.clone(),
      (company_name.clone(), company_logo_url.clone()),
      content_type.clone(),
      proxy_path.clone(),
      proxy_query_params.clone(),
      proxy_method.clone(),
      proxy_body.clone(),
      headers.clone(),
      props.on_create.clone(),
    ),
    |event: SubmitEvent,
     (
      edc_connector_context,
      (name, version, description, thumbnail_url, keywords),
      base_url,
      (company_name, company_logo_url),
      content_type,
      proxy_path,
      proxy_query_params,
      proxy_method,
      proxy_body,
      headers,
      on_create,
    )| {
      event.prevent_default();

      let name = (**name).clone();
      let version = (**version).clone();
      let description = (**description).clone();
      let thumbnail_url = (**thumbnail_url).clone();
      let keywords = (**keywords).clone();
      let base_url = (**base_url).clone();
      let company_name = (**company_name).clone();
      let company_logo_url = (**company_logo_url).clone();
      let content_type = (**content_type).clone();
      let proxy_path = **proxy_path;
      let proxy_query_params = **proxy_query_params;
      let proxy_method = **proxy_method;
      let proxy_body = **proxy_body;
      let headers = (**headers).clone();
      let edc_connector_context = edc_connector_context.clone();
      let on_create = on_create.clone();

      spawn_local(async move {
        let mut data_address_builder = DataAddress::builder()
          .kind("HttpData")
          .property("baseUrl", base_url)
          .property("proxyPath", if proxy_path { "true" } else { "false" })
          .property(
            "proxyQueryParams",
            if proxy_query_params { "true" } else { "false" },
          )
          .property("proxyMethod", if proxy_method { "true" } else { "false" })
          .property("proxyBody", if proxy_body { "true" } else { "false" });

        struct Creator {
          name: String,
          logo_url: String,
        }

        impl ToValue for Creator {
          fn into_value(self) -> serde_json::Value {
            serde_json::json!({
              "@type": [
                "http://xmlns.com/foaf/0.1/Organization",
                "http://www.w3.org/ns/prov#Agent"
              ],
              "http://xmlns.com/foaf/0.1/name": self.name,
              "http://xmlns.com/foaf/0.1/thumbnail": {
                "rdf:resource": self.logo_url,
              },
            })
          }
        }

        struct Thumbnail {
          resource: String,
        }

        impl ToValue for Thumbnail {
          fn into_value(self) -> serde_json::Value {
            serde_json::json!({
              "rdf:resource": self.resource,
            })
          }
        }

        for (key, value) in &headers {
          data_address_builder = data_address_builder.property(&format!("header:{key}"), value);
        }

        let data_address = data_address_builder.build().unwrap();

        let new_asset_builder = NewAsset::builder()
          // .id(&identifier)
          .data_address(data_address)
          .property("name", name.clone())
          .property("http://www.w3.org/ns/dcat#version", version)
          .property("contenttype", content_type)
          .property(
            "http://purl.org/dc/terms/creator",
            Creator {
              name: company_name,
              logo_url: company_logo_url,
            },
          );

        let new_asset_builder = if !name.is_empty() {
          new_asset_builder.property("http://purl.org/dc/terms/title", name)
        } else {
          new_asset_builder
        };

        let new_asset_builder = if !description.is_empty() {
          new_asset_builder.property("http://www.w3.org/2000/01/rdf-schema#comment", description)
        } else {
          new_asset_builder
        };

        let new_asset_builder = if !thumbnail_url.is_empty() {
          new_asset_builder.property(
            "http://xmlns.com/foaf/0.1/thumbnail",
            Thumbnail {
              resource: thumbnail_url,
            },
          )
        } else {
          new_asset_builder
        };

        let new_asset_builder = if !keywords.is_empty() {
          new_asset_builder.property("http://www.w3.org/ns/dcat#keyword", keywords)
        } else {
          new_asset_builder
        };

        let new_asset = new_asset_builder.build();

        if let Some(client) = edc_connector_context.get_client() {
          if let Err(error) = client
            .assets(EdcConnectorApiVersion::V4)
            .create(&new_asset)
            .await
          {
            log::error!("Error creating asset: {}", error);
          } else {
            on_create.emit(());
          }
        }
      })
    },
  );

  let onchange_name = use_callback(name.setter(), |value, name_setter| {
    name_setter.set(value);
  });

  let onchange_version = use_callback(version.setter(), |value, version_setter| {
    version_setter.set(value);
  });

  let onchange_description = use_callback(description.setter(), |value, description_setter| {
    description_setter.set(value);
  });

  let onchange_thumbnail_url =
    use_callback(thumbnail_url.setter(), |value, thumbnail_url_setter| {
      thumbnail_url_setter.set(value);
    });

  let onchange_keywords = use_callback(keywords.setter(), |keywords, keywords_setter| {
    keywords_setter.set(keywords);
  });

  let onchange_company_name = use_callback(company_name.setter(), |value, company_name_setter| {
    company_name_setter.set(value);
  });

  let onchange_company_logo_url = use_callback(
    company_logo_url.setter(),
    |value, company_logo_url_setter| {
      company_logo_url_setter.set(value);
    },
  );

  let onchange_base_url = use_callback(base_url.setter(), |value, base_url_setter| {
    base_url_setter.set(value);
  });

  let onchange_content_type = use_callback(content_type.setter(), |value, content_type_setter| {
    content_type_setter.set(value);
  });

  let onchange_proxy_path = use_callback(proxy_path.setter(), |value, proxy_path_setter| {
    proxy_path_setter.set(value);
  });

  let onchange_proxy_query_params = use_callback(
    proxy_query_params.setter(),
    |value, proxy_query_params_setter| {
      proxy_query_params_setter.set(value);
    },
  );

  let onchange_proxy_method = use_callback(proxy_method.setter(), |value, proxy_method_setter| {
    proxy_method_setter.set(value);
  });

  let onchange_proxy_body = use_callback(proxy_body.setter(), |value, proxy_body_setter| {
    proxy_body_setter.set(value);
  });

  let disabled = (*name).is_empty() || (*base_url).is_empty();

  let dataset = DataspaceDataset {
    id: "".to_string(),
    title: (*name).clone(),
    version: semver::Version::parse(&version).ok(),
    comment: if (*description).is_empty() {
      None
    } else {
      Some((*description).clone())
    },
    thumbnail: if (*thumbnail_url).is_empty() {
      None
    } else {
      Some(Thumbnail {
        resource: Some((*thumbnail_url).clone()),
      })
    },
    creator: if (*company_logo_url).is_empty() {
      None
    } else {
      Some(Creator {
        name: None,
        thumbnail: Some(Thumbnail {
          resource: Some((*company_logo_url).clone()),
        }),
      })
    },
    keywords: (*keywords).clone(),
    policies: vec![],
    dcterm_types: vec![],
  };

  html!(
    <Form {onsubmit}>
      <Card>
        <CardHeader>
          <Title level={Level::H2} size={Size::XXXLarge}>{ "Provider Information" }</Title>
        </CardHeader>
        <CardBody>
          <FormGroup label="Company Name">
            <TextInput
              required=true
              value={(*company_name).to_string()}
              onchange={onchange_company_name}
              readonly={props.company_name.is_some()}
            />
          </FormGroup>
          <FormGroup label="Company Logo URL">
            <TextInput
              required=true
              value={(*company_logo_url).to_string()}
              onchange={onchange_company_logo_url}
              readonly={props.company_logo_url.is_some()}
            />
          </FormGroup>
        </CardBody>
      </Card>
      <Card>
        <CardHeader>
          <Title level={Level::H2} size={Size::XXXLarge}>{ "Dataset Description" }</Title>
        </CardHeader>
        <CardBody>
          <Flex>
            <FlexItem
              modifiers={[FlexModifier::Flex1.all(), FlexModifier::Align(Alignment::Start).all()]}
            >
              <FormGroup label="Title" required=true>
                <TextInput required=true value={(*name).to_string()} onchange={onchange_name} />
              </FormGroup>
              <FormGroupValidated<TextInput>
                label="Version"
                required=true
                validator={Validator::from(|ctx: ValidationContext<String>| {
                  if ctx.initial {
                    ValidationResult::default()
                  } else if ctx.value.is_empty() {
                    ValidationResult::error("Must not be empty")
                  } else {
                    if semver::Version::parse(&ctx.value).is_ok() {
                      ValidationResult::ok()
                    } else {
                      ValidationResult::error("Not a semantic versioning format (MAJOR.MINOR.PATCH)")
                    }
                  }
               })}
              >
                <TextInput
                  required=true
                  value={(*version).to_string()}
                  onchange={onchange_version}
                />
              </FormGroupValidated<TextInput>>
              <FormGroup label="Description">
                <TextArea
                  required=true
                  value={(*description).to_string()}
                  onchange={onchange_description}
                />
              </FormGroup>
              <FormGroup label="Thumbnail URL">
                <TextInput
                  required=true
                  value={(*thumbnail_url).to_string()}
                  onchange={onchange_thumbnail_url}
                />
              </FormGroup>
              <FormGroup label="Keywords">
                <StringListEdit values={(*keywords).clone()} onchange={onchange_keywords} />
              </FormGroup>
            </FlexItem>
            <FlexItem modifiers={[FlexModifier::Align(Alignment::Start).all()]}>
              <DatasetCard {dataset} />
            </FlexItem>
          </Flex>
        </CardBody>
      </Card>
      <Card>
        <CardHeader>
          <Title level={Level::H2} size={Size::XXXLarge}>{ "Dataset Source" }</Title>
        </CardHeader>
        <CardBody>
          <FormGroup label="Base URL" required=true>
            <TextInput
              required=true
              value={(*base_url).to_string()}
              onchange={onchange_base_url}
              r#type={TextInputType::Url}
            />
          </FormGroup>
          <FormGroup label="Content Type">
            <TextInput value={(*content_type).to_string()} onchange={onchange_content_type} />
          </FormGroup>
          <FormGroup label="Proxy Path">
            <Switch checked={*proxy_path} onchange={onchange_proxy_path} />
          </FormGroup>
          <FormGroup label="Proxy Query Parameters">
            <Switch checked={*proxy_query_params} onchange={onchange_proxy_query_params} />
          </FormGroup>
          <FormGroup label="Proxy Method">
            <Switch checked={*proxy_method} onchange={onchange_proxy_method} />
          </FormGroup>
          <FormGroup label="Proxy Body">
            <Switch checked={*proxy_body} onchange={onchange_proxy_body} />
          </FormGroup>
        </CardBody>
      </Card>
      <ActionGroup>
        <Button
          variant={ButtonVariant::Primary}
          label="Submit"
          r#type={ButtonType::Submit}
          {disabled}
        />
        <Button variant={ButtonVariant::Secondary} label="Reset" r#type={ButtonType::Reset} />
      </ActionGroup>
    </Form>
  )
}