Software: Apache/2.4.41 (Ubuntu). PHP/8.0.30 uname -a: Linux apirnd 5.4.0-204-generic #224-Ubuntu SMP Thu Dec 5 13:38:28 UTC 2024 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) Safe-mode: OFF (not secure) /var/www/html/wincloud_gateway/node_modules/strapi-admin/admin/src/hooks/useSettingsForm/ drwxr-xr-x | |
| Viewing file: Select action/file-type: import { useEffect, useReducer } from 'react';
import { request } from 'strapi-helper-plugin';
import { get, has, omit } from 'lodash';
import { checkFormValidity, formatAPIErrors } from '../../utils';
import { initialState, reducer } from './reducer';
import init from './init';
const useSettingsForm = (endPoint, schema, cbSuccess, fieldsToPick) => {
const [
{ formErrors, initialData, isLoading, modifiedData, showHeaderButtonLoader, showHeaderLoader },
dispatch,
] = useReducer(reducer, initialState, () => init(initialState, fieldsToPick));
useEffect(() => {
const getData = async () => {
try {
const { data } = await request(endPoint, { method: 'GET' });
dispatch({
type: 'GET_DATA_SUCCEEDED',
data,
fieldsToPick,
});
} catch (err) {
console.error(err.response);
strapi.notification.toggle({
type: 'warning',
message: { id: 'notification.error' },
});
}
};
if (endPoint) {
getData();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [endPoint]);
const handleCancel = () => {
dispatch({
type: 'ON_CANCEL',
});
};
const handleChange = ({ target: { name, value, type: inputType } }) => {
dispatch({
type: 'ON_CHANGE',
inputType,
keys: name,
value,
});
};
const setField = (fieldName, value) => {
dispatch({
type: 'ON_CHANGE',
keys: fieldName,
value,
});
};
const handleSubmit = async e => {
e.preventDefault();
const errors = await checkFormValidity(modifiedData, schema);
dispatch({
type: 'SET_ERRORS',
errors: errors || {},
});
if (!errors) {
try {
strapi.lockAppWithOverlay();
dispatch({
type: 'ON_SUBMIT',
});
const cleanedData = omit(modifiedData, ['confirmPassword', 'registrationToken']);
if (cleanedData.roles) {
cleanedData.roles = cleanedData.roles.map(role => role.id);
}
const { data } = await request(endPoint, {
method: 'PUT',
body: cleanedData,
});
cbSuccess(data);
dispatch({
type: 'ON_SUBMIT_SUCCEEDED',
data,
});
strapi.notification.toggle({
type: 'success',
message: { id: 'notification.success.saved' },
});
} catch (err) {
const data = get(err, 'response.payload', { data: {} });
if (has(data, 'data') && typeof data.data === 'string') {
strapi.notification.toggle({
type: 'warning',
message: data.data,
});
} else {
strapi.notification.toggle({
type: 'warning',
message: data.message,
});
}
const apiErrors = formatAPIErrors(data);
dispatch({
type: 'SET_ERRORS',
errors: apiErrors,
});
} finally {
strapi.unlockApp();
}
}
};
return [
{ formErrors, initialData, isLoading, modifiedData, showHeaderButtonLoader, showHeaderLoader },
dispatch,
{ handleCancel, handleChange, handleSubmit, setField },
];
};
export default useSettingsForm;
|
:: Command execute :: | |
--[ c99shell v. 2.5 [PHP 8 Update] [24.05.2025] | Generation time: 0.0056 ]-- |