Skip to content

Commit 22d586e

Browse files
committed
feat(components): wip allow for deletion of components
1 parent 894ebcb commit 22d586e

File tree

7 files changed

+109
-5
lines changed

7 files changed

+109
-5
lines changed

src/actions/component.js

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
* Exports actions related to components.
44
*/
55

6-
import { put, takeLatest } from 'redux-saga/effects';
6+
import { call, put, takeLatest } from 'redux-saga/effects';
77

8-
import { COMPONENT_SELECT } from '../constants';
8+
import { COMPONENT_SELECT, COMPONENT_DELETE } from '../constants';
9+
10+
import {
11+
componentRemove
12+
} from '../lib/api';
13+
14+
import actionGenerator from '../lib/actionGenerator';
915

1016
/**
1117
* Sets the currently selected component.
@@ -22,6 +28,36 @@ export function* componentSelect({ id }) {
2228
});
2329
}
2430

31+
/**
32+
* Delete a component.
33+
*
34+
* @param {object} payload - Payload for this saga action.
35+
* @param {string} payload.id - ID of the component to be deleted.
36+
* @param {object} payload.user - Current user object with authentication.
37+
* @param {function} payload.successHandler - Function to be executed on success.
38+
* @param {function} payload.errorHandler - Function to be executed on error.
39+
*/
40+
export function* componentDelete({
41+
id,
42+
user,
43+
successHandler = () => {},
44+
errorHandler = () => {}
45+
}) {
46+
yield* actionGenerator(
47+
COMPONENT_DELETE,
48+
function* componentDeleteHandler() {
49+
yield call(componentRemove, id, user);
50+
yield put({
51+
type: `${COMPONENT_DELETE}_SUCCESS`,
52+
payload: { id }
53+
});
54+
},
55+
successHandler,
56+
errorHandler
57+
);
58+
}
59+
2560
export function* watchComponentActions() {
2661
yield takeLatest(COMPONENT_SELECT, componentSelect);
62+
yield takeLatest(COMPONENT_DELETE, componentDelete);
2763
}

src/components/ComponentForm/ComponentForm.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ import { withStyles, TextField, Button } from '@material-ui/core';
1111

1212
import {
1313
COMPONENT_SELECT,
14+
COMPONENT_DELETE,
1415
FORM_BUTTON_INSERT_UPDATE,
16+
FORM_BUTTON_DELETE,
17+
FORM_MESSAGE_DELETE_CONFIRM,
1518
OPEN_EXPERIENCE_COMPONENT_FIELD_PRESAVE,
1619
OPEN_EXPERIENCE_COMPONENT_EDIT
1720
} from '../../constants';
@@ -58,7 +61,13 @@ class ComponentForm extends Component {
5861
}).isRequired,
5962
experience: PropTypes.shape({
6063
error: PropTypes.string
61-
})
64+
}),
65+
user: PropTypes.shape({
66+
authentication: PropTypes.shape({
67+
accessToken: PropTypes.string.isRequired,
68+
csrfToken: PropTypes.string.isRequired
69+
}).isRequired
70+
}).isRequired
6271
};
6372

6473
static defaultProps = {
@@ -104,6 +113,19 @@ class ComponentForm extends Component {
104113
}, 200);
105114
};
106115

116+
/**
117+
* Dispatches an action to delete the current component.
118+
*/
119+
removeComponent = () => {
120+
const {
121+
dispatch,
122+
user,
123+
selectedComponent,
124+
} = this.props;
125+
126+
dispatch({ type: COMPONENT_DELETE, id: selectedComponent, user });
127+
};
128+
107129
/**
108130
* Handles field changes.
109131
*
@@ -228,6 +250,20 @@ class ComponentForm extends Component {
228250
>
229251
{FORM_BUTTON_INSERT_UPDATE}
230252
</Button>
253+
<Button
254+
onClick={e => {
255+
e.preventDefault();
256+
if (window.confirm(FORM_MESSAGE_DELETE_CONFIRM)) {
257+
this.removeComponent();
258+
}
259+
}}
260+
variant="raised"
261+
color="secondary"
262+
disabled={isSubmitting}
263+
className={classes.button}
264+
>
265+
{FORM_BUTTON_DELETE}
266+
</Button>
231267
</form>
232268
);
233269
}

src/constants/component.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
*/
55

66
export const COMPONENT_SELECT = 'COMPONENT_SELECT';
7+
export const COMPONENT_DELETE = 'COMPONENT_DELETE';
78
export const COMPONENT_TYPE_DIALOG = 'panelimage';
89
export const COMPONENT_TYPE_LINK = 'link';

src/constants/form.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
*/
55

66
export const FORM_BUTTON_INSERT_UPDATE = 'Save';
7+
export const FORM_BUTTON_DELETE = 'Delete';
8+
export const FORM_MESSAGE_DELETE_CONFIRM = 'Are you sure you want to delete this component?';
79
export const FORM_BUTTON_REGISTER = 'Register';
810
export const FORM_BUTTON_RESET_PASSWORD = 'Reset Password';
911
export const FORM_BUTTON_FORGOT_PASSWORD = 'Forgot Password';

src/lib/api/component.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,3 +142,21 @@ export const componentEdit = async (
142142
}
143143
}
144144
});
145+
146+
147+
/**
148+
* Delete a given component ID via the API.
149+
*
150+
* @param {string} id
151+
* ID of component being updated.
152+
* @param {object} user
153+
* Object containing information about the current user.
154+
* @param {object} user.authentication
155+
* Object containing auth data.
156+
* @param {string} user.authentication.accessToken
157+
* Access token for the current user.
158+
* @param {string} user.authentication.csrfToken
159+
* CSRF token for the current user.
160+
*/
161+
export const componentRemove = async ( id, { authentication } ) =>
162+
axiosInstance(authentication).delete(`${API_ENDPOINT_COMPONENT}/${id}`);

src/lib/api/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
} from './experiences';
1818
import { sceneCreate, sceneEdit, sceneAttachComponent } from './scene';
1919
import { fileImageCreate, fileVideoCreate, fileCreate } from './file';
20-
import { componentEdit, componentCreate } from './component';
20+
import { componentEdit, componentCreate, componentRemove } from './component';
2121

2222
import {
2323
openExperienceFetchForUser,
@@ -37,6 +37,7 @@ export {
3737
sceneAttachComponent,
3838
componentCreate,
3939
componentEdit,
40+
componentRemove,
4041
experiencesEdit,
4142
openExperienceFetchForUser,
4243
openExperienceAttachScene,

src/reducers/component.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Exports reducers pertaining to component state.
44
*/
55

6-
import { COMPONENT_SELECT } from '../constants';
6+
import { COMPONENT_SELECT, COMPONENT_DELETE } from '../constants';
77

88
/**
99
* Default user state.
@@ -30,6 +30,16 @@ export default function component(state = defaultState, action) {
3030
return { id };
3131
}
3232

33+
/**
34+
* Reducer that handles component selection.
35+
*/
36+
case `${COMPONENT_DELETE}_SUCCESS`: {
37+
const { id } = action.payload;
38+
// TODO: Remove component from state
39+
40+
return { id };
41+
}
42+
3343
/**
3444
* Default reducer that returns defaulted state.
3545
*/

0 commit comments

Comments
 (0)