forked from MapComplete/MapComplete
		
	Fix public holiday picker, add some tests, see #902
This commit is contained in:
		
							parent
							
								
									1773f851d1
								
							
						
					
					
						commit
						f99685c47d
					
				
					 2 changed files with 29 additions and 3 deletions
				
			
		| 
						 | 
					@ -316,12 +316,21 @@ export class OH {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     * 
 | 
				
			||||||
 | 
					     * OH.ParsePHRule("PH Off") // => {mode: "off"}
 | 
				
			||||||
 | 
					     * OH.ParsePHRule("PH OPEN") // => {mode: "open"}
 | 
				
			||||||
 | 
					     * OH.ParsePHRule("PH 10:00-12:00") // => {mode: " ", start: "10:00", end: "12:00"}
 | 
				
			||||||
 | 
					     * OH.ParsePHRule(undefined) // => null
 | 
				
			||||||
 | 
					     * OH.ParsePHRule(null) // => null
 | 
				
			||||||
 | 
					     * OH.ParsePHRule("some random string") // => null
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
    public static ParsePHRule(str: string): {
 | 
					    public static ParsePHRule(str: string): {
 | 
				
			||||||
        mode: string,
 | 
					        mode: string,
 | 
				
			||||||
        start?: string,
 | 
					        start?: string,
 | 
				
			||||||
        end?: string
 | 
					        end?: string
 | 
				
			||||||
    } {
 | 
					    } {
 | 
				
			||||||
        if (str === undefined) {
 | 
					        if (str === undefined || str === null) {
 | 
				
			||||||
            return null
 | 
					            return null
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        str = str.trim();
 | 
					        str = str.trim();
 | 
				
			||||||
| 
						 | 
					@ -330,13 +339,13 @@ export class OH {
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        str = str.trim();
 | 
					        str = str.trim();
 | 
				
			||||||
        if (str === "PH off") {
 | 
					        if (str.toLowerCase() === "ph off") {
 | 
				
			||||||
            return {
 | 
					            return {
 | 
				
			||||||
                mode: "off"
 | 
					                mode: "off"
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (str === "PH open") {
 | 
					        if (str.toLowerCase() === "ph open") {
 | 
				
			||||||
            return {
 | 
					            return {
 | 
				
			||||||
                mode: "open"
 | 
					                mode: "open"
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -26,6 +26,20 @@ export default class PublicHolidayInput extends InputElement<string> {
 | 
				
			||||||
        return true;
 | 
					        return true;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /**
 | 
				
			||||||
 | 
					     *
 | 
				
			||||||
 | 
					     * // should construct an element
 | 
				
			||||||
 | 
					     * const html = new PublicHolidayInput().InnerConstructElement()
 | 
				
			||||||
 | 
					     * html !== undefined // => true
 | 
				
			||||||
 | 
					     * 
 | 
				
			||||||
 | 
					     * // should construct an element despite having an invalid input
 | 
				
			||||||
 | 
					     * const html = new PublicHolidayInput(new UIEventSource("invalid")).InnerConstructElement()
 | 
				
			||||||
 | 
					     * html !== undefined // => true
 | 
				
			||||||
 | 
					     * 
 | 
				
			||||||
 | 
					     * // should construct an element despite having null as input
 | 
				
			||||||
 | 
					     * const html = new PublicHolidayInput(new UIEventSource(null)).InnerConstructElement()
 | 
				
			||||||
 | 
					     * html !== undefined // => true
 | 
				
			||||||
 | 
					     */
 | 
				
			||||||
    protected InnerConstructElement(): HTMLElement {
 | 
					    protected InnerConstructElement(): HTMLElement {
 | 
				
			||||||
        const dropdown = new DropDown(
 | 
					        const dropdown = new DropDown(
 | 
				
			||||||
            Translations.t.general.opening_hours.open_during_ph.Clone(),
 | 
					            Translations.t.general.opening_hours.open_during_ph.Clone(),
 | 
				
			||||||
| 
						 | 
					@ -75,6 +89,9 @@ export default class PublicHolidayInput extends InputElement<string> {
 | 
				
			||||||
        const value = this._value;
 | 
					        const value = this._value;
 | 
				
			||||||
        value.map(ph => OH.ParsePHRule(ph))
 | 
					        value.map(ph => OH.ParsePHRule(ph))
 | 
				
			||||||
            .addCallbackAndRunD(parsed => {
 | 
					            .addCallbackAndRunD(parsed => {
 | 
				
			||||||
 | 
					                if(parsed === null){
 | 
				
			||||||
 | 
					                    return
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
                mode.setData(parsed.mode)
 | 
					                mode.setData(parsed.mode)
 | 
				
			||||||
                startTime.setData(parsed.start)
 | 
					                startTime.setData(parsed.start)
 | 
				
			||||||
                endTime.setData(parsed.end)
 | 
					                endTime.setData(parsed.end)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue