expressions.js (3036B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */ 4 5 /** 6 * Expressions reducer 7 * 8 * @module reducers/expressions 9 */ 10 11 import { prefs } from "../utils/prefs"; 12 13 export const initialExpressionState = () => ({ 14 expressions: restoreExpressions(), 15 autocompleteMatches: {}, 16 currentAutocompleteInput: null, 17 }); 18 19 function update(state = initialExpressionState(), action) { 20 switch (action.type) { 21 case "ADD_EXPRESSION": 22 return appendExpressionToList(state, { 23 input: action.input, 24 value: null, 25 updating: true, 26 }); 27 28 case "UPDATE_EXPRESSION": { 29 const key = action.expression.input; 30 return updateExpressionInList(state, key, { 31 input: action.input, 32 value: null, 33 updating: true, 34 }); 35 } 36 37 case "EVALUATE_EXPRESSION": 38 return updateExpressionInList(state, action.input, { 39 input: action.input, 40 value: action.value, 41 updating: false, 42 }); 43 44 case "EVALUATE_EXPRESSIONS": { 45 const { inputs, results } = action; 46 47 return inputs.reduce( 48 (_state, input, index) => 49 updateExpressionInList(_state, input, { 50 input, 51 value: results[index], 52 updating: false, 53 }), 54 state 55 ); 56 } 57 58 case "DELETE_EXPRESSION": 59 return deleteExpression(state, action.input); 60 61 case "AUTOCOMPLETE": { 62 const { matchProp, matches } = action.result; 63 64 return { 65 ...state, 66 currentAutocompleteInput: matchProp, 67 autocompleteMatches: { 68 ...state.autocompleteMatches, 69 [matchProp]: matches, 70 }, 71 }; 72 } 73 74 case "CLEAR_AUTOCOMPLETE": 75 return { 76 ...state, 77 autocompleteMatches: {}, 78 currentAutocompleteInput: "", 79 }; 80 } 81 82 return state; 83 } 84 85 function restoreExpressions() { 86 const exprs = prefs.expressions; 87 if (!exprs.length) { 88 return []; 89 } 90 91 return exprs; 92 } 93 94 function storeExpressions({ expressions }) { 95 // Return the expressions without the `value` property 96 prefs.expressions = expressions.map(({ input, updating }) => ({ 97 input, 98 updating, 99 })); 100 } 101 102 function appendExpressionToList(state, value) { 103 const newState = { ...state, expressions: [...state.expressions, value] }; 104 105 storeExpressions(newState); 106 return newState; 107 } 108 109 function updateExpressionInList(state, key, value) { 110 const list = [...state.expressions]; 111 const index = list.findIndex(e => e.input == key); 112 list[index] = value; 113 114 const newState = { ...state, expressions: list }; 115 storeExpressions(newState); 116 return newState; 117 } 118 119 function deleteExpression(state, input) { 120 const list = [...state.expressions]; 121 const index = list.findIndex(e => e.input == input); 122 list.splice(index, 1); 123 const newState = { ...state, expressions: list }; 124 storeExpressions(newState); 125 return newState; 126 } 127 128 export default update;