Skip to main content
Sign in
Snippets Groups Projects
Commit be4f8afb authored by Emmanuel Raviart's avatar Emmanuel Raviart
Browse files

Variables are editable.

parent 84d17bf3
Branches
Tags
No related merge requests found
......@@ -4,14 +4,14 @@
import type { AnyParameter, ParameterNode } from "$lib/parameters"
import { ParameterClass } from "$lib/parameters"
import type { ReformChange } from "$lib/reforms"
import type { Reform } from "$lib/reforms"
export let ancestors: ParameterNode[]
export let editable: boolean
export let newSelfTargetUrl: (urlPath: string) => string
export let parameter: AnyParameter
const reform = getContext("reform") as Writable<ReformChange>
const reform = getContext("reform") as Writable<Reform>
let validStart = undefined
let validValue = undefined
......@@ -28,7 +28,7 @@
function changeValue(value) {
validValue = parseFloat(value)
if (value == null || Number.isNaN(validValue)) {
if (validValue == null || Number.isNaN(validValue)) {
validValue = undefined
}
updateReform(validStart, validValue)
......
......
......@@ -30,5 +30,5 @@
</script>
{#if variable !== undefined}
<VariableView {newSelfTargetUrl} {variable} />
<VariableView editable={true} {newSelfTargetUrl} {variable} />
{/if}
<script lang="ts">
import { getContext } from "svelte"
import type { Writable } from "svelte/store"
import { pluralByEntityKey } from "$lib/entities"
import type { SituationComplement, Situation } from "$lib/situations"
import type { Variable } from "$lib/variables"
export let editable: boolean
export let newSelfTargetUrl: (urlPath: string) => string
export let variable: Variable
let date = new Date().toISOString().split("T")[0]
const situationComplement = getContext(
"situationComplement",
) as Writable<SituationComplement>
const situationCore = getContext("situationCore") as Writable<Situation>
$: entityPlural = pluralByEntityKey[variable.entity]
$: entitySituationComplement = $situationComplement[entityPlural]
$: entitySituation = $situationCore[entityPlural]
function changeValue(itemName, itemValue) {
switch (variable.value_type) {
case "float":
itemValue = parseFloat(itemValue)
if (itemValue == null || Number.isNaN(itemValue)) {
itemValue = null
}
break
case "int":
itemValue = parseInt(itemValue)
if (itemValue == null || Number.isNaN(itemValue)) {
itemValue = null
}
break
}
$situationComplement[entityPlural] = {
...(entitySituationComplement ?? {}),
[itemName]: {
...(entitySituationComplement?.[itemName] ?? {}),
[variable.name]: itemValue ?? variable.default_value,
},
}
}
</script>
<h1>
......@@ -21,6 +61,18 @@
</div>
{/if}
{#if variable.definition_period !== undefined}
<div>Période de définition : {variable.definition_period}</div>
{/if}
{#if variable.entity !== undefined}
<div>Entité : {variable.entity}</div>
{/if}
{#if variable.value_type !== undefined}
<div>Type de valeur : {variable.value_type}</div>
{/if}
{#if variable.reference !== undefined}
<section>
<h1>Références</h1>
......@@ -102,6 +154,38 @@
</section>
{/if}
{#if editable}
<section>
<h1>Valeur</h1>
<ul>
{#each Object.keys(entitySituation ?? {}) as itemName}
<li>
{itemName} :
{#if variable.possible_values !== undefined}
<select
on:blur={({ target }) => changeValue(itemName, target.value)}
on:change={({ target }) => changeValue(itemName, target.value)}
value={entitySituationComplement?.[itemName]?.[variable.name] ??
variable.default_value}
>
{#each Object.entries(variable.possible_values) as [symbol, label]}
<option value={symbol}>{label}</option>
{/each}
</select>
{:else}
<input
on:change={({ target }) => changeValue(itemName, target.value)}
type="number"
value={entitySituationComplement?.[itemName]?.[variable.name] ??
variable.default_value}
/>
{/if}
</li>
{/each}
</ul>
</section>
{/if}
{#if variable.referring_variables !== undefined}
<section>
<h1>Variables dépendantes</h1>
......@@ -114,11 +198,11 @@
</ul>
</section>
{/if}
<!--
<hr class="my-4" />
<section>
<h1>JSON</h1>
<pre>{JSON.stringify(variable, null, 2)}</pre>
</section>
</section> -->
export const pluralByEntityKey = {
famille: "familles",
foyer_fiscal: "foyers_fiscaux",
individu: "individus",
menage: "menages",
}
......@@ -11,6 +11,10 @@ export interface EntitySituation {
[key: string]: { [date: string]: number | null } | string[]
}
export interface EntitySituationComplement {
[key: string]: boolean | number | string | null
}
export interface FamilleSituation extends EntitySituation {
enfants: string[]
parents: string[]
......@@ -36,3 +40,10 @@ export interface Situation {
individus: { [name: string]: IndividuSituation }
menages: { [name: string]: MenageSituation }
}
export interface SituationComplement {
familles: { [name: string]: EntitySituationComplement }
foyers_fiscaux: { [name: string]: EntitySituationComplement }
individus: { [name: string]: EntitySituationComplement }
menages: { [name: string]: EntitySituationComplement }
}
......@@ -6,6 +6,10 @@ export interface Formula {
variables?: string[]
}
export interface InputByVariableName {
[variableName: string]: Array<boolean | number | string>
}
export interface Variable {
default_value: boolean | number | string
definition_period: "eternity" | "month" | "year"
......@@ -18,8 +22,7 @@ export interface Variable {
reference?: string[]
referring_variables?: string[]
source: string
// TODO
valueType: "Boolean" | "Date" | "Float" | "Int" | "String"
value_type: "bool" | "date" | "float" | "int" | "str"
}
export interface VariableSummary {
......
......
......@@ -8,6 +8,12 @@
const reform = writable({})
setContext("reform", reform)
const situationComplement = writable({})
setContext("situationComplement", situationComplement)
const situationCore = writable({})
setContext("situationCore", situationCore)
</script>
<NavBar />
......
......
......@@ -11,7 +11,7 @@
import type { Decomposition } from "$lib/decompositions"
import { decomposition as decompositionWithoutValue } from "$lib/decompositions"
import type { ReformChange } from "$lib/reforms"
import type { Axis, Situation } from "$lib/situations"
import type { Axis, Situation, SituationComplement } from "$lib/situations"
let axes: Axis[][] = []
let deltaByCode: { [code: string]: number[] } = {}
......@@ -23,6 +23,10 @@
const reform = getContext("reform") as Writable<ReformChange>
let showNulls = false
let situation: Situation | undefined = undefined
let situationComplement = getContext(
"situationComplement",
) as Writable<SituationComplement>
let situationCore = getContext("situationCore") as Writable<Situation>
let vectorIndex = 0
let vectorLength = 1
let webSocket: Sockette | undefined = undefined
......@@ -57,6 +61,7 @@
function changeSituation({ detail }) {
situation = detail
$situationCore = situation
if (webSocketOpen) {
submit()
}
......@@ -122,7 +127,28 @@
if (situation === undefined) {
return
}
let situationWithAxes = situation
let situationWithAxes = { ...situation }
for (const [entityPlural, entitySituationComplement] of Object.entries(
$situationComplement,
)) {
const entitySituation = (situationWithAxes[entityPlural] = {
...situationWithAxes[entityPlural],
})
for (const [itemName, itemSituationComplement] of Object.entries(
entitySituationComplement,
)) {
const itemSituation = (entitySituation[itemName] = {
...entitySituation[itemName],
})
for (const [variableName, variableValue] of Object.entries(
itemSituationComplement,
)) {
itemSituation[variableName] = { [year]: variableValue }
}
}
}
if (axes.length > 0) {
// Remove variables used as axes from situation (otherwise OpenFisca Core fails).
situationWithAxes = {
......
......
......@@ -36,5 +36,5 @@
</svelte:head>
<main>
<VariableView newSelfTargetUrl={(url) => url} {variable} />
<VariableView editable={true} newSelfTargetUrl={(url) => url} {variable} />
</main>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment