Skip to content

Commit

Permalink
add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
VovaStelmashchuk committed Jan 4, 2025
1 parent 8cf6775 commit ce38a15
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
63 changes: 60 additions & 3 deletions pages/project/[slug].vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="flex flex-row">
<div v-if="data" class="flex-grow">
<div
v-if="data.files && data.files.length"
v-if="data && data.files && data.files.length"
class="grid gap-2"
:class="gridColsClass"
>
Expand Down Expand Up @@ -57,6 +57,7 @@
<input
id="width"
type="text"
v-model="widthPlate"
class="w-full border border-gray-300 rounded-lg shadow-sm p-2 focus:ring-2 focus:ring-blue-500 focus:outline-none"
/>
</div>
Expand All @@ -69,6 +70,7 @@
<input
id="height"
type="text"
v-model="heightPlate"
class="w-full border border-gray-300 rounded-lg shadow-sm p-2 focus:ring-2 focus:ring-blue-500 focus:outline-none"
/>
</div>
Expand All @@ -81,6 +83,7 @@
<input
id="tolerance"
type="text"
v-model="tolerance"
class="w-full border border-gray-300 rounded-lg shadow-sm p-2 focus:ring-2 focus:ring-blue-500 focus:outline-none"
/>
</div>
Expand All @@ -96,6 +99,9 @@
>
Nest
</button>
<div v-if="nestRequestError" class="text-red-500 mt-2">
{{ nestRequestError }}
</div>
</div>
</div>
</div>
Expand All @@ -111,6 +117,12 @@ import { watch } from "vue";
const route = useRoute();
const slug = route.params.slug;
const nestRequestError = ref(null);
const widthPlate = ref("");
const heightPlate = ref("");
const tolerance = ref("");
const counters = ref({});
const selectedFileCount = ref(0);
Expand All @@ -126,6 +138,7 @@ const gridColsClass = computed(() => {
watch(
() => data,
(data) => {
if (!data || !data.value || !data.value.files) return;
data.value.files.forEach((file) => {
counters.value[file.slug] = 0;
});
Expand All @@ -134,6 +147,7 @@ watch(
);
const updateCounter = () => {
nestRequestError.value = null;
const conuts = Object.values(counters.value);
selectedFileCount.value = conuts.reduce((acc, curr) => acc + curr, 0);
};
Expand All @@ -146,11 +160,54 @@ const increment = (slug) => {
const decrement = (slug) => {
if (counters.value[slug] > 0) {
counters.value[slug]--;
updateCounter();
}
};
const nest = () => {
console.log("Nest");
const nest = async () => {
nestRequestError.value = null;
const filesToNest = Object.entries(counters.value)
.filter(([_, count]) => count > 0)
.map(([slug, count]) => {
return {
slug,
count,
};
});
if (filesToNest.length === 0) {
nestRequestError.value = "Please select at least one file to nest.";
return;
}
const widthValue = parseFloat(widthPlate.value);
const heightValue = parseFloat(heightPlate.value);
const toleranceValue = parseFloat(tolerance.value);
if (isNaN(widthValue) || isNaN(heightValue) || isNaN(toleranceValue)) {
nestRequestError.value =
"Please enter valid values for width, height, and tolerance.";
return;
}
const request = {
files: filesToNest,
params: {
width: widthValue,
height: heightValue,
tolerance: toleranceValue,
},
};
const response = await fetch(`/api/project/${slug}/nest`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(request),
});
console.log(response);
};
</script>

Expand Down
2 changes: 1 addition & 1 deletion server/api/project/[slug].get.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default defineEventHandler(async (event) => {
const project = await db.collection("projects_v2").findOne({ slug: slug });

if (!project) {
return { statusCode: 404, body: { error: "Project not found" } };
throw createError({ statusCode: 404, message: "Project not found" });
}

return {
Expand Down
2 changes: 1 addition & 1 deletion server/core/dxf/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { entityToPoints } from "./entityToPoints.js";
* @returns {{closed: {polygon: {x: number, y: number}[], originEntities: any[]}[], open: {polygon: {x: number, y: number}[], originEntities: any[]}[]}}
*/
export function parseAndCombine(dxfString, tolerance) {
const dxfObj = new Entities(dxfString);
const dxfObj = new Entities(dxfString, tolerance);
const rawEntities = dxfObj.entities || [];

const lines = [];
Expand Down

0 comments on commit ce38a15

Please sign in to comment.