|
| 1 | +import React, { useContext } from 'react'; |
| 2 | +import styles from '../ActionCreator/ActionCreator.module.scss'; |
| 3 | +import { GlobalContext } from '../../../context/globalReducer'; |
| 4 | +import { |
| 5 | + deleteActionCreator, |
| 6 | + updateActionCreator, |
| 7 | + updateActionsFilePath, |
| 8 | + updateTypesFilePath, |
| 9 | +} from '../../../context/reduxTestCaseActions'; |
| 10 | +import { Draggable } from 'react-beautiful-dnd'; |
| 11 | +const closeIcon = require('../../../assets/images/close.png'); |
| 12 | +const dragIcon = require('../../../assets/images/drag-vertical.png'); |
| 13 | + |
| 14 | +const ActionCreator = ({ actionCreator, index, dispatchToReduxTestCase }) => { |
| 15 | + const [{ filePathMap }, _] = useContext(GlobalContext); |
| 16 | + const handleChangeActionCreatorFields = (e, field) => { |
| 17 | + let updatedActionCreator = { ...actionCreator }; |
| 18 | + updatedActionCreator[field] = e.target.value; |
| 19 | + dispatchToReduxTestCase(updateActionCreator(updatedActionCreator)); |
| 20 | + }; |
| 21 | + |
| 22 | + const handleClickDeleteActionCreator = e => { |
| 23 | + dispatchToReduxTestCase(deleteActionCreator(actionCreator.id)); |
| 24 | + }; |
| 25 | + |
| 26 | + const handleChangeActionsFileName = e => { |
| 27 | + const actionsFileName = e.target.value; |
| 28 | + const filePath = filePathMap[actionsFileName] || ''; |
| 29 | + dispatchToReduxTestCase(updateActionsFilePath(actionsFileName, filePath)); |
| 30 | + }; |
| 31 | + |
| 32 | + const handleChangeTypesFileName = e => { |
| 33 | + const typesFileName = e.target.value; |
| 34 | + const filePath = filePathMap[typesFileName] || ''; |
| 35 | + dispatchToReduxTestCase(updateTypesFilePath(typesFileName, filePath)); |
| 36 | + }; |
| 37 | + |
| 38 | + return ( |
| 39 | + <Draggable draggableId={actionCreator.id.toString()} index={index}> |
| 40 | + {provided => ( |
| 41 | + <div |
| 42 | + ref={provided.innerRef} |
| 43 | + {...provided.draggableProps} |
| 44 | + {...provided.dragHandleProps} |
| 45 | + id={styles.actionCreator} |
| 46 | + > |
| 47 | + <img |
| 48 | + src={closeIcon} |
| 49 | + id={styles.close} |
| 50 | + alt='close' |
| 51 | + onClick={handleClickDeleteActionCreator} |
| 52 | + /> |
| 53 | + |
| 54 | + <div id={styles.actionCreatorHeader}> |
| 55 | + <img src={dragIcon} alt='drag' /> |
| 56 | + <h3>Action Creator</h3> |
| 57 | + </div> |
| 58 | + |
| 59 | + <div id={styles.filesFlexBox}> |
| 60 | + <div id={styles.files}> |
| 61 | + <label htmlFor='actionsFolder'>Import Actions From</label> |
| 62 | + <input |
| 63 | + type='text' |
| 64 | + id='actionsFolder' |
| 65 | + value={actionCreator.actionsFile} |
| 66 | + onChange={handleChangeActionsFileName} |
| 67 | + placeholder='File Name' |
| 68 | + /> |
| 69 | + </div> |
| 70 | + |
| 71 | + <div id={styles.files}> |
| 72 | + <label htmlFor='typesFolder'>Import Action Types From</label> |
| 73 | + <input |
| 74 | + type='text' |
| 75 | + id='typesFolder' |
| 76 | + value={actionCreator.typesFile} |
| 77 | + onChange={handleChangeTypesFileName} |
| 78 | + placeholder='File Name' |
| 79 | + /> |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + |
| 83 | + <div id={styles.actionFlexBox}> |
| 84 | + <div id={styles.actions}> |
| 85 | + <label htmlFor='actionCreatorFunc'>Action Creator</label> |
| 86 | + <input |
| 87 | + type='text' |
| 88 | + id='actionCreatorFunc' |
| 89 | + onChange={e => handleChangeActionCreatorFields(e, 'actionCreatorFunc')} |
| 90 | + placeholder='e.g. addTodo' |
| 91 | + /> |
| 92 | + </div> |
| 93 | + |
| 94 | + <div id={styles.actions}> |
| 95 | + <label htmlFor='actionType'>Action Type</label> |
| 96 | + <input |
| 97 | + type='text' |
| 98 | + id='actionType' |
| 99 | + onChange={e => handleChangeActionCreatorFields(e, 'actionType')} |
| 100 | + placeholder='e.g. ADD_TODO' |
| 101 | + /> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + |
| 105 | + <div id={styles.payloadFlexBox}> |
| 106 | + <div id={styles.payloadKey}> |
| 107 | + <label htmlFor='payloadKey'>Payload Key</label> |
| 108 | + <input |
| 109 | + type='text' |
| 110 | + id='payloadKey' |
| 111 | + onChange={e => handleChangeActionCreatorFields(e, 'payloadKey')} |
| 112 | + placeholder='e.g. todo' |
| 113 | + /> |
| 114 | + </div> |
| 115 | + |
| 116 | + <div id={styles.payloadType}> |
| 117 | + <label htmlFor='payloadType'>Payload Type</label> |
| 118 | + <select |
| 119 | + id='payloadType' |
| 120 | + onChange={e => handleChangeActionCreatorFields(e, 'payloadType')} |
| 121 | + > |
| 122 | + <option value='' /> |
| 123 | + <option value='word'>word</option> |
| 124 | + <option value='words'>words</option> |
| 125 | + <option value='number'>number</option> |
| 126 | + <option value='arrayElement'>arrayElement</option> |
| 127 | + <option value='objectElement'>objectElement</option> |
| 128 | + </select> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + </div> |
| 132 | + )} |
| 133 | + </Draggable> |
| 134 | + ); |
| 135 | +}; |
| 136 | + |
| 137 | +export default ActionCreator; |
0 commit comments