github.com/hernad/nomad@v1.6.112/ui/app/machines/evaluations.js (about) 1 /** 2 * Copyright (c) HashiCorp, Inc. 3 * SPDX-License-Identifier: MPL-2.0 4 */ 5 6 import { assign, createMachine, send } from 'xstate'; 7 8 // Docs on using statecharts: https://xstate.js.org/docs/packages/xstate-fsm/#api 9 export default createMachine( 10 { 11 id: 'evaluations_ui', 12 context: { evaluation: null }, 13 type: 'parallel', 14 states: { 15 table: { 16 initial: 'unknown', 17 on: { 18 NEXT: { 19 actions: ['requestNextPage', send('MODAL_CLOSE')], 20 }, 21 PREV: { 22 actions: ['requestPrevPage', send('MODAL_CLOSE')], 23 }, 24 CHANGE_PAGES_SIZE: { 25 actions: ['changePageSize', send('MODAL_CLOSE')], 26 }, 27 MODEL_UPDATED: '#unknown', 28 }, 29 states: { 30 unknown: { 31 id: 'unknown', 32 always: [{ target: 'data', cond: 'hasData' }, { target: 'empty' }], 33 }, 34 data: {}, 35 empty: {}, 36 }, 37 }, 38 sidebar: { 39 initial: 'unknown', 40 states: { 41 unknown: { 42 always: [ 43 { target: 'open', cond: 'sidebarIsOpen' }, 44 { target: 'close' }, 45 ], 46 }, 47 open: { 48 initial: 'busy', 49 exit: ['removeCurrentEvaluationQueryParameter'], 50 states: { 51 busy: { 52 invoke: { 53 src: 'loadEvaluation', 54 onDone: 'success', 55 onError: 'error', 56 }, 57 }, 58 success: { 59 entry: assign({ 60 evaluation: (context, event) => { 61 return event.data; 62 }, 63 }), 64 on: { 65 LOAD_EVALUATION: { 66 target: 'busy', 67 actions: ['updateEvaluationQueryParameter'], 68 }, 69 }, 70 }, 71 error: { 72 entry: assign({ error: (_ctx, event) => event.data }), 73 on: { 74 RETRY: 'busy', 75 }, 76 }, 77 }, 78 on: { 79 MODAL_CLOSE: 'close', 80 CHANGE_EVAL: [{ target: 'close', cond: 'hasNoCurrentEval' }], 81 }, 82 }, 83 close: { 84 on: { 85 LOAD_EVALUATION: { 86 target: 'open', 87 actions: ['updateEvaluationQueryParameter'], 88 }, 89 CHANGE_EVAL: [ 90 { 91 target: 'open', 92 cond: 'hasCurrentEval', 93 }, 94 ], 95 }, 96 }, 97 }, 98 }, 99 }, 100 }, 101 { 102 services: { 103 // Overridden in the controller 104 async loadEvaluations() {}, 105 async loadEvaluation() {}, 106 }, 107 guards: { 108 sidebarIsOpen() { 109 return false; 110 }, 111 hasData() { 112 return true; 113 }, 114 hasNoCurrentEval(_ctx, { evaluation }) { 115 return !evaluation; 116 }, 117 hasCurrentEval(_ctx, { evaluation }) { 118 return evaluation; 119 }, 120 notBusy(_ctx, _event, meta) { 121 return !meta.state.matches({ sidebar: { open: 'busy' } }); 122 }, 123 }, 124 actions: { 125 updateEvaluationQueryParameter() {}, 126 removeCurrentEvaluationQueryParameter() {}, 127 }, 128 } 129 );