Skip to content

Commit afb7aa8

Browse files
authored
Merge pull request #2895 from asauber/unthunk-action-creators
Remove the need for base Action Creators to be thunks
2 parents 84827a4 + ae2d361 commit afb7aa8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+14946
-44753
lines changed

src/api/generic/linodes.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,6 @@ export const config = addParentRefs({
88
primaryKey: 'id',
99
endpoint: id => `/linode/instances/${id}`,
1010
supports: [ONE, MANY, PUT, DELETE, POST],
11-
properties: {
12-
type: (linode) => function (_, getState) {
13-
const types = getState().api.types.types || {};
14-
15-
return {
16-
label: 'Unknown',
17-
id: linode.type,
18-
...types[linode.type] || {},
19-
...linode.specs,
20-
};
21-
},
22-
image: (linode) => function (_, getState) {
23-
const images = getState().api.images.images || {};
24-
25-
return {
26-
vendor: 'Unknown',
27-
label: 'Unknown',
28-
id: linode.image,
29-
...images[linode.image],
30-
};
31-
},
32-
},
3311
subresources: {
3412
_configs: {
3513
name: 'configs',

src/api/internal.js

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,23 @@ function parseIntIfActualInt(string) {
4747
}
4848

4949
const actionCreatorGenerators = {
50-
// These functions take a config and return Redux Action Creators
51-
[ONE]: config => (resource, ...ids) => (dispatch) =>
52-
dispatch({
50+
[ONE]: c => (resource, ...ids) =>
51+
({
5352
resource,
54-
dispatch,
55-
type: `GEN@${fullyQualified(config)}/ONE`,
53+
type: `GEN@${fullyQualified(c)}/ONE`,
5654
ids: ids.map(parseIntIfActualInt),
5755
}),
58-
[MANY]: config => (page, ...ids) => (dispatch) =>
59-
dispatch({
56+
[MANY]: c => (page, ...ids) =>
57+
({
6058
page,
61-
dispatch,
62-
type: `GEN@${fullyQualified(config)}/MANY`,
59+
type: `GEN@${fullyQualified(c)}/MANY`,
60+
ids: ids.map(parseIntIfActualInt),
61+
}),
62+
[DELETE]: c => (...ids) =>
63+
({
64+
type: `GEN@${fullyQualified(c)}/DELETE`,
6365
ids: ids.map(parseIntIfActualInt),
6466
}),
65-
[DELETE]: config => (...ids) =>
66-
({ type: `GEN@${fullyQualified(config)}/DELETE`, ids: ids.map(parseIntIfActualInt) }),
6767
};
6868

6969
/**
@@ -129,23 +129,6 @@ export class ReducerGenerator {
129129

130130
const combinedStateOne = { ...oldStateOne, ...newStateOne, __updatedAt: new Date() };
131131

132-
if (config.properties && action.dispatch) {
133-
Object.keys(config.properties).forEach(function (property) {
134-
const accessor = config.properties[property];
135-
136-
const stateWithoutPropertyDefined = newStateOne;
137-
if (typeof stateWithoutPropertyDefined[property] !== 'undefined') {
138-
Object.defineProperty(combinedStateOne, property, {
139-
get: () => action.dispatch(accessor(newStateOne)),
140-
});
141-
}
142-
});
143-
}
144-
145-
(config.properiesMasks || []).forEach(function (property) {
146-
delete combinedStateOne[property];
147-
});
148-
149132
const newStateMany = {
150133
...oldStateMany,
151134
[config.name]: {

src/api/internal.spec.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,35 @@ import { resource, page } from '~/data/reduxGen';
55

66
describe('internal', () => {
77
it('generates an action to update one resource', function () {
8-
const dispatch = jest.fn();
9-
108
const config = testConfigOne;
119

1210
const actions = genActions(config);
13-
const oneThunk = actions.one(resource, '23');
14-
oneThunk(dispatch);
11+
const actionOne = actions.one(resource, '23');
1512

16-
expect(dispatch.mock.calls[0][0].resource.label).toBe('nodebalancer-1');
17-
expect(dispatch.mock.calls[0][0].type).toBe('GEN@nodebalancers/ONE');
18-
expect(dispatch.mock.calls[0][0].ids[0]).toBe(23);
13+
expect(actionOne.resource.label).toBe('nodebalancer-1');
14+
expect(actionOne.type).toBe('GEN@nodebalancers/ONE');
15+
expect(actionOne.ids[0]).toBe(23);
1916
});
2017

2118
it('generates an action to update many resources', function () {
22-
const dispatch = jest.fn();
23-
2419
const config = testConfigMany;
2520

2621
const actions = genActions(config);
27-
const manyThunk = actions.many(page);
28-
manyThunk(dispatch);
29-
expect(dispatch.mock.calls[0][0].page.nodebalancers[0].label)
22+
const actionMany = actions.many(page);
23+
24+
expect(actionMany.page.nodebalancers[0].label)
3025
.toBe('nodebalancer-1');
31-
expect(dispatch.mock.calls[0][0].page.nodebalancers[1].label)
26+
expect(actionMany.page.nodebalancers[1].label)
3227
.toBe('nodebalancer-2');
33-
expect(dispatch.mock.calls[0][0].type).toBe('GEN@nodebalancers/MANY');
28+
expect(actionMany.type).toBe('GEN@nodebalancers/MANY');
3429
});
3530

3631
it('generates an action to delete a resource', function () {
3732
const config = testConfigDelete;
3833

3934
const actions = genActions(config);
4035
const deleteAction = actions.delete('23');
36+
4137
expect(deleteAction.ids[0]).toBe(23);
4238
expect(deleteAction.type).toBe('GEN@nodebalancers/DELETE');
4339
});

src/data/linodes.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { testType } from './types';
22
import { apiTestRegion } from './regions';
3+
import { apiTestImage } from './images';
34

45
const stats = {
56
netv6: {
@@ -94,17 +95,13 @@ function createTestLinode(id) {
9495
ipv4: [ipv4.address, secondIPv4.address],
9596
ipv6: ipv6.address,
9697
created: '2016-07-06T16:47:27',
97-
type: testType,
98+
type: testType.id,
9899
status: 'running',
99100
region: apiTestRegion.id,
100101
memory: 2048,
101102
disk: 20480,
102103
vcpus: 2,
103-
image: {
104-
id: 'linode/ubuntu15.10',
105-
vendor: 'Ubuntu',
106-
label: 'Ubuntu 15.10',
107-
},
104+
image: apiTestImage.id,
108105
alerts: {
109106
cpu: 90,
110107
io: 5000,
@@ -479,11 +476,7 @@ export const testLinode1245 = {
479476

480477
export const testLinode1246 = {
481478
...createTestLinode(1246),
482-
image: {
483-
id: null,
484-
label: 'Unknown',
485-
vendor: 'unknown',
486-
},
479+
image: null,
487480
};
488481

489482
const rdnsIP1247 = { ...createTestIPv4(1247), rdns: 'host.rdns.tld' };

src/linodes/components/DistroStyle.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@ import { distros as distroAssets } from '~/assets';
66

77

88
export default function DistroStyle(props) {
9-
const { linode } = props;
9+
const { image } = props;
1010

11-
if (!linode || !linode.image || !linode.image.vendor) {
11+
if (!image.vendor) {
1212
return <span className="distro-style">Unknown</span>;
1313
}
1414

15-
const src = distroAssets[lowerCase(linode.image.vendor)];
15+
const src = distroAssets[lowerCase(image.vendor)];
1616

1717
return (
1818
<span className="distro-style">
1919
{src ? <img
2020
src={src}
21-
alt={linode.image.vendor}
21+
alt={image.vendor}
2222
width="15" height="15"
2323
/> : ''}
24-
<span>{linode.image.label}</span>
24+
<span>{image.label}</span>
2525
</span>
2626
);
2727
}
2828

2929
DistroStyle.propTypes = {
30-
linode: PropTypes.object,
30+
image: PropTypes.object.isRequired,
3131
};

src/linodes/components/__snapshots__/ConfigSelectModalBody.spec.js.snap

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,7 @@ ShallowWrapper {
321321
"group": "Test Group",
322322
"hypervisor": "kvm",
323323
"id": 1238,
324-
"image": Object {
325-
"id": "linode/ubuntu15.10",
326-
"label": "Ubuntu 15.10",
327-
"vendor": "Ubuntu",
328-
},
324+
"image": "linode/arch2016.05",
329325
"ipv4": Array [
330326
"97.107.143.15",
331327
"97.107.143.16",
@@ -335,29 +331,7 @@ ShallowWrapper {
335331
"memory": 2048,
336332
"region": "us-east-1a",
337333
"status": "offline",
338-
"type": Object {
339-
"_polling": false,
340-
"addons": Object {
341-
"backups": Object {
342-
"price": Object {
343-
"monthly": 2.5,
344-
},
345-
},
346-
},
347-
"class": "standard",
348-
"disk": 24576,
349-
"id": "linode2048.5",
350-
"label": "Linode 2048",
351-
"memory": 2048,
352-
"network_out": 125,
353-
"price": Object {
354-
"hourly": 0.015,
355-
"monthly": 10,
356-
},
357-
"service_type": "linode",
358-
"transfer": 2000,
359-
"vcpus": 2,
360-
},
334+
"type": "linode2048.5",
361335
"vcpus": 2,
362336
}
363337
}

src/linodes/components/__snapshots__/StatusDropdown.spec.js.snap

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -237,11 +237,7 @@ ShallowWrapper {
237237
"group": "Test Group",
238238
"hypervisor": "kvm",
239239
"id": 1236,
240-
"image": Object {
241-
"id": "linode/ubuntu15.10",
242-
"label": "Ubuntu 15.10",
243-
"vendor": "Ubuntu",
244-
},
240+
"image": "linode/arch2016.05",
245241
"ipv4": Array [
246242
"97.107.143.9",
247243
"97.107.143.10",
@@ -251,29 +247,7 @@ ShallowWrapper {
251247
"memory": 2048,
252248
"region": "us-east-1a",
253249
"status": "offline",
254-
"type": Object {
255-
"_polling": false,
256-
"addons": Object {
257-
"backups": Object {
258-
"price": Object {
259-
"monthly": 2.5,
260-
},
261-
},
262-
},
263-
"class": "standard",
264-
"disk": 24576,
265-
"id": "linode2048.5",
266-
"label": "Linode 2048",
267-
"memory": 2048,
268-
"network_out": 125,
269-
"price": Object {
270-
"hourly": 0.015,
271-
"monthly": 10,
272-
},
273-
"service_type": "linode",
274-
"transfer": 2000,
275-
"vcpus": 2,
276-
},
250+
"type": "linode2048.5",
277251
"vcpus": 2,
278252
}
279253
}

src/linodes/layouts/ListLinodes.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class ListLinodesPage extends Component {
9797
powerOn = (linodes) => this.genericAction(powerOnLinode, linodes)
9898
reboot = (linodes) => this.genericAction(rebootLinode, linodes, 'Reboot')
9999

100-
renderLinodes(linodes) {
100+
renderLinodes(linodes, types) {
101101
const { dispatch, selectedMap } = this.props;
102102
const { filter } = this.state;
103103

@@ -150,7 +150,7 @@ export class ListLinodesPage extends Component {
150150
headerClassName: 'LabelColumn',
151151
hrefFn: (linode) => `/linodes/${linode.label}`,
152152
tooltipEnabled: true,
153-
subtitleFn: linode => planStyle(linode.type),
153+
subtitleFn: linode => planStyle(types[linode.type]),
154154
},
155155
{ cellComponent: IPAddressCell, headerClassName: 'LinodeIPAddressColumn' },
156156
{
@@ -214,7 +214,7 @@ export class ListLinodesPage extends Component {
214214
</div>
215215
</header>
216216
<div className="PrimaryPage-body">
217-
{Object.keys(linodes).length ? this.renderLinodes(linodes) : (
217+
{Object.keys(linodes).length ? this.renderLinodes(linodes, types) : (
218218
<CreateHelper
219219
label="Linodes"
220220
linkText="Add a Linode"

0 commit comments

Comments
 (0)