Skip to content

Commit 1de5a27

Browse files
author
Maxime Quandalle
committed
Active le rendu côté serveur
Non activé car cela pose un problème bizarre avec la première animation. Sans doute lié à sveltejs/kit#2301
1 parent 1508510 commit 1de5a27

File tree

6 files changed

+37
-23
lines changed

6 files changed

+37
-23
lines changed

src/lib/components/Details.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
import { slide } from 'svelte/transition';
55
import DetailsLine from './DetailsLine.svelte';
66
import Emoji from './Emoji.svelte';
7-
import { emoji, engine, title } from './Results.svelte';
7+
import { emoji, title } from './Results.svelte';
88
import { localisationPublicodesSituation } from '$lib/stores/localisation';
99
import RevenuSelector from './RevenuSelector.svelte';
10-
const formatValue = typeof window !== 'undefined' && window.publicodes.formatValue;
10+
import { engine, formatValue } from '$lib/engine';
1111
1212
const veloCat = $page.query.get('velo');
13-
const categoryDescription = engine?.getRule(`vélo . ${veloCat}`).rawNode?.description ?? '';
13+
const categoryDescription = engine.getRule(`vélo . ${veloCat}`).rawNode?.description ?? '';
1414
1515
let bikePrice;
1616
let revenu = 0;

src/lib/components/Results.svelte

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
11
<script context="module">
2-
import aides from '$lib/../aides.yaml';
3-
4-
// HACK : Le paquet publicodes ne peut pas être importé normallement avec un
5-
// `import Engine from "publicodes"`, vraisembablement car le paquet n'est pas sous
6-
// le bon format ES Module
7-
export const engine = typeof window !== 'undefined' ? new window.publicodes.default(aides) : null;
8-
92
export function title(category) {
103
if (category === 'motorisation') {
114
return 'Motorisation d’un vélo classique';
@@ -25,8 +18,9 @@
2518
import Emoji from '$lib/components/Emoji.svelte';
2619
import { localisation, localisationPublicodesSituation } from '$lib/stores/localisation';
2720
import { goto } from '$app/navigation';
21+
import { engine, formatValue } from '$lib/engine';
2822
29-
const bikeKinds = engine?.getRule('vélo . type').rawNode['possibilités'];
23+
const bikeKinds = engine.getRule('vélo . type').rawNode['possibilités'];
3024
3125
$: aidesPerBikeKind = bikeKinds
3226
.map((type) => {
@@ -43,7 +37,7 @@
4337
{
4438
label: title(cat),
4539
emoji: emoji(cat),
46-
montant: window.publicodes.formatValue(node, { precision: 0 })
40+
montant: formatValue(node, { precision: 0 })
4741
}
4842
]);
4943
</script>

src/lib/components/RevenuSelector.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<script context="module">
2-
import { engine } from '$lib/components/Results.svelte';
2+
import { engine, formatValue, reduceAST } from '$lib/engine';
33
import Emoji from './Emoji.svelte';
4-
const formatValue = typeof window !== 'undefined' && window.publicodes.formatValue;
54
65
// We do a static analysis of the rules AST to search for a particular rule name.
76
// When in find it in a comparaison expression we retreive the value of the other
@@ -43,7 +42,7 @@
4342
}
4443
};
4544
46-
return window.publicodes.reduceAST(accumulateThresholds, [], engine.getRule(dottedName));
45+
return reduceAST(accumulateThresholds, [], engine.getRule(dottedName));
4746
}
4847
</script>
4948

src/lib/engine.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import publicodes, {
2+
formatValue as formatValueImport,
3+
reduceAST as reduceASTImport
4+
} from 'publicodes';
5+
import aides from '$lib/../aides.yaml';
6+
7+
// HACK : Côté client Le paquet publicodes ne peut pas être importé normalement
8+
// avec un `import publicodes from "publicodes"`, vraisemblablement car le
9+
// paquet n'est pas sous le bon format ES Module. Pour contourner le problème
10+
// temporairement on passe par `unpkg.com` qui ajoute une variable globale côté
11+
// client.
12+
const Publicodes = typeof window !== 'undefined' ? window.publicodes.default : publicodes;
13+
14+
export const engine = new Publicodes(aides);
15+
16+
export const formatValue =
17+
typeof window !== 'undefined' ? window.publicodes.formatValue : formatValueImport;
18+
export const reduceAST =
19+
typeof window !== 'undefined' ? window.publicodes.reduceAST : reduceASTImport;

src/routes/__layout.svelte

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
<script>
2+
import { prefetchRoutes } from '$app/navigation';
3+
24
import { page } from '$app/stores';
35
import Emoji from '$lib/components/Emoji.svelte';
46
import Footer from '$lib/components/Footer.svelte';
57
import Search from '$lib/components/Search.svelte';
8+
import { onMount } from 'svelte';
69
import 'virtual:windi.css';
10+
11+
onMount(() => prefetchRoutes(['/', '/ville/*']));
712
</script>
813

914
<div class="px-4 sm:px-8 h-screen flex flex-col">

src/routes/ville/[slug].svelte

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@
22
import { browser } from '$app/env';
33
import { localisation } from '$lib/stores/localisation';
44
5-
export const ssr = false;
6-
75
// This pattern is explained here:
86
// https://github.com/sveltejs/kit/issues/2851
97
export async function load({ page, fetch }) {
10-
if (browser && get(localisation)?.slug === page.params.slug) {
8+
if (browser && get(localisation)?.slug === page.params?.slug) {
119
return { props: { ville: get(localisation) } };
1210
} else {
13-
const res = await fetch(`/api/collectivites?slug=${page.params.slug}`);
11+
const res = await fetch(`/api/collectivites?slug=${page.params?.slug}`);
1412
return { props: { ville: await res.json() } };
1513
}
1614
}
@@ -21,12 +19,11 @@
2119
import Details from '$lib/components/Details.svelte';
2220
import Results from '$lib/components/Results.svelte';
2321
import { fly } from 'svelte/transition';
24-
import { onMount } from 'svelte';
2522
import { get } from 'svelte/store';
2623
2724
export let ville;
2825
29-
onMount(() => localisation.set(ville));
26+
localisation.set(ville);
3027
</script>
3128
3229
<svelte:head>
@@ -42,14 +39,14 @@
4239
{#if $page.query.get('velo')}
4340
<div
4441
class="col-start-1 col-end-1 row-start-1 row-end-1"
45-
transition:fly|local={{ x: 600, duration: 400 }}
42+
transition:fly|local={{ x: 800, duration: 400 }}
4643
>
4744
<Details {ville} />
4845
</div>
4946
{:else}
5047
<div
5148
class="col-start-1 col-end-1 row-start-1 row-end-1"
52-
transition:fly|local={{ x: -600, duration: 400 }}
49+
transition:fly|local={{ x: -800, duration: 400 }}
5350
>
5451
<Results {ville} />
5552
</div>

0 commit comments

Comments
 (0)