Skip to content

Commit d1c88a1

Browse files
deinakingsSaraVieira
authored andcommitted
🔨 Refactored 🧠 Overmind Hacktober | Refactor app/pages/Sandbox/Editor/Workspace/items/Server/EnvVars/index.js to use Overmind (#2818)
* refactor EnvironmentVariables to use useOvermind * Fix typing of EnvVariables
1 parent 4119445 commit d1c88a1

File tree

3 files changed

+22
-21
lines changed
  • packages

3 files changed

+22
-21
lines changed

‎packages/app/src/app/overmind/effects/api/index.ts‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ import { client } from 'app/graphql/client';
2323
import { LIST_TEMPLATES } from 'app/pages/Dashboard/queries';
2424

2525
import {
26-
transformSandbox,
2726
transformDirectory,
2827
transformModule,
28+
transformSandbox,
2929
} from '../utils/sandbox';
3030
import apiFactory, { Api, ApiConfig } from './apiFactory';
3131
import {
32-
SandboxAPIResponse,
33-
IModuleAPIResponse,
3432
IDirectoryAPIResponse,
33+
IModuleAPIResponse,
34+
SandboxAPIResponse,
3535
} from './types';
3636

3737
let api: Api;
@@ -154,7 +154,9 @@ export default {
154154
},
155155
});
156156
},
157-
getEnvironmentVariables(sandboxId: string): Promise<EnvironmentVariable[]> {
157+
getEnvironmentVariables(
158+
sandboxId: string
159+
): Promise<{ [key: string]: string }> {
158160
return api.get(
159161
`/sandboxes/${sandboxId}/env`,
160162
{},
@@ -164,7 +166,7 @@ export default {
164166
saveEnvironmentVariable(
165167
sandboxId: string,
166168
environmentVariable: EnvironmentVariable
167-
): Promise<EnvironmentVariable[]> {
169+
): Promise<{ [key: string]: string }> {
168170
return api.post(
169171
`/sandboxes/${sandboxId}/env`,
170172
{
@@ -178,7 +180,7 @@ export default {
178180
deleteEnvironmentVariable(
179181
sandboxId: string,
180182
name: string
181-
): Promise<EnvironmentVariable[]> {
183+
): Promise<{ [key: string]: string }> {
182184
return api.delete(
183185
`/sandboxes/${sandboxId}/env/${name}`,
184186
{},
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
import { useOvermind } from 'app/overmind';
12
import React, { useEffect } from 'react';
2-
import { inject, observer } from 'app/componentConnectors';
33

44
import { WorkspaceInputContainer } from '../../../elements';
5-
65
import { EnvEntry } from './EnvEntry';
76
import { EnvModal } from './EnvModal';
87

9-
const EnvironmentVariablesComponent = ({ signals: { editor }, store }) => {
8+
export const EnvironmentVariables: React.FC<{}> = props => {
9+
const {
10+
actions: { editor },
11+
state: {
12+
editor: { currentSandbox },
13+
},
14+
} = useOvermind();
1015
useEffect(() => {
1116
editor.fetchEnvironmentVariables();
1217
}, [editor]);
@@ -19,7 +24,7 @@ const EnvironmentVariablesComponent = ({ signals: { editor }, store }) => {
1924
editor.deleteEnvironmentVariable({ name });
2025
};
2126

22-
const envVars = store.editor.currentSandbox.environmentVariables;
27+
const envVars = currentSandbox.environmentVariables;
2328

2429
if (!envVars) {
2530
return (
@@ -31,17 +36,13 @@ const EnvironmentVariablesComponent = ({ signals: { editor }, store }) => {
3136

3237
return (
3338
<div>
34-
{Object.keys(envVars.toJSON ? envVars.toJSON() : envVars).map(keyName => (
39+
{Object.keys(envVars).map(keyName => (
3540
<EnvEntry
3641
onSubmit={createEnv}
3742
onDelete={deleteEnv}
3843
key={keyName}
3944
name={keyName}
40-
value={
41-
typeof envVars.get === 'function'
42-
? envVars.get(keyName)
43-
: envVars[keyName]
44-
}
45+
value={envVars[keyName]}
4546
/>
4647
))}
4748

@@ -51,7 +52,3 @@ const EnvironmentVariablesComponent = ({ signals: { editor }, store }) => {
5152
</div>
5253
);
5354
};
54-
55-
export const EnvironmentVariables = inject('store', 'signals')(
56-
observer(EnvironmentVariablesComponent)
57-
);

‎packages/common/src/types/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,9 @@ export type Sandbox = {
307307
git: GitInfo | null;
308308
tags: string[];
309309
isFrozen: boolean;
310-
environmentVariables: EnvironmentVariable[] | null;
310+
environmentVariables: {
311+
[key: string]: string;
312+
} | null;
311313
/**
312314
* This is the source it's assigned to, a source contains all dependencies, modules and directories
313315
*

0 commit comments

Comments
 (0)