Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT feat: add fillers behaviour #1004

Open
wants to merge 3 commits into
base: 3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions e2e/fillers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { expect, test } from '@playwright/test';
import {
expectPageToHaveText,
gotoNextPage,
gotoPreviousPage,
goToStory,
} from './utils';

test.describe('Fillers', () => {
test(`can fill data`, async ({ page }) => {
await goToStory(page, 'behaviour-fillers--default#t100');

// First filling
await page.getByLabel('Code postal').fill('34000');
await gotoNextPage(page);
await expectPageToHaveText(page, 'Chargement');
await expect(page.getByLabel('Ville')).toHaveValue('Montpellier');

// Second filling
await gotoPreviousPage(page);
await page.getByLabel('Code postal').fill('31000');
await gotoNextPage(page);
await expect(page.getByLabel('Ville')).toHaveValue('Toulouse');
});
});
36 changes: 36 additions & 0 deletions lunatic-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@
},
"maxPage": {
"type": "string"
},
"fillers": {
"type": "array",
"items": {
"$ref": "#/$defs/FillerDefinition"
}
}
},
"required": ["components", "variables"],
Expand Down Expand Up @@ -1461,6 +1467,36 @@
}
},
"required": ["name", "fields", "queryParser", "version"]
},
"FillerDefinition": {
"type": "object",
"properties": {
"endpoint": {
"type": "object",
"properties": {
"url": {
"type": "string"
},
"type": {
"enum": ["VTL", "TXT"]
}
},
"required": ["url"]
},
"responses": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": ["name"]
}
}
},
"required": ["responses", "endpoint"]
}
}
}
11 changes: 11 additions & 0 deletions src/components/FillerLoader/FillerLoader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { slottableComponent } from '../shared/HOC/slottableComponent';

/**
* Displays a loader while fetching data to fill the form
*/
export const FillerLoader = slottableComponent(
'FillerLoader',
function FillerLoader(props: { id: string }) {
return <p>Chargement des données...</p>;
}
);
2 changes: 2 additions & 0 deletions src/components/library.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { PairwiseLinks } from './PairwiseLinks/PairwiseLinks';
import { CheckboxOne } from './CheckboxOne/CheckboxOne';
import { Suggester } from './Suggester/Suggester';
import { Summary } from './Summary/Summary';
import { FillerLoader } from './FillerLoader/FillerLoader';

// List of all the "componentType"
export const library = {
Expand Down Expand Up @@ -51,6 +52,7 @@ export const library = {
Suggester: Suggester,
Summary: Summary,
Accordion: Accordion,
FillerLoader: FillerLoader,
} satisfies {
[Property in LunaticComponentType]: ComponentType<
LunaticComponentProps<Property>
Expand Down
2 changes: 2 additions & 0 deletions src/components/shared/HOC/slottableComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type { SummaryResponses, SummaryTitle } from '../../Summary/Summary';
import type { LunaticComponentProps } from '../../type';
import type { MarkdownLink } from '../MDLabel/MarkdownLink';
import type { Accordion } from '../../Accordion/Accordion';
import type { FillerLoader } from '../../FillerLoader/FillerLoader';

/**
* Contains the type of every customizable component
Expand Down Expand Up @@ -109,6 +110,7 @@ export type LunaticSlotComponents = {
>;
MarkdownLink: typeof MarkdownLink;
Accordion: typeof Accordion;
FillerLoader: typeof FillerLoader;
};

const empty = {} as Partial<LunaticSlotComponents> | undefined;
Expand Down
4 changes: 4 additions & 0 deletions src/components/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,10 @@ export type ComponentPropsByType = {
iterations?: VtlExpression;
}>;
};
FillerLoader: {
componentType?: 'FillerLoader';
id: string;
};
};

export type LunaticComponentType = keyof ComponentPropsByType;
Expand Down
32 changes: 32 additions & 0 deletions src/stories/behaviour/fillers/fillers.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import source from './source.json';
import Orchestrator from '../../utils/orchestrator.jsx';

export default {
title: 'Behaviour/Fillers',
component: Orchestrator,
};

export const Default = {
args: {
source: source,
data: {},
mockFiller: (url) => {
return new Promise((resolve) => {
setTimeout(
() => {
const code = new URL(url).searchParams.get('code');
if (code === '34000') {
return resolve({ CITY: 'Montpellier' });
} else if (code === '31000') {
return resolve({ CITY: 'Toulouse' });
}
return resolve({});
},
window.location.hash.startsWith('#t')
? parseInt(window.location.hash.replace('#t', ''), 10)
: 1500
);
});
},
},
};
78 changes: 78 additions & 0 deletions src/stories/behaviour/fillers/source.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"$schema": "../../../../lunatic-schema.json",
"maxPage": "2",
"fillers": [
{
"endpoint": {
"url": "\"https://inseefr.github.io/Lunatic/?code=\" || CODE",
"type": "VTL"
},
"responses": [
{
"name": "CODE"
}
]
}
],
"components": [
{
"id": "a",
"page": "1",
"componentType": "Input",
"label": {
"type": "TXT",
"value": "Code postal"
},
"response": {
"name": "CODE"
},
"declarations": [
{
"declarationType": "COMMENT",
"position": "DETACHABLE",
"id": "d_a",
"label": {
"value": "Ce code permettra de remplir la ville (34000 ou 31000)",
"type": "TXT"
}
}
]
},
{
"id": "a",
"page": "2",
"componentType": "Input",
"label": {
"type": "TXT",
"value": "Ville"
},
"response": {
"name": "CITY"
}
}
],
"variables": [
{
"variableType": "COLLECTED",
"values": {
"COLLECTED": null,
"EDITED": null,
"INPUTTED": null,
"FORCED": null,
"PREVIOUS": null
},
"name": "CODE"
},
{
"variableType": "COLLECTED",
"values": {
"COLLECTED": null,
"EDITED": null,
"INPUTTED": null,
"FORCED": null,
"PREVIOUS": null
},
"name": "CITY"
}
]
}
4 changes: 4 additions & 0 deletions src/stories/utils/orchestrator.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ function OrchestratorForStories({
refusedButton,
readOnly = false,
disabled = false,
mockFiller = null,
...rest
}) {
const { maxPage } = source;
Expand Down Expand Up @@ -154,6 +155,9 @@ function OrchestratorForStories({
withOverview: showOverview,
dontKnowButton,
refusedButton,
mocks: {
filler: mockFiller,
},
});

const components = getComponents();
Expand Down
10 changes: 10 additions & 0 deletions src/type.source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ export type LunaticSource = {
};
};
maxPage?: string;
fillers?: FillerDefinition[];
};
export type VTLExpression = {
/**
Expand Down Expand Up @@ -467,3 +468,12 @@ export type SuggesterDefinition = {
type: 'soft';
};
};
export type FillerDefinition = {
endpoint: {
url: string;
type?: 'VTL' | 'TXT';
};
responses: {
name: string;
}[];
};
Loading
Loading