mirror of
https://github.com/strapi/strapi.git
synced 2025-11-10 15:19:00 +00:00
Add submit logic
Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
parent
626f731270
commit
fa99e43b9c
@ -83,6 +83,8 @@ const ProfilePage = () => {
|
|||||||
type: 'ON_SUBMIT_SUCCEEDED',
|
type: 'ON_SUBMIT_SUCCEEDED',
|
||||||
data,
|
data,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
strapi.notification.success('notification.success.saved');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const data = get(err, 'response.payload', { data: {} });
|
const data = get(err, 'response.payload', { data: {} });
|
||||||
const apiErrors = formatAPIErrors(data);
|
const apiErrors = formatAPIErrors(data);
|
||||||
|
|||||||
@ -24,11 +24,10 @@ import init from './init';
|
|||||||
|
|
||||||
const EditPage = () => {
|
const EditPage = () => {
|
||||||
const { settingsBaseURL } = useGlobalContext();
|
const { settingsBaseURL } = useGlobalContext();
|
||||||
const [{ formErrors, isLoading, initialData, modifiedData }, dispatch] = useReducer(
|
const [
|
||||||
reducer,
|
{ formErrors, isLoading, initialData, modifiedData, showHeaderLoader },
|
||||||
initialState,
|
dispatch,
|
||||||
init
|
] = useReducer(reducer, initialState, init);
|
||||||
);
|
|
||||||
const { formatMessage } = useIntl();
|
const { formatMessage } = useIntl();
|
||||||
const {
|
const {
|
||||||
params: { id },
|
params: { id },
|
||||||
@ -75,6 +74,8 @@ const EditPage = () => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// TODO: remove this line when API's ready
|
||||||
|
// eslint-disable-next-line consistent-return
|
||||||
const handleSubmit = async e => {
|
const handleSubmit = async e => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
@ -85,11 +86,38 @@ const EditPage = () => {
|
|||||||
errors: errors || {},
|
errors: errors || {},
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log(errors);
|
|
||||||
|
|
||||||
if (!errors) {
|
if (!errors) {
|
||||||
// todo
|
try {
|
||||||
console.log('will submit');
|
strapi.lockAppWithOverlay();
|
||||||
|
|
||||||
|
dispatch({
|
||||||
|
type: 'ON_SUBMIT',
|
||||||
|
});
|
||||||
|
|
||||||
|
return new Promise(resolve => {
|
||||||
|
setTimeout(() => {
|
||||||
|
dispatch({
|
||||||
|
type: 'ON_SUBMIT_SUCCEEDED',
|
||||||
|
// TODO
|
||||||
|
// data,
|
||||||
|
});
|
||||||
|
|
||||||
|
strapi.notification.success('notification.success.saved');
|
||||||
|
resolve();
|
||||||
|
}, 1000);
|
||||||
|
});
|
||||||
|
} catch (err) {
|
||||||
|
// const data = get(err, 'response.payload', { data: {} });
|
||||||
|
// const apiErrors = formatAPIErrors(data);
|
||||||
|
// dispatch({
|
||||||
|
// type: 'SET_ERRORS',
|
||||||
|
// errors: apiErrors,
|
||||||
|
// });
|
||||||
|
// TODO
|
||||||
|
strapi.notification.error('An error occured');
|
||||||
|
} finally {
|
||||||
|
strapi.unlockApp();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -103,7 +131,7 @@ const EditPage = () => {
|
|||||||
<Header
|
<Header
|
||||||
// TODO: remove the content it is temporary
|
// TODO: remove the content it is temporary
|
||||||
content="Waiting for the API, this text will be removed when connected to the back-end"
|
content="Waiting for the API, this text will be removed when connected to the back-end"
|
||||||
isLoading={isLoading}
|
isLoading={showHeaderLoader}
|
||||||
initialData={initialData}
|
initialData={initialData}
|
||||||
label={headerLabel}
|
label={headerLabel}
|
||||||
modifiedData={modifiedData}
|
modifiedData={modifiedData}
|
||||||
|
|||||||
@ -27,23 +27,23 @@ const reducer = (state, action) =>
|
|||||||
case 'ON_CHANGE': {
|
case 'ON_CHANGE': {
|
||||||
if (action.inputType === 'password' && !action.value) {
|
if (action.inputType === 'password' && !action.value) {
|
||||||
unset(draftState.modifiedData, action.keys.split('.'));
|
unset(draftState.modifiedData, action.keys.split('.'));
|
||||||
} else if (action.keys.includes('username')) {
|
} else if (action.keys.includes('username') && !action.value) {
|
||||||
set(draftState.modifiedData, action.keys.split('.'), null);
|
set(draftState.modifiedData, action.keys.split('.'), null);
|
||||||
} else {
|
} else {
|
||||||
set(draftState.modifiedData, action.keys.split('.'), action.value);
|
set(draftState.modifiedData, action.keys.split('.'), action.value);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
// case 'ON_SUBMIT': {
|
case 'ON_SUBMIT': {
|
||||||
// draftState.showHeaderLoader = true;
|
draftState.showHeaderLoader = true;
|
||||||
// break;
|
break;
|
||||||
// }
|
}
|
||||||
// case 'ON_SUBMIT_SUCCEEDED': {
|
case 'ON_SUBMIT_SUCCEEDED': {
|
||||||
// draftState.initialData = pick(action.data, ['email', 'firstname', 'lastname', 'username']);
|
// TODO fix this and add tests
|
||||||
// draftState.modifiedData = pick(action.data, ['email', 'firstname', 'lastname', 'username']);
|
draftState.initialData = state.modifiedData;
|
||||||
// draftState.showHeaderLoader = false;
|
draftState.showHeaderLoader = false;
|
||||||
// break;
|
break;
|
||||||
// }
|
}
|
||||||
case 'SET_ERRORS': {
|
case 'SET_ERRORS': {
|
||||||
draftState.formErrors = action.errors;
|
draftState.formErrors = action.errors;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@ -10,4 +10,152 @@ describe('ADMIN | CONTAINERS | USERS | EditPage | reducer', () => {
|
|||||||
expect(reducer(initialState, {})).toEqual(initialState);
|
expect(reducer(initialState, {})).toEqual(initialState);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('ON_CANCEL', () => {
|
||||||
|
it('should set the modifiedData with the initialData', () => {
|
||||||
|
const initialState = {
|
||||||
|
initialData: {
|
||||||
|
email: 'john@strapi.io',
|
||||||
|
firstname: '',
|
||||||
|
},
|
||||||
|
modifiedData: {
|
||||||
|
email: 'john@strapi.io',
|
||||||
|
firstname: 'test',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const action = {
|
||||||
|
type: 'ON_CANCEL',
|
||||||
|
};
|
||||||
|
const expected = {
|
||||||
|
initialData: {
|
||||||
|
email: 'john@strapi.io',
|
||||||
|
firstname: '',
|
||||||
|
},
|
||||||
|
modifiedData: {
|
||||||
|
email: 'john@strapi.io',
|
||||||
|
firstname: '',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(reducer(initialState, action)).toEqual(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('ON_CHANGE', () => {
|
||||||
|
it('should change the data correctly if the inputType is not password', () => {
|
||||||
|
const initialState = {
|
||||||
|
modifiedData: {
|
||||||
|
email: 'john@strapi.io',
|
||||||
|
firstname: null,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const action = {
|
||||||
|
type: 'ON_CHANGE',
|
||||||
|
keys: 'email',
|
||||||
|
inputType: 'email',
|
||||||
|
value: 'test123',
|
||||||
|
};
|
||||||
|
const expected = {
|
||||||
|
modifiedData: {
|
||||||
|
email: 'test123',
|
||||||
|
firstname: null,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(reducer(initialState, action)).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should change the data correctly if the inputType is password', () => {
|
||||||
|
const initialState = {
|
||||||
|
modifiedData: {
|
||||||
|
email: 'john@strapi.io',
|
||||||
|
password: 'pwd123',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const action = {
|
||||||
|
type: 'ON_CHANGE',
|
||||||
|
keys: 'password',
|
||||||
|
inputType: 'password',
|
||||||
|
value: 'pwd1234',
|
||||||
|
};
|
||||||
|
const expected = {
|
||||||
|
modifiedData: {
|
||||||
|
email: 'john@strapi.io',
|
||||||
|
password: 'pwd1234',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(reducer(initialState, action)).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should change the data correctly if the inputType is password and the value is empty', () => {
|
||||||
|
const initialState = {
|
||||||
|
modifiedData: {
|
||||||
|
email: 'john@strapi.io',
|
||||||
|
password: 'pwd123',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const action = {
|
||||||
|
type: 'ON_CHANGE',
|
||||||
|
keys: 'password',
|
||||||
|
inputType: 'password',
|
||||||
|
value: '',
|
||||||
|
};
|
||||||
|
const expected = {
|
||||||
|
modifiedData: {
|
||||||
|
email: 'john@strapi.io',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(reducer(initialState, action)).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should set the username value to null if the value is empty', () => {
|
||||||
|
const initialState = {
|
||||||
|
modifiedData: {
|
||||||
|
email: 'john@strapi.io',
|
||||||
|
password: 'pwd123',
|
||||||
|
username: 'test',
|
||||||
|
},
|
||||||
|
};
|
||||||
|
const action = {
|
||||||
|
type: 'ON_CHANGE',
|
||||||
|
keys: 'username',
|
||||||
|
inputType: 'text',
|
||||||
|
value: '',
|
||||||
|
};
|
||||||
|
const expected = {
|
||||||
|
modifiedData: {
|
||||||
|
email: 'john@strapi.io',
|
||||||
|
password: 'pwd123',
|
||||||
|
username: null,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(reducer(initialState, action)).toEqual(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('ON_SUBMIT', () => {
|
||||||
|
it('should change the showHeaderLoader property to true', () => {
|
||||||
|
const initialState = {
|
||||||
|
initialData: {},
|
||||||
|
modifiedData: {},
|
||||||
|
isLoading: false,
|
||||||
|
showHeaderLoader: false,
|
||||||
|
};
|
||||||
|
const expected = {
|
||||||
|
initialData: {},
|
||||||
|
modifiedData: {},
|
||||||
|
isLoading: false,
|
||||||
|
showHeaderLoader: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
const action = {
|
||||||
|
type: 'ON_SUBMIT',
|
||||||
|
};
|
||||||
|
|
||||||
|
expect(reducer(initialState, action)).toEqual(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -6,7 +6,7 @@ const data = {
|
|||||||
username: null,
|
username: null,
|
||||||
email: 'soup@soup.com',
|
email: 'soup@soup.com',
|
||||||
isActive: false,
|
isActive: false,
|
||||||
roles: [],
|
roles: [{ id: 1, name: 'Super Admin' }],
|
||||||
registrationToken: 'my-super-token',
|
registrationToken: 'my-super-token',
|
||||||
},
|
},
|
||||||
other: {
|
other: {
|
||||||
@ -16,7 +16,10 @@ const data = {
|
|||||||
username: 'Display username',
|
username: 'Display username',
|
||||||
email: 'soup@soup.com',
|
email: 'soup@soup.com',
|
||||||
isActive: false,
|
isActive: false,
|
||||||
roles: [],
|
roles: [
|
||||||
|
{ id: 1, name: 'Super Admin' },
|
||||||
|
{ id: 2, name: 'Author' },
|
||||||
|
],
|
||||||
registrationToken: null,
|
registrationToken: null,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -312,6 +312,7 @@
|
|||||||
"notification.form.success.fields": "Changes saved",
|
"notification.form.success.fields": "Changes saved",
|
||||||
"notification.link-copied": "Link copied into the clipboard",
|
"notification.link-copied": "Link copied into the clipboard",
|
||||||
"notification.success.delete": "The item has been deleted",
|
"notification.success.delete": "The item has been deleted",
|
||||||
|
"notification.success.saved": "Saved",
|
||||||
"global.prompt.unsaved": "Are you sure you want to leave this page? All your modifications will be lost",
|
"global.prompt.unsaved": "Are you sure you want to leave this page? All your modifications will be lost",
|
||||||
|
|
||||||
"app.components.Users.SortPicker.button-label": "Sort by",
|
"app.components.Users.SortPicker.button-label": "Sort by",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user