All files / src/store/features/workflow workflowSlice.ts

34.61% Statements 9/26
0% Branches 0/9
0% Functions 0/10
32% Lines 8/25

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 891x   1x           1x         1x                             1x                                                                                       1x                       1x   1x    
import { createSlice } from "@reduxjs/toolkit";
 
export const defaultCode = `def transform(context):
  out = {}
  for k in context.keys():
    out[k] = str(context[k]) + "-a"
  return out`;
 
export const exampleInput = `{
  "key1": "value1",
  "key2": "value2"
}`;
 
const initialState: any = {
  currentWorkflow: {
    name: "",
    schedule: "one_off",
    workflowType: "slack_channel_alert",
    trigger: {},
    errors: {},
    generateSummary: false,
    globalVariables: [],
    useTransformer: false,
    transformerCode: defaultCode,
    exampleInput,
  },
};
 
const workflowSlice = createSlice({
  name: "workflows",
  initialState,
  reducers: {
    setWorkflowKey(state, { payload }) {
      state[payload.key] = payload.value;
    },
    setCurrentWorkflowKey(state, { payload }) {
      state.currentWorkflow[payload.key] = payload.value;
    },
    setCurrentWorkflowTriggerKey(state, { payload }) {
      state.currentWorkflow.trigger[payload.key] = payload.value;
    },
    setErrorKey(state, { payload }) {
      state.currentWorkflow.errors[payload.key] = payload.value;
    },
    removeErrorKey(state, { payload }) {
      Iif (state.currentWorkflow.errors && state.currentWorkflow.errors[payload])
        delete state.currentWorkflow.errors[payload];
    },
    resetWorkflowState(state) {
      state.currentWorkflow = initialState.currentWorkflow;
    },
    addGlobalVariable(state, { payload }) {
      const list = state.currentWorkflow.globalVariables ?? [];
      list?.push({
        name: payload.name,
        value: payload.value,
      });
      state.currentWorkflow.globalVariables = list;
    },
    updateGlobalVariable(state, { payload }) {
      const list = state.currentWorkflow.globalVariables ?? [];
      list[payload.index].value = payload.value;
      state.currentWorkflow.globalVariables = list;
    },
    deleteGlobalVariable(state, { payload }) {
      const list = state.currentWorkflow.globalVariables ?? [];
      list.splice(payload.index, 1);
      state.currentWorkflow.globalVariables = list;
    },
  },
});
 
export const {
  setWorkflowKey,
  setCurrentWorkflowKey,
  setCurrentWorkflowTriggerKey,
  setErrorKey,
  removeErrorKey,
  resetWorkflowState,
  addGlobalVariable,
  updateGlobalVariable,
  deleteGlobalVariable,
} = workflowSlice.actions;
 
export default workflowSlice.reducer;
 
export const currentWorkflowSelector = (state) =>
  state.workflows.currentWorkflow;