Skip to content

Commit

Permalink
Merge pull request #15 from Cognifide/feature/persist-data-state
Browse files Browse the repository at this point in the history
Feature/persist data state
  • Loading branch information
goeson00 authored Aug 27, 2019
2 parents ee61678 + 054e27f commit d8ac478
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 31 deletions.
10 changes: 9 additions & 1 deletion cogboard-webapp/src/actions/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { dataChanged } from './actionCreators';

const checkResponseStatus = response => {
const { status, statusText } = response;

Expand Down Expand Up @@ -83,4 +85,10 @@ export const mapDataToState = (data) => {
generalData,
serverData: { id, type, ...other }
};
};
};

export const withDataChanged = (actionCallback) => (...args) =>
dispatch => {
dispatch(actionCallback.apply(null, args));
dispatch(dataChanged());
};
36 changes: 25 additions & 11 deletions cogboard-webapp/src/actions/thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,32 @@ import {
requestData,
receiveData,
requestUpdate,
addBoard,
editBoard,
deleteBoard,
setCurrentBoard,
updateWidget,
addWidget,
editWidget,
deleteMultipleWidgets,
dataChanged,
saveDataStart
saveDataStart,
deleteWidget
} from './actionCreators';
import {
fetchData,
createNewWidgetData,
createEditWidgetData,
mapDataToState
mapDataToState,
withDataChanged
} from './helpers';
import { URL } from '../constants';

