/
OrcusApp.js
109 lines (98 loc) · 3.07 KB
/
OrcusApp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* OrcusApp.js
* redux-orm model for an App instance
* Dependencies: redux-orm, @reduxjs/toolkit modules, EnhancedModel class
* Author: Joshua Carter
* Created: February 2, 2020
*/
"use strict";
//import dependencies
import { fk, many, attr, createSelector } from 'redux-orm';
import { createSlice } from '@reduxjs/toolkit';
//import classes
import { EnhancedModel } from './EnhancedModel.js';
//define constants
const DEFAULT_ID = "ORCUS_APP_DEFAULT_ID_VALUE_68142";
//create our OrcusApp model
var OrcusApp = class extends EnhancedModel {
static modelName = 'OrcusApp';
static fields = {
// non-relational fields
// component props
slug: attr(), //string, required
id: attr(),
icon: attr(),
name: attr(),
opened: attr(), //bool
};
static options = {
idAttribute: "slug"
};
static defaultProps = {
id: DEFAULT_ID,
icon: "fa:home",
name: "",
opened: false
};
//return an initial state object that is derived from some component props
static getInitialStateFromProps (props) {
return Object.fromEntries(
Object.entries(
Object.assign({}, OrcusApp.defaultProps, props, {
opened: props.initialOpened
})
).filter(
it => [
'slug', 'id', 'icon', 'name', 'opened'
].includes(it[0])
)
);
}
// SELECTORS
static select = {};
static createSelectors (orm) {
OrcusApp.select.app = createSelector(orm.OrcusApp);
OrcusApp.select.appSlug = createSelector(orm.OrcusApp.slug);
}
// REDUCER
static slice = createSlice({
name: 'OrcusAppSlice',
initialState: undefined,
reducers: {
createApp (App, action) {
App.create(
Object.assign({}, OrcusApp.defaultProps, action.payload)
);
},
updateAppProp (App, action) {
App
.requireId(action.payload.slug)
.set(action.payload.prop, action.payload.value);
},
updateApp (App, action) {
App
.requireId(action.payload.slug)
.update(action.payload.props);
},
openApp (App, action) {
App.requireId(action.payload.slug).set("opened", true);
},
closeApp (App, action) {
App.requireId(action.payload.slug).set("opened", false);
},
destroyApp (App, action) {
App.requireId(action.payload.slug).delete();
}
}
});
toString () {
return `OrcusApp: ${this.name}`;
}
};
//export OrcusApp class
export default OrcusApp;
//export actions
export const {
createApp, updateAppProp, updateApp, openApp, closeApp, destroyApp
} = OrcusApp.slice.actions;
//export DEFAULT_ID constant for default id functionality in render layer
export { DEFAULT_ID };