Skip to content

Commit

Permalink
Docs module elements
Browse files Browse the repository at this point in the history
  • Loading branch information
guillemcordoba committed Apr 17, 2024
1 parent 361bf5c commit 7c72373
Show file tree
Hide file tree
Showing 62 changed files with 666 additions and 553 deletions.
6 changes: 4 additions & 2 deletions run_test_module.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ DIR=$(pwd)
nix shell .#hc-scaffold-module-template --command bash -c "
cd /tmp
rm -rf posts-open-dev
mkdir posts-open-dev
cd posts-open-dev

hc-scaffold web-app posts-open-dev --setup-nix true
hc-scaffold web-app posts --setup-nix true
"
cd /tmp/posts-open-dev
cd /tmp/posts-open-dev/posts
nix develop --override-input scaffolding "path:$DIR" --command bash -c "
set -e
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn create_a_{{snake_case referenceable.name}}_and_get_{{snake_case collect
// Alice creates a {{pascal_case referenceable.name}}
let record: Record = create_{{snake_case referenceable.name}}(&conductors[0], &alice_zome, sample.clone()).await;

await_consistency(Duration::from_secs(30), [&alice, &bobbo])
await_consistency(Duration::from_secs(60), [&alice, &bobbo])
.await
.expect("Timed out waiting for consistency");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}

{{else}}
async get_{{snake_case collection_name}}(_: any): Promise<Array<Link>> {
async get_{{snake_case collection_name}}(): Promise<Array<Link>> {
const records: Record[] = Array.from(this.{{camel_case (plural referenceable.name)}}.values()).map(r => r.revisions[r.revisions.length - 1]);
return Promise.all(records.map(async record => ({
target: {{#if (eq referenceable.hash_type "EntryHash")}}(record.signed_action.hashed.content as NewEntryAction).entry_hash{{else}}record.signed_action.hashed.hash{{/if}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { createContext } from '@lit/context';
import { {{pascal_case zome_manifest.name}}Store } from './{{kebab_case zome_manifest.name}}-store.js';

export const {{camel_case zome_manifest.name}}StoreContext = createContext<{{pascal_case zome_manifest.name}}Store>(
'hc_zome_{{snake_case zome_manifest.name}}/store'
'{{snake_case zome_manifest.name}}/store'
);

Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async fn create_and_read_{{snake_case entry_type.name}}() {
// Alice creates a {{pascal_case entry_type.name}}
let record: Record = create_{{snake_case entry_type.name}}(&conductors[0], &alice_zome, sample.clone()).await;

await_consistency(Duration::from_secs(30), [&alice, &bobbo])
await_consistency(Duration::from_secs(60), [&alice, &bobbo])
.await
.expect("Timed out waiting for consistency");

Expand Down Expand Up @@ -117,7 +117,7 @@ async fn create_and_update_{{snake_case entry_type.name}}() {
let record: Record = create_{{snake_case entry_type.name}}(&conductors[0], &alice_zome, sample_1.clone()).await;
let original_action_hash = record.signed_action.hashed.hash.clone();

await_consistency(Duration::from_secs(30), [&alice, &bobbo])
await_consistency(Duration::from_secs(60), [&alice, &bobbo])
.await
.expect("Timed out waiting for consistency");

Expand All @@ -138,7 +138,7 @@ async fn create_and_update_{{snake_case entry_type.name}}() {
let entry: {{pascal_case entry_type.name}} = update_record.entry().to_app_option().unwrap().unwrap();
assert_eq!(sample_2, entry);

await_consistency(Duration::from_secs(30), [&alice, &bobbo])
await_consistency(Duration::from_secs(60), [&alice, &bobbo])
.await
.expect("Timed out waiting for consistency");

Expand All @@ -164,7 +164,7 @@ async fn create_and_update_{{snake_case entry_type.name}}() {
let entry: {{pascal_case entry_type.name}} = update_record.entry().to_app_option().unwrap().unwrap();
assert_eq!(sample_1, entry);

await_consistency(Duration::from_secs(30), [&alice, &bobbo])
await_consistency(Duration::from_secs(60), [&alice, &bobbo])
.await
.expect("Timed out waiting for consistency");

Expand Down Expand Up @@ -206,7 +206,7 @@ async fn create_and_delete_{{snake_case entry_type.name}}() {
.call(&alice_zome, "delete_{{snake_case entry_type.name}}", original_action_hash.clone())
.await;

await_consistency(Duration::from_secs(30), [&alice, &bobbo])
await_consistency(Duration::from_secs(60), [&alice, &bobbo])
.await
.expect("Timed out waiting for consistency");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class Create{{pascal_case entry_type.name}} extends LitElement {
{{/if}}
{{/each}}

async create{{pascal_case entry_type.name}}(fields: any) {
async create{{pascal_case entry_type.name}}(fields: Partial<{{pascal_case entry_type.name}}>) {
{{#each entry_type.fields}}
{{#if (not widget)}}
{{#if (ne cardinality "option")}}
Expand All @@ -94,12 +94,12 @@ export class Create{{pascal_case entry_type.name}} extends LitElement {
{{#each entry_type.fields}}
{{#if widget}}
{{#if (eq cardinality "vector")}}
{{snake_case field_name}}: (Array.isArray(fields.{{snake_case field_name}}) ? fields.{{snake_case field_name}} : [fields.{{snake_case field_name}}]).map((el: any) => {{> (concat field_type.type "/" widget "/edit/to-value") field_name=field_name variable_to_read="el" }}),
{{snake_case field_name}}: (Array.isArray(fields.{{snake_case field_name}}!) ? fields.{{snake_case field_name}}! : [fields.{{snake_case field_name}}!]).map((el: any) => {{> (concat field_type.type "/" widget "/edit/to-value") field_name=field_name variable_to_read="el" }}),
{{else}}
{{snake_case field_name}}: {{> (concat field_type.type "/" widget "/edit/to-value") field_name=field_name variable_to_read=(concat "fields." (snake_case field_name)) }},
{{snake_case field_name}}: {{> (concat field_type.type "/" widget "/edit/to-value") field_name=field_name variable_to_read=(concat "fields." (snake_case field_name) "!" ) }},
{{/if}}
{{else}}
{{snake_case field_name}}: this.{{camel_case field_name}},
{{snake_case field_name}}: this.{{camel_case field_name}}!,
{{/if}}
{{/each}}
};
Expand All @@ -117,7 +117,7 @@ export class Create{{pascal_case entry_type.name}} extends LitElement {
}));

this.form.reset();
} catch (e: any) {
} catch (e: unknown) {
console.error(e);
notifyError(msg("Error creating the {{lower_case entry_type.name}}"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export class Edit{{pascal_case entry_type.name}} extends LitElement {
this.shadowRoot?.querySelector('form')!.reset();
}

async update{{pascal_case entry_type.name}}(fields: any) {
async update{{pascal_case entry_type.name}}(fields: unknown) {
const {{camel_case entry_type.name}}: {{pascal_case entry_type.name}} = {
{{#each entry_type.fields}}
{{#if widget}}
Expand Down Expand Up @@ -108,7 +108,7 @@ export class Edit{{pascal_case entry_type.name}} extends LitElement {
updated{{pascal_case entry_type.name}}Hash: updateRecord.actionHash
}
}));
} catch (e: any) {
} catch (e: unknown) {
console.error(e);
notifyError(msg("Error updating the {{lower_case entry_type.name}}"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class {{pascal_case entry_type.name}}Detail extends LitElement {
{{camel_case entry_type.name}}Hash: this.{{camel_case entry_type.name}}Hash
}
}));
} catch (e: any) {
} catch (e: unknown) {
console.error(e);
notifyError(msg("Error deleting the {{lower_case entry_type.name}}"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async fn link_a_{{snake_case from_referenceable.name}}_to_a_{{snake_case to_refe
target_{{snake_case to_referenceable.singular_arg}}: target_address.clone()
}).await;

await_consistency(Duration::from_secs(30), [&alice, &bobbo])
await_consistency(Duration::from_secs(60), [&alice, &bobbo])
.await
.expect("Timed out waiting for consistency");

Expand Down Expand Up @@ -100,7 +100,7 @@ async fn link_a_{{snake_case from_referenceable.name}}_to_a_{{snake_case to_refe
target_{{snake_case to_referenceable.singular_arg}}: target_address.clone()
}).await;

await_consistency(Duration::from_secs(30), [&alice, &bobbo])
await_consistency(Duration::from_secs(60), [&alice, &bobbo])
.await
.expect("Timed out waiting for consistency");

Expand Down
9 changes: 9 additions & 0 deletions templates/app/web-app/.prettierrc.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"singleQuote": true,
"arrowParens": "avoid",
"plugins": ["@trivago/prettier-plugin-sort-imports"],
"importOrder": ["<THIRD_PARTY_MODULES>", "^[./]"],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true,
"importOrderParserPlugins": ["typescript", "decorators-legacy"]
}
16 changes: 16 additions & 0 deletions templates/app/web-app/eslint.config.js.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// @ts-check

import eslint from '@eslint/js';
import tseslint from 'typescript-eslint';
import eslintConfigPrettier from "eslint-config-prettier";

export default tseslint.config(
eslint.configs.recommended,
...tseslint.configs.recommended,
eslintConfigPrettier,
{
rules: {
'@typescript-eslint/no-unused-vars': "warn"
}
}
);
2 changes: 0 additions & 2 deletions templates/app/web-app/flake.nix.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@

hc-infra = {
url = "github:holochain-open-dev/infrastructure";
inputs.holochain.follows = "holochain";
};
scaffolding = {
url = "github:holochain-open-dev/templates";
inputs.holochain.follows = "holochain";
};

};
Expand Down
1 change: 1 addition & 0 deletions templates/app/web-app/package.json.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"new-port-cli": "^1.0.0",
"rimraf": "^3.0.2"
},
"type": "module",
"engines": {
"pnpm": ">=9.0.0"
}
Expand Down
2 changes: 1 addition & 1 deletion templates/app/web-app/tests/package.json.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@holochain/tryorama": "{{tryorama_version}}",
"@holochain-open-dev/stores": "^0.300.0-dev.1",
"@holochain-open-dev/utils": "^0.300.0-dev.0",
"typescript": "^4.9.4",
"typescript": "^5.4.5",
"vitest": "^1.4.0"
},
"type": "module"
Expand Down
25 changes: 11 additions & 14 deletions templates/app/web-app/ui/package.json.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"scripts": {
"start": "vite --port $UI_PORT --clearScreen false",
"build": "vite build",
"lint": "eslint --ext .ts,.html . --ignore-path .gitignore && prettier \"**/*.ts\" --check --ignore-path .gitignore",
"format": "eslint --fix --ext .ts,.html . --ignore-path .gitignore && prettier \"**/*.ts\" --write --ignore-path .gitignore",
"lint": "eslint src --ignore-path .gitignore && prettier \"**/*.ts\" --check --ignore-path .gitignore",
"format": "eslint src --fix --ignore-path .gitignore && prettier \"**/*.ts\" --write --ignore-path .gitignore",
"package": "pnpm build && cd dist && bestzip ../dist.zip *",
"localize:extract": "lit-localize extract",
"localize:build": "lit-localize build"
},
"dependencies": {
"@holochain/client": "^0.17.0-dev.7",
"@holochain/client": "^0.17.0-dev.9",
"@holochain-open-dev/elements": "^0.300.0-dev.0",
"@holochain-open-dev/stores": "^0.300.0-dev.3",
"@holochain-open-dev/utils": "^0.300.0-dev.1",
Expand All @@ -23,21 +23,18 @@
"lit": "^3.0.0"
},
"devDependencies": {
"@custom-elements-manifest/analyzer": "^0.9.4",
"@lit/localize-tools": "^0.6.3",
"@open-wc/eslint-config": "^4.3.0",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@typescript-eslint/eslint-plugin": "^5.43.0",
"@typescript-eslint/parser": "^5.43.0",
"bootstrap-icons": "^1.10.3",
"@eslint/js": "^9.0.0",
"bestzip": "^2.2.0",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint": "^9.0.0",
"eslint-config-prettier": "^9.1.0",
"prettier": "^3.2.5",
"rimraf": "^3.0.2",
"typescript": "^5.4.5",
"typescript-eslint": "^7.7.0",
"tslib": "^2.6.2",
"vite": "^4.0.0",
"vite-plugin-checker": "^0.5.3",
"vite-plugin-static-copy": "^0.13.0",
"tslib": "^2.6.0",
"typescript": "^4.9.0"
"vite-plugin-static-copy": "^0.13.0"
}
}
4 changes: 2 additions & 2 deletions templates/app/web-app/ui/src/holochain-app.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type View = { view: 'main' };
export class HolochainApp extends LitElement {
@state() _loading = true;
@state() _view = { view: 'main' };
@state() _error: any | undefined;
@state() _error: unknown | undefined;

@provide({ context: profilesStoreContext })
@property()
Expand All @@ -42,7 +42,7 @@ export class HolochainApp extends LitElement {
try {
this._client = await AppAgentWebsocket.connect('{{app_name}}');
await this.initStores(this._client);
} catch (e: any) {
} catch (e: unknown) {
this._error = e;
} finally {
this._loading = false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# `{{kebab_case app_name}}`
# `{{kebab_case collection_name}}`

## Usage

Expand Down
2 changes: 1 addition & 1 deletion templates/module/collection/ui/src/mocks.ts.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
}

{{else}}
async get_{{snake_case collection_name}}(_: any): Promise<Array<Link>> {
async get_{{snake_case collection_name}}(): Promise<Array<Link>> {
const records: Record[] = Array.from(this.{{camel_case (plural referenceable.name)}}.values()).map(r => r.revisions[r.revisions.length - 1]);
return Promise.all(records.map(async record => ({
target: {{#if (eq referenceable.hash_type "EntryHash")}}(record.signed_action.hashed.content as NewEntryAction).entry_hash{{else}}record.signed_action.hashed.hash{{/if}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async fn create_a_{{snake_case referenceable.name}}_and_get_{{snake_case collect
// Alice creates a {{pascal_case referenceable.name}}
let record: Record = create_{{snake_case referenceable.name}}(&conductors[0], &alice_zome, sample.clone()).await;

await_consistency(Duration::from_secs(30), [&alice, &bobbo])
await_consistency(Duration::from_secs(60), [&alice, &bobbo])
.await
.expect("Timed out waiting for consistency");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# `create-{{kebab_case entry_type.name}}`

## Usage

0. If you haven't already, [go through the setup for the module](/setup).

1. Import the `create-{{kebab_case entry_type.name}}` element somewhere in the javascript side of your web-app like this:

```js
import '@holochain-open-dev/{{kebab_case app_name}}/dist/elements/create-{{kebab_case entry_type.name}}.js'
```

2. Use it in the html side of your web-app like this:

```html
<create-{{kebab_case entry_type.name}}>
</create-{{kebab_case entry_type.name}}>
```

> [!WARNING]
> Like all the elements in this module, `<create-{{kebab_case entry_type.name}}>` needs to be placed inside an initialized `<{{kebab_case app_name}}-context>`.

## Demo

Here is an interactive demo of the element:

<element-demo>
</element-demo>

<script setup>
import { onMounted } from "vue";
import { {{pascal_case app_name}}ZomeMock, sample{{pascal_case entry_type.name}} } from "@holochain-open-dev/{{kebab_case app_name}}/dist/mocks.js";
import { {{pascal_case app_name}}Store, {{pascal_case app_name}}Client } from "@holochain-open-dev/{{kebab_case app_name}}";
import { decodeHashFromBase64 } from '@holochain/client';
import { render, html } from "lit";
onMounted(async () => {
// Elements need to be imported on the client side, not the SSR side
// Reference: https://vitepress.dev/guide/ssr-compat#importing-in-mounted-hook
await import('@api-viewer/docs/lib/api-docs.js');
await import('@api-viewer/demo/lib/api-demo.js');
await import('@holochain-open-dev/{{kebab_case app_name}}/dist/elements/{{kebab_case app_name}}-context.js');
await import('@holochain-open-dev/{{kebab_case app_name}}/dist/elements/create-{{kebab_case entry_type.name}}.js');
const mock = new {{pascal_case app_name}}ZomeMock();
const client = new {{pascal_case app_name}}Client(mock);
const {{camel_case entry_type.name}} = await sample{{pascal_case entry_type.name}}(client);
const record = await mock.create_{{snake_case entry_type.name}}({{camel_case entry_type.name}});
const store = new {{pascal_case app_name}}Store(client);
render(html`
<{{kebab_case app_name}}-context .store=${store}>
<api-demo src="custom-elements.json" only="create-{{kebab_case entry_type.name}}" exclude-knobs="store">
</api-demo>
</{{kebab_case app_name}}-context>
`, document.querySelector('element-demo'))
})
</script>

## API Reference

`create-{{kebab_case entry_type.name}}` is a [custom element](https://web.dev/articles/custom-elements-v1), which means that it can be used in any web app or website. Here is the reference for its API:

<api-docs src="custom-elements.json" only="create-{{kebab_case entry_type.name}}">
</api-docs>
Loading

0 comments on commit 7c72373

Please sign in to comment.