export const fetchInitialData = () =>
(dispatch) => {
dispatch(requestData());

return fetchData('/api/config')
return fetchData(URL.LOAD_DATA)
.then(
data => dispatch(receiveData(data)),
console.error
Expand All @@ -34,14 +39,14 @@ export const saveData = () =>
const { boards, widgets } = getState();
const data = { boards, widgets };

return fetchData('/api/config/save', 'POST', data)
return fetchData(URL.SAVE_DATA, 'POST', data)
.then(
() => dispatch(saveDataStart()),
console.error
);
};

export const deleteBoardWithWidgets = (id) =>
const deleteBoardWithWidgetsThunk = (id) =>
(dispatch, getState) => {
const { ui, boards } = getState();
const { widgets } = boards.boardsById[id];
Expand All @@ -55,8 +60,8 @@ export const deleteBoardWithWidgets = (id) =>
dispatch(deleteMultipleWidgets(widgets));
};

const makeWidgetUpdaterThunk = (beforeUpdateActionCreator, widgetDataCreator) => data => {
return (dispatch, getState) => {
const makeWidgetUpdaterThunk = (beforeUpdateActionCreator, widgetDataCreator) => data =>
(dispatch, getState) => {
const allWidgets = getState().widgets.allWidgets;
const widgetData = widgetDataCreator({...data, allWidgets});
const { id } = widgetData;
Expand All @@ -66,14 +71,23 @@ const makeWidgetUpdaterThunk = (beforeUpdateActionCreator, widgetDataCreator) =>
dispatch(dataChanged());
dispatch(requestUpdate(id));

return fetchData('/api/widget/update', 'POST', serverData)
return fetchData(URL.UPDATE_WIDGET, 'POST', serverData)
.then(
() => dispatch(updateWidget(serverData)),
console.error
);
};
};

export const addNewWidget = makeWidgetUpdaterThunk(addWidget, createNewWidgetData);
const removeWidgetThunk = (id) =>
(dispatch, getState) => {
const { currentBoard } = getState().ui;

export const saveWidget = makeWidgetUpdaterThunk(editWidget, createEditWidgetData);
dispatch(deleteWidget(id, currentBoard));
};

export const addNewWidget = makeWidgetUpdaterThunk(addWidget, createNewWidgetData);
export const saveWidget = makeWidgetUpdaterThunk(editWidget, createEditWidgetData);
export const removeWidget = withDataChanged(removeWidgetThunk);
export const addNewBoard = withDataChanged(addBoard);
export const saveBoard = withDataChanged(editBoard);
export const deleteBoardWithWidgets = withDataChanged(deleteBoardWithWidgetsThunk);
4 changes: 2 additions & 2 deletions cogboard-webapp/src/components/AddBoard.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useDispatch } from 'react-redux';
import styled from '@emotion/styled/macro';

import { useDialogToggle } from '../hooks';
import { addBoard } from '../actions/actionCreators';
import { addNewBoard } from '../actions/thunks';

import { Button, IconButton } from '@material-ui/core';
import { Add } from '@material-ui/icons';
Expand All @@ -25,7 +25,7 @@ const AddBoard = () => {
};

const handleAddActionClick = values => () => {
dispatch(addBoard(values));
dispatch(addNewBoard(values));
handleDialogClose();
};

Expand Down
3 changes: 1 addition & 2 deletions cogboard-webapp/src/components/BoardCard.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { func, object } from 'prop-types';
import { object } from 'prop-types';
import { useDispatch } from 'react-redux';
import styled from '@emotion/styled/macro';

Expand Down Expand Up @@ -92,7 +92,6 @@ const BoardCard = ({ boardData, className }) => {
};

BoardCard.propTypes = {
handleBoardClick: func.isRequired,
boardData: object.isRequired
}

Expand Down
8 changes: 1 addition & 7 deletions cogboard-webapp/src/components/BoardList.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import { func } from 'prop-types';
import styled from '@emotion/styled/macro';
import { useSelector } from 'react-redux';

Expand All @@ -10,7 +9,7 @@ const StyledBoardCard = styled(BoardCard)`
margin-bottom: 32px;
`;

const BoardList = ({ handleBoardClick, className }) => {
const BoardList = ({ className }) => {
const boards = useSelector(
({ boards }) => {
const { boardsById, allBoards } = boards;
Expand All @@ -23,7 +22,6 @@ const BoardList = ({ handleBoardClick, className }) => {
<Box className={className}>
{boards.map(boardData =>
<StyledBoardCard
handleBoardClick={handleBoardClick}
boardData={boardData}
key={boardData.id}
/>
Expand All @@ -32,8 +30,4 @@ const BoardList = ({ handleBoardClick, className }) => {
);
};

BoardList.propTypes = {
handleBoardClick: func.isRequired
};

export default BoardList;
4 changes: 2 additions & 2 deletions cogboard-webapp/src/components/EditBoard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';
import { useDispatch } from 'react-redux';
import styled from '@emotion/styled/macro';

import { editBoard } from '../actions/actionCreators';
import { saveBoard } from '../actions/thunks';

import { Button } from '@material-ui/core'
import BoardForm from './BoardForm';
Expand All @@ -16,7 +16,7 @@ const EditBoard = ({ closeDialog, id, ...initialFormValues }) => {
const dispatch = useDispatch()

const handleSaveClick = (values) => () => {
dispatch(editBoard({ id, ...values }));
dispatch(saveBoard({ id, ...values }));
closeDialog();
};

Expand Down
9 changes: 4 additions & 5 deletions cogboard-webapp/src/components/Widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import styled from '@emotion/styled/macro';
import { useTheme } from '@material-ui/styles';

import { useDialogToggle } from '../hooks';
import { deleteWidget } from '../actions/actionCreators';
import { removeWidget } from '../actions/thunks';

import { Card, CardHeader, CardContent, MenuItem } from '@material-ui/core';
import AppDialog from './AppDialog';
Expand All @@ -29,7 +29,6 @@ const StyledCard = styled(({
`;

const Widget = ({ id }) => {
const currentBoardId = useSelector(({ ui }) => ui.currentBoard);
const widgetData = useSelector(
state => state.widgets.widgetsById[id],
shallowEqual
Expand Down Expand Up @@ -58,9 +57,9 @@ const Widget = ({ id }) => {
};

const handleDeleteClick = (closeMenu) => () => {
dispatch(deleteWidget(id, currentBoardId));
dispatch(removeWidget(id));
closeMenu();
}
};

return (
<>
Expand Down Expand Up @@ -123,6 +122,6 @@ StyledCard.propTypes = {
goNewLine: bool.isRequired,
status: string.isRequired,
theme: object.isRequired
}
};

export default Widget;
7 changes: 6 additions & 1 deletion cogboard-webapp/src/constants/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
export const URL = {
LOAD_DATA: '/api/config',
SAVE_DATA: '/api/config/save',
UPDATE_WIDGET: '/api/widget/update'
};
export const COLUMNS_MIN = 1;
export const COLUMNS_MAX = 8;
export const COLUMNS_MAX = 20;

0 comments on commit d8ac478

Please sign in to comment.