Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Product Category switch button logic #1145

Open
wants to merge 6 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions frontend/src/components/DataTable/DataTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ export default function DataTable({ config, extra = [] }) {
panel.open();
collapsedBox.open();
}
function handleSwitchEdit(record, key) {
const newRecord = {
...record,
[key]: !record[key]
}
dispatch(crud.update({ entity, id: newRecord._id, jsonData: newRecord, switchUpdate: true }))
.then(() => {
dispatch(crud.list({ entity }));
});
}
function handleDelete(record) {
dispatch(crud.currentAction({ actionType: 'delete', data: record }));
modal.open();
Expand All @@ -99,7 +109,7 @@ export default function DataTable({ config, extra = [] }) {

let dispatchColumns = [];
if (fields) {
dispatchColumns = [...dataForTable({ fields, translate, moneyFormatter, dateFormat })];
dispatchColumns = [...dataForTable({ fields, translate, moneyFormatter, dateFormat, handleSwitchEdit })];
} else {
dispatchColumns = [...dataTableColumns];
}
Expand Down Expand Up @@ -149,7 +159,12 @@ export default function DataTable({ config, extra = [] }) {

const { result: listResult, isLoading: listIsLoading } = useSelector(selectListItems);

const { pagination, items: dataSource } = listResult;
const sortedItems = [...listResult.items].sort((a, b) => a.name.localeCompare(b.name));

const { pagination, items: dataSource } = {
...listResult,
items: sortedItems,
};

const dispatch = useDispatch();

Expand Down
16 changes: 9 additions & 7 deletions frontend/src/redux/crud/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export const crud = {
}
},
update:
({ entity, id, jsonData, withUpload = false }) =>
({ entity, id, jsonData, withUpload = false, switchUpdate = false }) =>
async (dispatch) => {
dispatch({
type: actionTypes.REQUEST_LOADING,
Expand All @@ -145,15 +145,17 @@ export const crud = {
if (withUpload) {
data = await request.updateAndUpload({ entity, id, jsonData });
} else {
data = await request.update({ entity, id, jsonData });
data = await request.update({ entity, id, jsonData, switchUpdate });
}

if (data.success === true) {
dispatch({
type: actionTypes.REQUEST_SUCCESS,
keyState: 'update',
payload: data.result,
});
if(!switchUpdate) {
dispatch({
type: actionTypes.REQUEST_SUCCESS,
keyState: 'update',
payload: data.result,
});
}
dispatch({
type: actionTypes.CURRENT_ITEM,
payload: data.result,
Expand Down
12 changes: 7 additions & 5 deletions frontend/src/request/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,15 @@ const request = {
return errorHandler(error);
}
},
update: async ({ entity, id, jsonData }) => {
update: async ({ entity, id, jsonData, switchUpdate }) => {
try {
const response = await axios.patch(entity + '/update/' + id, jsonData);
successHandler(response, {
notifyOnSuccess: true,
notifyOnFailed: true,
});
if(!switchUpdate) {
successHandler(response, {
notifyOnSuccess: true,
notifyOnFailed: true,
});
}
return response.data;
} catch (error) {
return errorHandler(error);
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/utils/dataStructure.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const dataForRead = ({ fields, translate }) => {
return columns;
};

export function dataForTable({ fields, translate, moneyFormatter, dateFormat }) {
export function dataForTable({ fields, translate, moneyFormatter, dateFormat, handleSwitchEdit }) {
let columns = [];

Object.keys(fields).forEach((key) => {
Expand All @@ -43,6 +43,7 @@ export function dataForTable({ fields, translate, moneyFormatter, dateFormat })
checked={record[key]}
checkedChildren={<CheckOutlined />}
unCheckedChildren={<CloseOutlined />}
onChange={() => handleSwitchEdit(record, key)}
/>
),
},
Expand Down
Loading