Skip to content

Commit e631be9

Browse files
committed
Add obj types as common constants
1 parent a4c6906 commit e631be9

File tree

6 files changed

+47
-22
lines changed

6 files changed

+47
-22
lines changed

.eslintrc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ module.exports = {
2121
sourceType: 'module',
2222
},
2323
plugins: ['react', 'react-hooks', 'prettier'],
24-
rules: {},
24+
rules: {
25+
'react/prop-types': 'off', // keeping off for first phase
26+
'no-unused-vars': 'off', // Getting a lot of Error for: '_' is assigned a value but never used no-unused-vars. For now disabling this because need to understand more about the use '_'.
27+
},
2528
settings: {
2629
'import/resolver': {
2730
node: {

src/components/molecules/workspaceDirectory/Directories.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import Directory from 'components/molecules/workspaceDirectory/Directory';
44
import { DirectoryOptionsActions } from 'constants/WorkspaceDirectory';
55
import NewLabelModal from '../modals/workspaceDirectory/NewLabelModal';
66
import { deleteCollection, deleteFolder, deleteFlowTest } from 'service/collection';
7+
import { OBJ_TYPES } from 'constants/Common';
78

89
const Directories = ({ collections }) => {
910
const [newLabelModalOpen, setNewLabelModal] = useState(false);
@@ -12,7 +13,7 @@ const Directories = ({ collections }) => {
1213
const [selectedCollectionId, setSelectedCollectionId] = useState('');
1314

1415
const handleDeleteMenuItem = (menuItemType, path, collectionId) => {
15-
if (menuItemType === 'collection') {
16+
if (menuItemType === OBJ_TYPES.collection) {
1617
deleteCollection(collectionId)
1718
.then((result) => {
1819
console.log(`Deleted collection: collectionId = ${collectionId}`);
@@ -23,7 +24,7 @@ const Directories = ({ collections }) => {
2324
});
2425
}
2526

26-
if (menuItemType === 'folder') {
27+
if (menuItemType === OBJ_TYPES.folder) {
2728
deleteFolder(path, collectionId)
2829
.then((result) => {
2930
console.log(`Deleted folder: path = ${path}, collectionId = ${collectionId}`);
@@ -34,7 +35,7 @@ const Directories = ({ collections }) => {
3435
});
3536
}
3637

37-
if (menuItemType === 'file') {
38+
if (menuItemType === OBJ_TYPES.flowtest) {
3839
deleteFlowTest(path, collectionId)
3940
.then((result) => {
4041
console.log(`Deleted flowtest: path = ${path}, collectionId = ${collectionId}`);

src/components/molecules/workspaceDirectory/Directory.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState } from 'react';
22
import { PropTypes } from 'prop-types';
3-
import { FLOW_FILE_SUFFIX_REGEX } from 'constants/Common';
3+
import { FLOW_FILE_SUFFIX_REGEX, OBJ_TYPES } from 'constants/Common';
44
import { ArchiveBoxIcon, FolderIcon, DocumentIcon } from '@heroicons/react/24/outline';
55
import OptionsMenu from './OptionsMenu';
66
import { readFlowTest } from 'service/collection';
@@ -9,7 +9,7 @@ const Directory = ({ collectionId, item, depth }) => {
99
const [isExpanded, setIsExpanded] = useState(false);
1010

1111
const getListDisplayTitle = () => {
12-
if (item.type === 'collection') {
12+
if (item.type === OBJ_TYPES.collection) {
1313
// this is for collections tab thus we have archive box icon
1414
return (
1515
<div
@@ -29,14 +29,14 @@ const Directory = ({ collectionId, item, depth }) => {
2929
<OptionsMenu
3030
data-click-from='options-menu'
3131
directory={item}
32-
data-item-type='collections'
33-
itemType={'collection'}
32+
data-item-type={OBJ_TYPES.collection}
33+
itemType={OBJ_TYPES.collection}
3434
/>
3535
</div>
3636
);
3737
}
3838

39-
if (item.type === 'flowtest' && item.name.match(FLOW_FILE_SUFFIX_REGEX)) {
39+
if (item.type === OBJ_TYPES.flowtest && item.name.match(FLOW_FILE_SUFFIX_REGEX)) {
4040
return (
4141
<div
4242
className='flex items-center justify-between gap-2 text-balance rounded p-0 text-start transition duration-200 ease-out hover:bg-slate-100'
@@ -57,12 +57,17 @@ const Directory = ({ collectionId, item, depth }) => {
5757
<DocumentIcon className='h-4 w-4' />
5858
<span>{item.name}</span>
5959
</div>
60-
<OptionsMenu data-click-from='options-menu' directory={item} data-item-type='file' itemType={'file'} />
60+
<OptionsMenu
61+
data-click-from='options-menu'
62+
directory={item}
63+
data-item-type={OBJ_TYPES.flowtest}
64+
itemType={OBJ_TYPES.flowtest}
65+
/>
6166
</div>
6267
);
6368
}
6469

65-
if (item.type === 'folder') {
70+
if (item.type === OBJ_TYPES.folder) {
6671
return (
6772
<div
6873
className='flex items-center justify-between gap-2 text-balance rounded p-0 text-start transition duration-200 ease-out hover:bg-slate-100'
@@ -77,7 +82,12 @@ const Directory = ({ collectionId, item, depth }) => {
7782
<FolderIcon className='h-4 w-4' />
7883
<span data-type-name={item.type}>{item.name}</span>
7984
</div>
80-
<OptionsMenu data-click-from='options-menu' directory={item} data-item-type='folder' itemType={'folder'} />
85+
<OptionsMenu
86+
data-click-from='options-menu'
87+
directory={item}
88+
data-item-type={OBJ_TYPES.folder}
89+
itemType={OBJ_TYPES.folder}
90+
/>
8191
</div>
8292
);
8393
}

src/constants/Common.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,10 @@ export const BUTTON_TYPES = {
1616
warning: 'warning',
1717
error: 'error',
1818
};
19+
20+
export const OBJ_TYPES = {
21+
collection: 'collection',
22+
flowtest: 'flowtest',
23+
folder: 'folder',
24+
environment: 'environment',
25+
};

src/service/collection.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import useCollectionStore from '../stores/CollectionStore';
22
import { v4 as uuidv4 } from 'uuid';
33
import { findItemInCollectionByPathname } from 'stores/utils';
44
import { useEventStore } from 'stores/EventListenerStore';
5+
import { OBJ_TYPES } from 'constants/Common';
56

67
export const createCollection = (openAPISpecFilePath, collectionFolderPath) => {
78
const { ipcRenderer } = window;
@@ -40,7 +41,7 @@ export const createFolder = (folderName, folderPath, collectionId) => {
4041
const collection = useCollectionStore.getState().collections.find((c) => c.id === collectionId);
4142
if (collection) {
4243
const folderPathItem = findItemInCollectionByPathname(collection, folderPath);
43-
const sameFolderExists = folderPathItem.items.find((i) => i.type === 'folder' && i.name === folderName);
44+
const sameFolderExists = folderPathItem.items.find((i) => i.type === OBJ_TYPES.folder && i.name === folderName);
4445
if (sameFolderExists) {
4546
return Promise.reject(new Error('A folder with the same name already exists'));
4647
} else {
@@ -153,7 +154,9 @@ export const createFlowTest = (name, folderPath, collectionId) => {
153154
const collection = useCollectionStore.getState().collections.find((c) => c.id === collectionId);
154155
if (collection) {
155156
const folderPathItem = findItemInCollectionByPathname(collection, folderPath);
156-
const sameFlowTestExists = folderPathItem.items.find((i) => i.type === 'flowtest' && i.name === `${name}.flow`);
157+
const sameFlowTestExists = folderPathItem.items.find(
158+
(i) => i.type === OBJ_TYPES.flowtest && i.name === `${name}.flow`,
159+
);
157160
if (sameFlowTestExists) {
158161
return Promise.reject(new Error('A flowtest with the same name already exists'));
159162
} else {

src/stores/CollectionStore.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import {
99
} from './utils.js';
1010
import { useEventStore } from './EventListenerStore.js';
1111
import { useTabStore } from './TabStore.js';
12+
import { OBJ_TYPES } from 'constants/Common.js';
1213

1314
const useCollectionStore = create((set, get) => ({
1415
collections: [],
1516
createCollection: (id, name, pathname, nodes) => {
1617
const collectionObj = {
1718
version: '1',
1819
id: id,
19-
type: 'collection',
20+
type: OBJ_TYPES.collection,
2021
name: name,
2122
pathname: pathname,
2223
nodes: nodes,
@@ -44,13 +45,13 @@ const useCollectionStore = create((set, get) => ({
4445
let currentPath = collection.pathname;
4546
let currentSubItems = collection.items;
4647
for (const directoryName of subDirsFromRoot) {
47-
let childItem = currentSubItems.find((f) => f.type === 'folder' && f.name === directoryName);
48+
let childItem = currentSubItems.find((f) => f.type === OBJ_TYPES.folder && f.name === directoryName);
4849
if (!childItem) {
4950
childItem = {
5051
id: uuidv4(),
5152
pathname: `${currentPath}${PATH_SEPARATOR}${directoryName}`,
5253
name: directoryName,
53-
type: 'folder',
54+
type: OBJ_TYPES.folder,
5455
items: [],
5556
};
5657
currentSubItems.push(childItem);
@@ -78,7 +79,7 @@ const useCollectionStore = create((set, get) => ({
7879

7980
if (item) {
8081
const flowTestIds = flattenItems(item.items).map((i) => {
81-
if (i.type !== 'folder') {
82+
if (i.type !== OBJ_TYPES.folder) {
8283
i.id;
8384
}
8485
});
@@ -107,7 +108,7 @@ const useCollectionStore = create((set, get) => ({
107108
const timestamp = Date.now();
108109
const env = {
109110
id: uuidv4(),
110-
type: 'environment',
111+
type: OBJ_TYPES.environment,
111112
createdAt: timestamp,
112113
modifiedAt: timestamp,
113114
...file,
@@ -160,13 +161,13 @@ const useCollectionStore = create((set, get) => ({
160161
let currentPath = collection.pathname;
161162
let currentSubItems = collection.items;
162163
for (const directoryName of file.subDirectories) {
163-
let childItem = currentSubItems.find((f) => f.type === 'folder' && f.name === directoryName);
164+
let childItem = currentSubItems.find((f) => f.type === OBJ_TYPES.folder && f.name === directoryName);
164165
if (!childItem) {
165166
childItem = {
166167
id: uuidv4(),
167168
pathname: `${currentPath}${PATH_SEPARATOR}${directoryName}`,
168169
name: directoryName,
169-
type: 'folder',
170+
type: OBJ_TYPES.folder,
170171
items: [],
171172
};
172173
currentSubItems.push(childItem);
@@ -180,7 +181,7 @@ const useCollectionStore = create((set, get) => ({
180181
const timestamp = Date.now();
181182
const flowtest = {
182183
id: uuidv4(),
183-
type: 'flowtest',
184+
type: OBJ_TYPES.flowtest,
184185
createdAt: timestamp,
185186
modifiedAt: timestamp,
186187
name: file.name,

0 commit comments

Comments
 (0)