Skip to content

Commit

Permalink
batch delete articles
Browse files Browse the repository at this point in the history
  • Loading branch information
Kworz committed Mar 19, 2024
1 parent 3341b7a commit 7c93173
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/lib/i18n/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@
"store_name": "Store name",
"store": "Store",
"store_list_linked": "List linked store",
"warning": "Warning"
"warning": "Warning",
"close": "Close"
},
"time": {
"hours": "Hours"
Expand Down
9 changes: 7 additions & 2 deletions src/lib/i18n/lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@
"warning": "Attention",
"order_quantity": "Quantité de commande minimum",
"unit_of_work_quantity": "Quantité d'unité d'oeuvre",
"non_physical": "Article non physique"
"non_physical": "Article non physique",
"close": "Fermer"
},
"time": {
"hours": "Heures"
Expand Down Expand Up @@ -311,7 +312,11 @@
"title": "Modifier l'article {name}"
},
"delete-article-confirmation": "Souhaitez vous supprimer l'article {name} ?"
}
},
"actions": {
"delete": "Supprimer {n, plural, =1 {un article} other {des articles}}"
},
"delete_batch": "Souhaitez vous vraiment supprimer {n, plural, =1 {cet articles} other {ces {n} articles}} ? Aucune sécurité sur les dépendances n'est vérifiée ici."
},
"suppliers": {
"list": {
Expand Down
14 changes: 13 additions & 1 deletion src/routes/app/(scm)/scm/articles/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,21 @@ export const actions: Actions = {
catch(ex)
{
console.log(ex);
return fail(400, { create: { error: "scm.article.create.error.generic" }})
return fail(400, { create: { error: "errors.scm.article.create.generic" }})
}

throw redirect(303, `/app/scm/articles/${createdArticle.id}`);
},
delete: async ({ params, locals, request }) => {

const form = await request.formData();
const articles = form.get("articles")?.toString();

if(!articles)
return fail(400, { delete: { error: "errors.scm.article.delete.no-articles" }});

await locals.prisma.scm_article.deleteMany({ where: { id: { in: articles.split(",") }}});

return { delete: { success: true }}
}
};
37 changes: 34 additions & 3 deletions src/routes/app/(scm)/scm/articles/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,35 @@
import Table from "$lib/components/generics/table/Table.svelte";
import TableCell from "$lib/components/generics/table/TableCell.svelte";
import TablePages from "$lib/components/generics/table/TablePages.svelte";
import { ArrowDownTray, ArrowUpTray, PlusCircle, QrCode } from "@steeze-ui/heroicons";
import type { PageData } from "./$types";
import { ArrowDownTray, ArrowUpTray, PlusCircle, QrCode, Trash } from "@steeze-ui/heroicons";
import type { ActionData, PageData } from "./$types";
import EmptyData from "$lib/components/EmptyData.svelte";
import { computeArticlePrice } from "$lib/components/derived/article/article";
import { _ } from "svelte-i18n";
import { browser } from "$app/environment";
import Modal from "$lib/components/generics/modal/Modal.svelte";
import TableCellCheckbox from "$lib/components/generics/table/TableCellCheckbox.svelte";
export let data: PageData;
export let form: ActionData;
let filter = $page.url.searchParams.has("filter") ? JSON.parse(decodeURIComponent($page.url.searchParams.get("filter") as string)) : {};
let sort = $page.url.searchParams.has("sort") ? JSON.parse(decodeURIComponent($page.url.searchParams.get("sort") as string)) : {};
let tablePage = $page.url.searchParams.has("page") ? parseInt($page.url.searchParams.get("page") as string) : 0;
let selected: Array<string> = [];
let createArticle = false;
let deleteArticles = false;
let deleteArticleSuspense = false;
const refresh = () => { if(browser) goto(`?filter=${encodeURIComponent(JSON.stringify(filter))}&sort=${encodeURIComponent(JSON.stringify(sort))}&page=${tablePage}`); }
$: filter, sort, tablePage, refresh();
$: if(form?.delete) { deleteArticleSuspense = false; }
$: if(form?.delete !== undefined && "success" in form.delete) { deleteArticles = false; selected = []; }
</script>

<svelte:head>
Expand All @@ -55,6 +64,27 @@
</MenuSide>
{/if}

{#if deleteArticles}
<Modal title={$_('app.action.delete')} on:close={() => deleteArticles = false}>
{#if form?.delete?.error !== undefined}
<p>{$_(form.delete.error)}</p>
{:else}
<p>{$_('scm.article.delete_batch', { values: { n: selected.length }})}</p>
{/if}

<form class="flex flex-row gap-4" action="?/delete" slot="form" method="post" use:enhance on:submit={() => deleteArticleSuspense = true}>
{#if form?.delete?.error !== undefined}
<Button size="small" preventSend role="tertiary" click={() => deleteArticles = false}>{$_('app.generic.close')}</Button>
{:else}
<input type="hidden" value={selected.join(',')} name="articles" />
<Button size="small" role="danger" suspense={deleteArticleSuspense}>{$_('app.action.delete')}</Button>
<Button size="small" preventSend role="tertiary" click={() => deleteArticles = false}>{$_('app.generic.cancel')}</Button>
{/if}

</form>
</Modal>
{/if}

<h1>{$_('app.generic.articles')}</h1>
<p>{$_('scm.articles.description')}</p>

Expand All @@ -64,6 +94,7 @@
<PillMenuButton icon={ArrowUpTray} click={() => window.open(`/app/scm/articles/export/?articles=${selected.join(',')}`, '_blank')?.focus()} role="secondary">{$_('scm.articles.action.export_articles', { values: { n: selected.length }})}</PillMenuButton>
{#if selected.length > 0}
<PillMenuButton icon={QrCode} click={() => window.open(`/app/scm/articles/print/?articles=${selected.join(',')}`, '_blank')?.focus()}>{$_('scm.articles.action.print_label', { values: { n: selected.length }})}</PillMenuButton>
<PillMenuButton icon={Trash} click={() => deleteArticles = true}>{$_('scm.article.actions.delete', { values: { n: selected.length }})}</PillMenuButton>
{/if}
</PillMenu>

Expand Down Expand Up @@ -96,7 +127,7 @@
{@const price = computeArticlePrice(article.order_rows)}
{@const stockQuantity = article.store_relations.filter(sr => sr.store.assemblies_buylist === null).reduce((c, p) => p.quantity + c, 0)}

<TableCell class="items-center"><input type="checkbox" bind:group={selected} value={article.id} /></TableCell>
<TableCellCheckbox bind:group={selected} value={article.id} />
<TableCell>
<ArticleRow {article} displayPrice={false} displayManufacturer={false} displayInboundSupplies />
</TableCell>
Expand Down

0 comments on commit 7c93173

Please sign in to comment.