Commit 43ae28e4 authored by tizah's avatar tizah

created actions and reducer to optimise code

parent a1ebf63c
import React, { useState } from 'react' //eslint-disable-line
export const compilation = (analysisModule, state, run) => {
// const setCompilationResult = async (data, source, file) => {
// await setResult({ lastCompilationResult: data, lastCompilationSource: source, currentFile: file })
// }
if (analysisModule) {
analysisModule.on(
'solidity',
'compilationFinished',
(file, source, languageVersion, data) => {
if (languageVersion.indexOf('soljson') !== 0) return
setCompilationResult(data, source, file)
if (state.categoryIndex.length > 0) {
run(data, source, file)
}
}
)
}
}
export const setCompilationResult = async (data, source, file) => {
return await { data, source, file }
}
import remixLib from '@remix-project/remix-lib'
import * as _ from 'lodash'
const StaticAnalysisRunner = require('@remix-project/remix-analyzer').CodeAnalysis
const utils = remixLib.util
const runner = new StaticAnalysisRunner()
const preProcessModules = (arr: any) => {
return arr.map((Item, i) => {
const itemObj = new Item()
itemObj._index = i
itemObj.categoryDisplayName = itemObj.category.displayName
itemObj.categoryId = itemObj.category.id
return itemObj
})
}
const groupedModules = utils.groupBy(
preProcessModules(runner.modules()),
'categoryId'
)
const getIndex = (modules, array) => {
Object.values(modules).map((value: {_index}) => {
if (Array.isArray(value)) {
value.forEach((x) => {
array.push(x._index.toString())
})
} else {
array.push(value._index.toString())
}
})
}
const groupedModuleIndex = (modules) => {
const indexOfCategory = []
if (!_.isEmpty(modules)) {
getIndex(modules, indexOfCategory)
}
return indexOfCategory
}
export const initialState = { categoryIndex: [] }
export const analysisReducer = (state, action) => {
switch (action.type) {
case 'initialize':
return { ...state, categoryIndex: groupedModuleIndex(groupedModules) }
case 'uncheck':
return {
...state,
categoryIndex: state.categoryIndex.filter((el) => {
return !action.payload.includes(el)
})
}
case 'check':
return { ...state, categoryIndex: _.uniq([...state.categoryIndex, ...action.payload]) }
case 'uncheckSingle':
return { ...state, categoryIndex: state.categoryIndex.filter(val => val !== action.payload) }
case 'checkSingle':
return { ...state, categoryIndex: _.uniq([...state.categoryIndex, action.payload]) }
default:
return { ...state, categoryIndex: groupedModuleIndex(groupedModules) }
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment