Skip to content

Commit

Permalink
Allow deleting shipping zone
Browse files Browse the repository at this point in the history
  • Loading branch information
treoden committed Apr 7, 2024
1 parent 42ee8cb commit 98d9c17
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import React from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';
import { toast } from 'react-toastify';
import { Card } from '@components/admin/cms/Card';
import MapIcon from '@heroicons/react/solid/esm/LocationMarkerIcon';
import { useModal } from '@components/common/modal/useModal';
Expand All @@ -13,7 +15,7 @@ function Zone({ zone, countries, getZones }) {
title={
<div className="flex justify-between items-center gap-2">
<div>{zone.name}</div>
<div>
<div className="flex justify-between gap-2">
<a
href="#"
className="text-interactive"
Expand All @@ -24,6 +26,33 @@ function Zone({ zone, countries, getZones }) {
>
Edit Zone
</a>
<a
className="text-critical"
href="#"
onClick={async (e) => {
e.preventDefault();
try {
const response = await axios.delete(zone.deleteApi);
if (response.status === 200) {
// Toast success
toast.success('Zone removed successfully');
// Delay for 2 seconds
setTimeout(() => {
// Reload page
window.location.reload();
}, 1500);
} else {
// Toast error
toast.error('Failed to remove zone');
}
} catch (error) {
// Toast error
toast.error('Failed to remove zone');
}
}}
>
Remove Zone
</a>
</div>
</div>
}
Expand Down Expand Up @@ -97,6 +126,7 @@ Zone.propTypes = {
})
),
addMethodApi: PropTypes.string,
deleteApi: PropTypes.string,
updateApi: PropTypes.string
}).isRequired,
countries: PropTypes.arrayOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import Zone from './Zone';
export function Zones({ countries, getZones, zones }) {
return (
<>
{zones.map((zone) => <Zone zone={zone} getZones={getZones} countries={countries} />)}
{zones.map((zone) => (
<Zone zone={zone} getZones={getZones} countries={countries} />
))}
</>
);
}
Expand Down Expand Up @@ -34,7 +36,8 @@ Zones.propTypes = {
uuid: PropTypes.string.isRequired,
name: PropTypes.string.isRequired
})
).isRequired
).isRequired,
deleteApi: PropTypes.string.isRequired
})
).isRequired,
getZones: PropTypes.func.isRequired
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-disable camelcase */
const {
rollback,
commit,
startTransaction,
del,
select
} = require('@evershop/postgres-query-builder');
const {
getConnection
} = require('@evershop/evershop/src/lib/postgres/connection');
const {
OK,
INTERNAL_SERVER_ERROR,
INVALID_PAYLOAD
} = require('@evershop/evershop/src/lib/util/httpStatus');
const { error } = require('@evershop/evershop/src/lib/log/logger');

// eslint-disable-next-line no-unused-vars
module.exports = async (request, response, deledate, next) => {
const { id } = request.params;
const connection = await getConnection();
await startTransaction(connection);
try {
// Load the shipping zone
const shippingZone = await select()
.from('shipping_zone')
.where('uuid', '=', id)
.load(connection);

if (!shippingZone) {
response.status(INVALID_PAYLOAD);
response.json({
error: {
status: INVALID_PAYLOAD,
message: 'Invalid zone id'
}
});
return;
}
const zone = await del('shipping_zone')
.where('uuid', '=', id)
.execute(connection);

await commit(connection);
response.status(OK);
response.json({
data: zone
});
} catch (e) {
error(e);
await rollback(connection);
response.status(INTERNAL_SERVER_ERROR);
response.json({
error: {
status: INTERNAL_SERVER_ERROR,
message: e.message
}
});
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"methods": ["DELETE"],
"path": "/shippingZones/:id",
"access": "private"
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type ShippingZone {
provinces: [Province]
methods: [ShippingMethodByZone]
updateApi: String!
deleteApi: String!
addMethodApi: String!
removeMethodApi: String!
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ module.exports = {
.execute(pool);
return provinces.map((row) => row.province);
},
updateApi: async ({ uuid }) => buildUrl('updateShippingZone', { id: uuid }),
addMethodApi: async ({ uuid }) =>
buildUrl('addShippingZoneMethod', { id: uuid }),
removeMethodApi: async ({ uuid }) =>
updateApi: ({ uuid }) => buildUrl('updateShippingZone', { id: uuid }),
deleteApi: ({ uuid }) => buildUrl('deleteShippingZone', { id: uuid }),
addMethodApi: ({ uuid }) => buildUrl('addShippingZoneMethod', { id: uuid }),
removeMethodApi: ({ uuid }) =>
buildUrl('removeShippingZoneMethod', { id: uuid })
},
ShippingMethodByZone: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const ZonesQuery = `
updateApi
}
updateApi
deleteApi
addMethodApi
}
}
Expand Down

0 comments on commit 98d9c17

Please sign in to comment.