import React, { FunctionComponent, useState, useEffect, ChangeEvent, MouseEvent } from 'react'; import { DecisionSupportFileUpdaterProps } from './DecisionSupportFileUpdaterProps'; import { base58UUID } from "../../util/uuid"; export interface OptionsSectionProps extends DecisionSupportFileUpdaterProps {}; const OptionsSectionName = 'options'; export const OptionsSection: FunctionComponent = ({ dsf, updateDSF, readOnly }) => { interface OptionsSectionState { changed: boolean section: OptionsSection } interface OptionsSection { options: Option[] } interface Option { id: string label: string pros: string cons: string } const [ state, setState ] = useState({ changed: false, section: { options: [], } }); useEffect(() => { if (!state.changed) return; updateDSF({ ...dsf, sections: { ...dsf.sections, [OptionsSectionName]: { ...state.section }} }) setState(state => ({ ...state, changed: false })); }, [state.changed]); useEffect(() => { if (!dsf.sections[OptionsSectionName]) return; setState(state => ({ ...state, changed: false, section: {...state.section, ...dsf.sections[OptionsSectionName] }})); }, [dsf.sections[OptionsSectionName]]); function newOption(label: string, pros: string, cons: string): Option { return { id: base58UUID(), label, pros, cons }; } const onAddOptionClick = (evt: MouseEvent) => { const options = JSON.parse(JSON.stringify(state.section.options)) const option = newOption("Décision", "", ""); options.push(option); setState(state => ({ ...state, changed: true, section: { ...state.section, options }})); }; const onOptionChange = (id: number, attrName: string, evt: ChangeEvent) => { const target = evt.currentTarget; const value = target.hasOwnProperty('checked') ? target.checked : target.value; const options = JSON.parse(JSON.stringify(state.section.options)) options[id][attrName] = value; setState(state => ({ ...state, changed: true, section: { ...state.section, options }})); }; const onRemoveOptionClick = (id: number, evt: MouseEvent) => { if(confirm('Voulez-vous supprimer cette option ?')){ const options = JSON.parse(JSON.stringify(state.section.options)) options.splice(id, 1); setState(state => ({ ...state, changed: true, section: { ...state.section, options }})); } }; return (

Explorer les options

{ state.section.options.map((o, index) => { return ( ) }) } { state.section.options.length === 0 ? : null }
Décision Pours Contres
Aucune option pour l'instant.
); };