Browse Source

preparing to create new items

Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
module/document
Stephan Richter 2 weeks ago
parent
commit
4b4a575356
  1. 2
      frontend/src/routes/stock/Index.svelte
  2. 29
      frontend/src/routes/stock/ItemList.svelte
  3. 8
      frontend/src/urls.svelte.js
  4. 8
      stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java

2
frontend/src/routes/stock/Index.svelte

@ -189,7 +189,7 @@ @@ -189,7 +189,7 @@
</h3>
<MarkdownEditor editable={true} value={location.description} type="div" onSet={newDesc => patchLocation(location,'description',newDesc)} />
{/if}
<ItemList items={data?.items.sort((a,b) => a.code.localeCompare(b.code))} bind:selected={item} drag_start={drag_item} />
<ItemList {location} items={data?.items.sort((a,b) => a.code.localeCompare(b.code))} bind:selected={item} drag_start={drag_item} />
</div>
<div class="properties">
<ItemProps {item} {properties} />

29
frontend/src/routes/stock/ItemList.svelte

@ -1,7 +1,20 @@ @@ -1,7 +1,20 @@
<script>
import { t } from '../../translations.svelte';
import { api, post } from '../../urls.svelte';
import { error, yikes } from '../../warn.svelte';
import { t } from '../../translations.svelte';
let { items, selected = $bindable(null), drag_start = item => console.log({dragging:item}) } = $props();
let { items, selected = $bindable(null), location = null, drag_start = item => console.log({dragging:item}) } = $props();
let newItem = $state({name:null, code: null});
async function saveNewItem(){
newItem.location = location;
console.log(JSON.parse(JSON.stringify(newItem)));
const url = api('stock/item');
const res = post(url,newItem);
if (res.ok) {
yikes();
} else error(res);
}
</script>
<table>
<thead>
@ -19,5 +32,17 @@ @@ -19,5 +32,17 @@
<td>{item.name}</td>
</tr>
{/each}
{#if location}
<tr>
<td>{t('create_new_object',{object:t('item')})}</td>
<td>
<input type="text" bind:value={newItem.code} />
</td>
<td>
<input type="text" bind:value={newItem.name} />
<button onclick={saveNewItem}>{t('save')}</button>
</td>
</tr>
{/if}
</tbody>
</table>

8
frontend/src/urls.svelte.js

@ -4,6 +4,14 @@ export function api(rel_path){ @@ -4,6 +4,14 @@ export function api(rel_path){
return `${location.protocol}//${location.host.replace('5173','8080')}/api/${rel_path}`;
}
export async function post(url,data){
return fetch(url,{
credentials : 'include',
method : 'POST',
body : JSON.stringify(data)
});
}
export function target(code){
if (!code) return null;
let altered = code;

8
stock/src/main/java/de/srsoftware/umbrella/stock/StockModule.java

@ -140,6 +140,7 @@ public class StockModule extends BaseHandler implements StockService { @@ -140,6 +140,7 @@ public class StockModule extends BaseHandler implements StockService {
if (user.isEmpty()) return unauthorized(ex);
var head = path.pop();
return switch (head) {
case ITEM -> postItem(user.get(), ex);
case LOCATION -> postLocation(user.get(),ex);
case PROPERTY -> postProperty(user.get(),ex);
case null, default -> super.doPost(path,ex);
@ -262,6 +263,13 @@ public class StockModule extends BaseHandler implements StockService { @@ -262,6 +263,13 @@ public class StockModule extends BaseHandler implements StockService {
return sendContent(ex,location);
}
private boolean postItem(UmbrellaUser user, HttpExchange ex) throws IOException {
var json = json(ex);
if (!json.has(NAME) || !(json.get(NAME) instanceof String name)) throw missingFieldException(NAME);
if (!json.has(CODE) || !(json.get(CODE) instanceof String code)) throw missingFieldException(CODE);
}
private boolean postLocation(UmbrellaUser user, HttpExchange ex) throws IOException {
var json = json(ex);
if (!(json.get(NAME) instanceof String name)) throw missingFieldException(NAME);

Loading…
Cancel
Save