Skip to content

Commit

Permalink
refactor: put the stateModel reducer in a slice to make room for othe…
Browse files Browse the repository at this point in the history
…r slices

(cherry picked from commit c1676c7)
  • Loading branch information
cramakri authored and ciyer committed Aug 25, 2022
1 parent 0b7f6fc commit 0c602be
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 47 deletions.
2 changes: 1 addition & 1 deletion client/src/dataset/addtoproject/DatasetAdd.present.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function HeaderAddDataset({ dataset }) {

function DatasetAdd({ dataset, model, handlers, isDatasetValid, currentStatus, importingDataset, insideProject }) {
const [isNewProject, setIsNewProject] = useState(false);
const logged = useSelector((state) => state.user.logged);
const logged = useSelector((state) => state.stateModel.user.logged);

// Return early if there is no dataset
if (!dataset) return <Loader />;
Expand Down
2 changes: 1 addition & 1 deletion client/src/dataset/addtoproject/addDatasetNewProject.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const AddDatasetNewProject = (
const [ newProject, setNewProject ] = useState(null);
const setCurrentStatus = handlers.setCurrentStatus;
const { client } = useContext(AppContext);
const user = useSelector( (state) => state.user);
const user = useSelector( (state) => state.stateModel.user);

useEffect(() => setCurrentStatus(null), [setCurrentStatus]);

Expand Down
2 changes: 1 addition & 1 deletion client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Promise.all([configFetch, privacyFetch]).then(valuesRead => {

// Map redux user data to the initial react application
function mapStateToProps(state, ownProps) {
return { user: state.user, ...ownProps };
return { user: state.stateModel.user, ...ownProps };
}

// Render UI application
Expand Down
2 changes: 1 addition & 1 deletion client/src/landing/Landing.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Home extends Component {

mapStateToProps(state, ownProps) {
// map projects to props
return { projects: state.projects };
return { projects: state.stateModel.projects };
}

render() {
Expand Down
2 changes: 1 addition & 1 deletion client/src/landing/NabBarWarnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function pollComponentsVersion(model, client) {
*/
function VersionsBanner(props) {
function mapStateToProps(state, ownProps) {
return { environment: state.environment };
return { environment: state.stateModel.environment };
}

const VisibleBanner = connect(mapStateToProps)(VersionsBannerPresent);
Expand Down
21 changes: 16 additions & 5 deletions client/src/model/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,24 @@ class Schema {
}
}

function getReduxState(reduxStore, slice) {
const state = reduxStore.getState();
if (slice) return state[slice];
return state;
}

class ReduxStateModel {

constructor(owner, stateBinding, stateHolder, initialState) {
constructor(owner, stateBinding, stateHolder, initialState, slice = null) {
this.stateBinding = stateBinding; // We know stateBinding === StateKind.REDUX
this.reduxStore = stateHolder;
this.slice = slice;
// Initialize state
const updateObj = updateObjectFromObject(initialState, this.reduxStore.getState());
const updateObj = updateObjectFromObject(initialState, getReduxState(stateHolder, slice));
this.immutableUpdate(updateObj, null);
}

getStateObject() { return this.reduxStore.getState(); }
getStateObject() { return getReduxState(this.reduxStore, this.slice); }

immutableUpdate(updateObj, callback) {
this.reduxStore.dispatch({
Expand Down Expand Up @@ -194,8 +201,12 @@ class StateModel {
const initializedState = initialState ? initialState : schema.createInitialized();

if (stateBinding === StateKind.REDUX) {
if (!stateHolder) stateHolder = createStore(schema.reducer(), this.constructor.name);
this._stateModel = new ReduxStateModel(this, stateBinding, stateHolder, initializedState);
let slice = null;
if (!stateHolder) {
slice = "stateModel";
stateHolder = createStore({ [slice]: schema.reducer() }, this.constructor.name);
}
this._stateModel = new ReduxStateModel(this, stateBinding, stateHolder, initializedState, slice);
this.reduxStore = this._stateModel.reduxStore;
}
else if (stateBinding === StateKind.REACT) {
Expand Down
14 changes: 7 additions & 7 deletions client/src/notebooks/Notebooks.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import { sleep } from "../utils/helpers/HelperFunctions";
*/

function mapSessionStateToProps(state, ownProps) {
const notebooks = state.notebooks.notebooks;
const notebooks = state.stateModel.notebooks.notebooks;
const available = notebooks.all[ownProps.target] ?
true :
false;
Expand All @@ -60,12 +60,12 @@ function mapSessionStateToProps(state, ownProps) {
data: available ?
notebooks.all[ownProps.target] :
{},
logs: state.notebooks.logs
logs: state.stateModel.notebooks.logs
};
return {
handlers: ownProps.handlers,
target: ownProps.target,
filters: state.notebooks.filters,
filters: state.stateModel.notebooks.filters,
notebook
};
}
Expand Down Expand Up @@ -144,8 +144,8 @@ class ShowSession extends Component {
function mapSessionListStateToProps(state, ownProps) {
return {
handlers: ownProps.handlers,
...state.notebooks,
logs: { ...state.notebooks.logs, show: ownProps.showingLogs }
...state.stateModel.notebooks,
logs: { ...state.stateModel.notebooks.logs, show: ownProps.showingLogs }
};
}

Expand Down Expand Up @@ -823,7 +823,7 @@ class StartNotebookServer extends Component {
}

function mapNotebookStatusStateToProps(state, ownProps) {
const subState = state.notebooks;
const subState = state.stateModel.notebooks;

const notebookKeys = Object.keys(subState.notebooks.all);
const notebook = notebookKeys.length > 0 ?
Expand Down Expand Up @@ -888,7 +888,7 @@ class CheckNotebookStatus extends Component {
}

mapStateToProps(state, ownProps) {
const subState = state.notebooks;
const subState = state.stateModel.notebooks;

const notebookKeys = Object.keys(subState.notebooks.all);
const notebook = notebookKeys.length > 0 ?
Expand Down
10 changes: 5 additions & 5 deletions client/src/notifications/Notifications.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ class NotificationsMenu extends Component {
mapStateToProps(state, ownProps) {
return {
handlers: this.handlers,
notifications: state.notifications.all,
unread: state.notifications.unread,
enabled: state.notifications.dropdown.enabled
notifications: state.stateModel.notifications.all,
unread: state.stateModel.notifications.unread,
enabled: state.stateModel.notifications.dropdown.enabled
};
}

Expand Down Expand Up @@ -213,8 +213,8 @@ class NotificationsPage extends Component {
mapStateToProps(state, ownProps) {
return {
handlers: this.handlers,
notifications: state.notifications.all,
unread: state.notifications.unread
notifications: state.stateModel.notifications.all,
unread: state.stateModel.notifications.unread
};
}

Expand Down
2 changes: 1 addition & 1 deletion client/src/project/Project.js
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,7 @@ class View extends Component {
*/
function mapProjectFeatures(projectCoordinator, features = [], parentProperty = null) {
let mapStateToProps = function (state) {
const projectState = state.project;
const projectState = projectCoordinator.get();
if (!features || !features.length)
features = Object.keys(projectState);

Expand Down
32 changes: 17 additions & 15 deletions client/src/project/new/ProjectNew.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,17 @@ class ForkProjectMapper extends Component {

mapStateToProps(state, ownProps) {
return {
namespaces: { ...state.projects.namespaces },
namespaces: { ...state.stateModel.projects.namespaces },
// We need only a selection of the featured projects. Replicate the namespaces structure fetched/fetching/list
projects: {
fetched: state.projects.featured.fetched,
fetching: state.projects.featured.fetching,
list: state.projects.featured.member,
fetched: state.stateModel.projects.featured.fetched,
fetching: state.stateModel.projects.featured.fetching,
list: state.stateModel.projects.featured.member,
},
user: {
logged: state.user.logged,
username: state.user.data && state.user.data.username ? state.user.data.username : null
logged: state.stateModel.user.logged,
username: state.stateModel.user.data && state.stateModel.user.data.username ?
state.stateModel.user.data.username : null
}
};
}
Expand Down Expand Up @@ -580,24 +581,25 @@ class NewProject extends Component {
// map minimal projects and user information
const additional = {
projects: {
fetched: state.projects.featured.fetched,
fetching: state.projects.featured.fetching,
list: state.projects.featured.member
fetched: state.stateModel.projects.featured.fetched,
fetching: state.stateModel.projects.featured.fetching,
list: state.stateModel.projects.featured.member
},
namespaces: {
fetched: state.projects.namespaces.fetched,
fetching: state.projects.namespaces.fetching,
list: state.projects.namespaces.list
fetched: state.stateModel.projects.namespaces.fetched,
fetching: state.stateModel.projects.namespaces.fetching,
list: state.stateModel.projects.namespaces.list
},
user: {
logged: state.user.logged,
username: state.user.data && state.user.data.username ? state.user.data.username : null
logged: state.stateModel.user.logged,
username: state.stateModel.user.data && state.stateModel.user.data.username ?
state.stateModel.user.data.username : null
},
};

return {
...additional,
...state.newProject,
...state.stateModel.newProject,
handlers: this.handlers
};
}
Expand Down
10 changes: 5 additions & 5 deletions client/src/project/settings/ProjectSettings.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ class ProjectSettingsSessionsMapper extends Component {

mapStateToProps(state, ownProps) {
return {
backend: state.project.migration.core,
options: state.notebooks.options,
metadata: state.project.metadata,
config: state.project.config,
user: state.user
backend: state.stateModel.project.migration.core,
options: state.stateModel.notebooks.options,
metadata: state.stateModel.project.metadata,
config: state.stateModel.project.config,
user: state.stateModel.user
};
}
render() {
Expand Down
2 changes: 1 addition & 1 deletion client/src/statuspage/Statuspage.container.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { connect } from "react-redux";
import { StatuspageDisplay as DisplayPresent, StatuspageBanner as BannerPresent } from "./Statuspage.present";

function mapStateToProps(state, ownProps) {
return { statusSummary: state.statuspage, ...ownProps };
return { statusSummary: state.stateModel.statuspage, ...ownProps };
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { simpleHash } from "../../helpers/HelperFunctions";
function locationToLocationHash(loc) { return "uid_" + simpleHash(loc); }

function mapStateToProps(state, props) {
const currentDraft = state.formGenerator.formDrafts[props.locationHash];
const currentDraft = state.stateModel.formGenerator.formDrafts[props.locationHash];
return {
draft: currentDraft,
...props
Expand Down
4 changes: 2 additions & 2 deletions client/src/utils/helpers/EnhancedState.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@

import { configureStore } from "@reduxjs/toolkit";

function createStore(reducer, name = "renku") {
function createStore(renkuStateModelReducer, name = "renku") {

// For the moment, disable the custom middleware, since it causes
// problems for our app.
const store = configureStore({
reducer: reducer,
reducer: renkuStateModelReducer,
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
immutableCheck: false,
Expand Down

0 comments on commit 0c602be

Please sign in to comment.