A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/markerikson/project-minimek/commit/75cc30ab85b22b3af7f0a70d9a23a2d646833855 below:

Add generic entity creation and deletion handling · markerikson/project-minimek@75cc30a · GitHub

File tree Expand file treeCollapse file tree 3 files changed

+66

-3

lines changed

Filter options

Expand file treeCollapse file tree 3 files changed

+66

-3

lines changed Original file line number Diff line number Diff line change

@@ -1,4 +1,8 @@

1 -

import {ENTITY_UPDATE} from "./entityConstants";

1 +

import {

2 +

ENTITY_UPDATE,

3 +

ENTITY_DELETE,

4 +

ENTITY_CREATE,

5 +

} from "./entityConstants";

2 6 3 7

export function updateEntity(itemType, itemID, newItemAttributes) {

4 8

return {

@@ -9,4 +13,21 @@ export function updateEntity(itemType, itemID, newItemAttributes) {

9 13

newItemAttributes,

10 14

},

11 15

};

16 +

}

17 + 18 +

export function deleteEntity(itemType, itemID) {

19 +

return {

20 +

type : ENTITY_DELETE,

21 +

payload : {itemType, itemID}

22 +

};

23 +

}

24 + 25 +

export function createEntity(itemType, newItemAttributes) {

26 +

return {

27 +

type : ENTITY_CREATE,

28 +

payload : {

29 +

itemType,

30 +

newItemAttributes,

31 +

},

32 +

};

12 33

}

Original file line number Diff line number Diff line change

@@ -1 +1,3 @@

1 -

export const ENTITY_UPDATE = "ENTITY_UPDATE";

1 +

export const ENTITY_UPDATE = "ENTITY_UPDATE";

2 +

export const ENTITY_CREATE = "ENTITY_CREATE";

3 +

export const ENTITY_DELETE = "ENTITY_DELETE";

Original file line number Diff line number Diff line change

@@ -1,4 +1,8 @@

1 -

import {ENTITY_UPDATE} from "./entityConstants";

1 +

import {

2 +

ENTITY_UPDATE,

3 +

ENTITY_DELETE,

4 +

ENTITY_CREATE,

5 +

} from "./entityConstants";

2 6 3 7

import {createConditionalSliceReducer} from "common/utils/reducerUtils";

4 8

@@ -23,8 +27,44 @@ export function updateEntity(state, payload) {

23 27

return newState;

24 28

}

25 29 30 + 31 +

export function deleteEntity(state, payload) {

32 +

const {itemID, itemType} = payload;

33 + 34 +

const session = schema.from(state);

35 +

const ModelClass = session[itemType];

36 + 37 +

let newState = state;

38 + 39 +

if(ModelClass.hasId(itemID)) {

40 +

const modelInstance = ModelClass.withId(itemID);

41 + 42 +

modelInstance.delete();

43 + 44 +

// Immutably apply updates and return the new entities structure

45 +

newState = session.reduce();

46 +

}

47 + 48 +

return newState;

49 +

}

50 + 51 +

export function createEntity(state, payload) {

52 +

const {itemType, newItemAttributes} = payload;

53 + 54 +

const session = schema.from(state);

55 +

const ModelClass = session[itemType];

56 + 57 +

ModelClass.parse(newItemAttributes);

58 + 59 +

const newState = session.reduce();

60 +

return newState;

61 +

}

62 + 63 + 26 64

const entityHandlers = {

27 65

[ENTITY_UPDATE] : updateEntity,

66 +

[ENTITY_CREATE] : createEntity,

67 +

[ENTITY_DELETE] : deleteEntity,

28 68

};

29 69 30 70

const entityCrudFeatureReducer = createConditionalSliceReducer("entities", entityHandlers);

You can’t perform that action at this time.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4