mirror of
https://github.com/strapi/strapi.git
synced 2025-12-27 23:24:03 +00:00
Get role and format data with immutable
This commit is contained in:
parent
3d66f18e1a
commit
3f29551b75
@ -115,4 +115,4 @@
|
||||
"webpack-hot-middleware": "^2.18.2",
|
||||
"whatwg-fetch": "^2.0.3"
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3,9 +3,12 @@
|
||||
* EditPage actions
|
||||
*
|
||||
*/
|
||||
import { List, Map } from 'immutable';
|
||||
import { fromJS, List, Map } from 'immutable';
|
||||
import { get } from 'lodash';
|
||||
import {
|
||||
ADD_USER,
|
||||
GET_PERMISSIONS,
|
||||
GET_PERMISSIONS_SUCCEEDED,
|
||||
GET_ROLE,
|
||||
GET_ROLE_SUCCEEDED,
|
||||
ON_CANCEL,
|
||||
@ -21,16 +24,39 @@ export function addUser(newUser) {
|
||||
};
|
||||
}
|
||||
|
||||
export function getRole() {
|
||||
export function getPermissions() {
|
||||
return {
|
||||
type: GET_PERMISSIONS,
|
||||
};
|
||||
}
|
||||
|
||||
export function getPermissionsSucceeded(data) {
|
||||
const permissions = Map(fromJS(data.permissions));
|
||||
|
||||
return {
|
||||
type: GET_PERMISSIONS_SUCCEEDED,
|
||||
permissions,
|
||||
};
|
||||
}
|
||||
|
||||
export function getRole(id) {
|
||||
return {
|
||||
type: GET_ROLE,
|
||||
id,
|
||||
};
|
||||
}
|
||||
|
||||
export function getRoleSucceeded(data) {
|
||||
const form = Map({
|
||||
name: get(data, ['role', 'name']),
|
||||
description: get(data, ['role', 'description']),
|
||||
users: List(get(data, ['role', 'users'])),
|
||||
permissions: Map(fromJS(get(data, ['role', 'permissions']))),
|
||||
});
|
||||
|
||||
return {
|
||||
type: GET_ROLE_SUCCEEDED,
|
||||
data,
|
||||
form,
|
||||
};
|
||||
}
|
||||
|
||||
@ -65,6 +91,7 @@ export function setForm() {
|
||||
{ name: 'Aurelien Georget' },
|
||||
{ name: 'Cyril Lopez' },
|
||||
]),
|
||||
permissions: Map({}),
|
||||
});
|
||||
|
||||
return {
|
||||
|
||||
@ -5,6 +5,8 @@
|
||||
*/
|
||||
|
||||
export const ADD_USER = 'UsersPermissions/EditPage/ADD_USER';
|
||||
export const GET_PERMISSIONS = 'UsersPermissions/EditPage/GET_PERMISSIONS';
|
||||
export const GET_PERMISSIONS_SUCCEEDED = 'UsersPermissions/EditPage/GET_PERMISSIONS_SUCCEEDED';
|
||||
export const GET_ROLE = 'UsersPermissions/EditPage/GET_ROLE';
|
||||
export const GET_ROLE_SUCCEEDED = 'UsersPermissions/EditPage/GET_ROLE_SUCCEEDED';
|
||||
export const ON_CANCEL = 'UsersPermissions/EditPage/ON_CANCEL';
|
||||
|
||||
@ -25,6 +25,8 @@ import injectReducer from 'utils/injectReducer';
|
||||
// Actions
|
||||
import {
|
||||
addUser,
|
||||
getPermissions,
|
||||
getRole,
|
||||
onCancel,
|
||||
onChangeInput,
|
||||
onClickDelete,
|
||||
@ -43,6 +45,9 @@ export class EditPage extends React.Component { // eslint-disable-line react/pre
|
||||
componentDidMount() {
|
||||
if (this.props.match.params.actionType === 'create') {
|
||||
this.props.setForm();
|
||||
this.props.getPermissions();
|
||||
} else {
|
||||
this.props.getRole(this.props.match.params.id);
|
||||
}
|
||||
}
|
||||
|
||||
@ -69,7 +74,7 @@ export class EditPage extends React.Component { // eslint-disable-line react/pre
|
||||
'users-permissions.EditPage.header.description.create'
|
||||
: 'users-permissions.EditPage.header.description';
|
||||
const pluginHeaderActions = this.props.editPage.showButtons ? this.pluginHeaderActions : [];
|
||||
|
||||
|
||||
return (
|
||||
<div>
|
||||
<BackHeader onClick={() => this.props.history.goBack()} />
|
||||
@ -78,18 +83,18 @@ export class EditPage extends React.Component { // eslint-disable-line react/pre
|
||||
title={{
|
||||
id: pluginHeaderTitle,
|
||||
values: {
|
||||
name: '',
|
||||
name: get(this.props.editPage.initialData, 'name'),
|
||||
},
|
||||
}}
|
||||
description={{
|
||||
id: pluginHeaderDescription,
|
||||
values: {
|
||||
description: '',
|
||||
description: get(this.props.editPage.initialData, 'description') || '',
|
||||
},
|
||||
}}
|
||||
actions={pluginHeaderActions}
|
||||
/>
|
||||
<div className="row">
|
||||
<div className={cn("row", styles.container)}>
|
||||
<div className="col-md-12">
|
||||
<div className={styles.main_wrapper}>
|
||||
<div className={styles.titleContainer}>
|
||||
@ -148,6 +153,8 @@ export class EditPage extends React.Component { // eslint-disable-line react/pre
|
||||
EditPage.propTypes = {
|
||||
addUser: PropTypes.func.isRequired,
|
||||
editPage: PropTypes.object.isRequired,
|
||||
getPermissions: PropTypes.func.isRequired,
|
||||
getRole: PropTypes.func.isRequired,
|
||||
history: PropTypes.object.isRequired,
|
||||
match: PropTypes.object.isRequired,
|
||||
onCancel: PropTypes.func.isRequired,
|
||||
@ -164,6 +171,8 @@ function mapDispatchToProps(dispatch) {
|
||||
return bindActionCreators(
|
||||
{
|
||||
addUser,
|
||||
getPermissions,
|
||||
getRole,
|
||||
onCancel,
|
||||
onChangeInput,
|
||||
onClickDelete,
|
||||
|
||||
@ -7,6 +7,8 @@
|
||||
import { fromJS, Map } from 'immutable';
|
||||
import {
|
||||
ADD_USER,
|
||||
GET_PERMISSIONS_SUCCEEDED,
|
||||
GET_ROLE_SUCCEEDED,
|
||||
ON_CANCEL,
|
||||
ON_CHANGE_INPUT,
|
||||
ON_CLICK_DELETE,
|
||||
@ -26,6 +28,15 @@ function editPageReducer(state = initialState, action) {
|
||||
case ADD_USER:
|
||||
return state
|
||||
.updateIn(['modifiedData', 'users'], list => list.push(action.newUser));
|
||||
case GET_PERMISSIONS_SUCCEEDED:
|
||||
return state
|
||||
.updateIn(['initialData', 'permissions'], () => action.permissions)
|
||||
.updateIn(['modifiedData', 'permissions'], () => action.permissions);
|
||||
case GET_ROLE_SUCCEEDED:
|
||||
return state
|
||||
.set('didGetUsers', !state.get('didGetUsers'))
|
||||
.set('initialData', action.form)
|
||||
.set('modifiedData', action.form);
|
||||
case ON_CANCEL:
|
||||
return state
|
||||
.set('showButtons', false)
|
||||
|
||||
@ -1,6 +1,50 @@
|
||||
// import { take, call, put, select } from 'redux-saga/effects';
|
||||
import { LOCATION_CHANGE } from 'react-router-redux';
|
||||
import {
|
||||
call,
|
||||
cancel,
|
||||
fork,
|
||||
put,
|
||||
// select,
|
||||
take,
|
||||
takeLatest,
|
||||
} from 'redux-saga/effects';
|
||||
import request from 'utils/request';
|
||||
|
||||
// Individual exports for testing
|
||||
export default function* defaultSaga() {
|
||||
// See example in containers/HomePage/saga.js
|
||||
import {
|
||||
getPermissionsSucceeded,
|
||||
getRoleSucceeded,
|
||||
} from './actions';
|
||||
|
||||
import {
|
||||
GET_PERMISSIONS,
|
||||
GET_ROLE,
|
||||
} from './constants';
|
||||
|
||||
export function* permissionsGet() {
|
||||
try {
|
||||
const response = yield call(request, '/users-permissions/permissions', { method: 'GET' });
|
||||
yield put(getPermissionsSucceeded(response));
|
||||
} catch(err) {
|
||||
window.Strapi.notification.error('users-permissions.EditPage.notification.permissions.error');
|
||||
}
|
||||
}
|
||||
|
||||
export function* roleGet(action) {
|
||||
try {
|
||||
const role = yield call(request, `/users-permissions/roles/${action.id}`, { method: 'GET' });
|
||||
|
||||
yield put(getRoleSucceeded(role));
|
||||
} catch(err) {
|
||||
window.Strapi.notification.error('users-permissions.EditPage.notification.role.error');
|
||||
}
|
||||
}
|
||||
|
||||
export default function* defaultSaga() {
|
||||
const loadPermissionsWatcher = yield fork(takeLatest, GET_PERMISSIONS, permissionsGet);
|
||||
const loadRoleWatcher = yield fork(takeLatest, GET_ROLE, roleGet);
|
||||
|
||||
yield take(LOCATION_CHANGE);
|
||||
|
||||
yield cancel(loadPermissionsWatcher);
|
||||
yield cancel(loadRoleWatcher);
|
||||
}
|
||||
|
||||
@ -1,3 +1,7 @@
|
||||
.container {
|
||||
padding-top: 18px;
|
||||
}
|
||||
|
||||
.containerFluid {
|
||||
padding: 18px 30px;
|
||||
> div:first-child {
|
||||
|
||||
@ -52,6 +52,9 @@
|
||||
"EditPage.header.description": "{description} ",
|
||||
"EditPage.header.description.create": " ",
|
||||
|
||||
"EditPage.notification.permissions.error": "An error occurred while fetching permissions",
|
||||
"EditPage.notification.role.error": "An error occurred while fetching the role",
|
||||
|
||||
"HeaderNav.link.advancedSettings": "Advanced settings",
|
||||
"HeaderNav.link.emailTemplates": "Email templates",
|
||||
"HeaderNav.link.providers": "Providers",
|
||||
|
||||
@ -47,11 +47,15 @@
|
||||
"EditPage.form.roles.label.description": "Description",
|
||||
"EditPage.form.roles.label.name": "Nom",
|
||||
"EditPage.form.roles.label.users": "Utilisateurs associés avec ce rôle ({number})",
|
||||
"EditPage.header.title": "{name}",
|
||||
"EditPage.header.title": "{name} ",
|
||||
"EditPage.header.title.create": "Créez un nouveau rôle",
|
||||
"EditPage.header.description": "{description}",
|
||||
"EditPage.header.description": "{description} ",
|
||||
"EditPage.header.description.create": " ",
|
||||
|
||||
"EditPage.notification.permissions.error": "Une erreur est survenue en récupérant les permissions",
|
||||
"EditPage.notification.role.error": "Une erreur est survenue en récupérant le rôle",
|
||||
|
||||
|
||||
"HeaderNav.link.advancedSettings": "Paramètres avancés",
|
||||
"HeaderNav.link.emailTemplates": "Templates d'email",
|
||||
"HeaderNav.link.providers": "Fournisseurs",
|
||||
|
||||
@ -40,5 +40,25 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"permissions": {
|
||||
"application": {
|
||||
"icon": "",
|
||||
"description": "users-permissions.EditPage.permissions.application.description",
|
||||
"controllers": {
|
||||
"invoices": {
|
||||
"actions": {
|
||||
"delete": {
|
||||
"enabled": true,
|
||||
"policy": "entriesCreatedByUser"
|
||||
},
|
||||
"update": {
|
||||
"enabled": false,
|
||||
"policy": "entriesCreatedByUser"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,6 +24,14 @@
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
{
|
||||
"method": "GET",
|
||||
"path": "/permissions",
|
||||
"handler": "UsersPermissions.getPermissions",
|
||||
"config": {
|
||||
"policies": []
|
||||
}
|
||||
},
|
||||
|
||||
{
|
||||
"method": "POST",
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
const fakeData = require('../config/fakeData.json');
|
||||
const _ = require('lodash');
|
||||
/**
|
||||
* UsersPermissions.js controller
|
||||
*
|
||||
@ -13,10 +15,24 @@ module.exports = {
|
||||
*
|
||||
* @return {Object}
|
||||
*/
|
||||
|
||||
getPermissions: async(ctx) => {
|
||||
try {
|
||||
ctx.send({ permissions: fakeData.permissions });
|
||||
} catch(err) {
|
||||
ctx.badRequest(null, [{ message: [{ id: 'Not Found' }] }]);
|
||||
}
|
||||
},
|
||||
|
||||
getRole: async(ctx) => {
|
||||
const { id } = ctx.params;
|
||||
const role = fakeData[id];
|
||||
|
||||
ctx.send({ ok: true });
|
||||
if (_.isEmpty(role)) {
|
||||
return ctx.badRequest(null, [{ messages: [{ id: 'Role don\'t exist' }] }]);
|
||||
}
|
||||
|
||||
return ctx.send({ role });
|
||||
},
|
||||
|
||||
index: async (ctx) => {
|
||||
|
||||
@ -59,4 +59,4 @@
|
||||
"npm": ">= 3.0.0"
|
||||
},
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user