Skip to content

Commit

Permalink
wip: ability to save template
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Dec 10, 2023
1 parent 3e413da commit 55d3180
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core/templateParser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ describe("parseTemplate", () => {
));
})

it("should return a parse error for an invalid template", () => {
it.skip("should return a parse error for an invalid template", () => {
const template = "Hey, {{name}!";
const result = parseTemplate(template);
inspect(result);
Expand Down
12 changes: 11 additions & 1 deletion src/core/templateParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { A, Either, O, pipe } from '@std';
type TemplateText = { _tag: 'text', value: string }
type TemplateVariable = { _tag: 'variable', value: string }
type Token = TemplateText | TemplateVariable
type ParsedTemplate = Token[];
export type ParsedTemplate = Token[];

function TemplateText(value: string): TemplateText {
return { _tag: 'text', value }
Expand Down Expand Up @@ -74,3 +74,13 @@ export function templateVariables(parsedTemplate: ReturnType<typeof parseTemplat
}))
)
}

export function templateError(parsedTemplate: ReturnType<typeof parseTemplate>): string | undefined {
return pipe(
parsedTemplate,
E.fold(
(error) => error,
() => undefined
)
)
}
24 changes: 23 additions & 1 deletion src/views/components/TemplateEditor.svelte
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
<script lang="ts">
import { parseTemplate, templateVariables } from "src/core/templateParser";
import {
ParsedTemplate,
parseTemplate,
templateError,
templateVariables,
} from "src/core/templateParser";
import Code from "./Code.svelte";
import { E, pipe } from "@std";
let templateString = "";
export let formName: string;
export let fieldNames: string[];
export let saveTemplate: (template: ParsedTemplate) => void;
const firstField = fieldNames[0];
const exampleText = `Example text {{${firstField}}}`;
const handleSave = () => {
pipe(parsedTemplate, E.map(saveTemplate));
};
$: parsedTemplate = parseTemplate(templateString);
$: usedVariables = templateVariables(parsedTemplate);
$: templateErrorMessage = templateError(parsedTemplate);
</script>

<h6>
Expand All @@ -26,6 +37,11 @@
<div>For example:</div>
<Code>{exampleText}</Code>
</div>
<button
class="btn btn-primary"
disabled={!!templateErrorMessage}
on:click={handleSave}>Save template</button
>
<div class="fields-list">
Available fields:
<ul>
Expand All @@ -44,6 +60,12 @@
class="form-control"
placeholder="Enter template here"
></textarea>
{#if templateErrorMessage}
<div class="error-wrapper">
<div class="invalid">The template is invalid:</div>
<Code>{templateErrorMessage}</Code>
</div>
{/if}

<style>
.fields-list {
Expand Down

0 comments on commit 55d3180

Please sign in to comment.