|
|
|
@ -1,6 +1,6 @@ |
|
|
|
<script> |
|
|
|
<script> |
|
|
|
import { onMount } from 'svelte'; |
|
|
|
import { onMount } from 'svelte'; |
|
|
|
import { api } from '../../urls.svelte'; |
|
|
|
import { api, drop, get, patch } from '../../urls.svelte'; |
|
|
|
import { error, yikes } from '../../warn.svelte'; |
|
|
|
import { error, yikes } from '../../warn.svelte'; |
|
|
|
import { t } from '../../translations.svelte'; |
|
|
|
import { t } from '../../translations.svelte'; |
|
|
|
|
|
|
|
|
|
|
|
@ -17,10 +17,18 @@ |
|
|
|
let location = $state(null); |
|
|
|
let location = $state(null); |
|
|
|
let draggedItem = $state(null) |
|
|
|
let draggedItem = $state(null) |
|
|
|
let draggedLocation = $state(null) |
|
|
|
let draggedLocation = $state(null) |
|
|
|
|
|
|
|
let { item_id, location_id, owner, owner_id } = $props(); |
|
|
|
|
|
|
|
|
|
|
|
$effect(() => { |
|
|
|
$effect(() => { |
|
|
|
// This effect runs whenever `location` changes |
|
|
|
// This effect runs whenever `location` changes |
|
|
|
if (location !== null) item = null; |
|
|
|
if (location !== null) { |
|
|
|
|
|
|
|
item = null; |
|
|
|
|
|
|
|
setLocationUrl(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$effect(() => { |
|
|
|
|
|
|
|
if (item !== null) setItemUrl(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
let properties = $state(null); |
|
|
|
let properties = $state(null); |
|
|
|
let top_level = $state(null); |
|
|
|
let top_level = $state(null); |
|
|
|
@ -28,22 +36,13 @@ |
|
|
|
async function deleteLocation(loc){ |
|
|
|
async function deleteLocation(loc){ |
|
|
|
if (!confirm(t('confirm_delete',{element:loc.name}))) return; |
|
|
|
if (!confirm(t('confirm_delete',{element:loc.name}))) return; |
|
|
|
const url = api(`stock/location/${loc.id}`); |
|
|
|
const url = api(`stock/location/${loc.id}`); |
|
|
|
const res = await fetch(url,{ |
|
|
|
const res = await drop(url); |
|
|
|
credentials: 'include', |
|
|
|
|
|
|
|
method: 'DELETE', |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
if (res.ok){ |
|
|
|
if (res.ok){ |
|
|
|
yikes(); |
|
|
|
yikes(); |
|
|
|
unlistLocation(loc); |
|
|
|
unlistLocation(loc); |
|
|
|
} else error(res); |
|
|
|
} else error(res); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function unlistLocation(loc){ |
|
|
|
|
|
|
|
for (var owner of top_level){ |
|
|
|
|
|
|
|
if (owner.locations && dropNestedLocation(owner.locations,loc)) break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function drag_item(item){ |
|
|
|
function drag_item(item){ |
|
|
|
draggedLocation = null; |
|
|
|
draggedLocation = null; |
|
|
|
draggedItem = item; |
|
|
|
draggedItem = item; |
|
|
|
@ -68,11 +67,7 @@ |
|
|
|
async function move_dragged_to(new_loc){ |
|
|
|
async function move_dragged_to(new_loc){ |
|
|
|
const data = draggedItem ? { item : draggedItem.id, target: new_loc.id } : { parent_location_id: new_loc.id } |
|
|
|
const data = draggedItem ? { item : draggedItem.id, target: new_loc.id } : { parent_location_id: new_loc.id } |
|
|
|
const url = api(draggedItem ? 'stock/move_item' : `stock/location/${draggedLocation.id}`); |
|
|
|
const url = api(draggedItem ? 'stock/move_item' : `stock/location/${draggedLocation.id}`); |
|
|
|
const res = await fetch(url,{ |
|
|
|
const res = await patch(url,data); |
|
|
|
credentials : 'include', |
|
|
|
|
|
|
|
method : 'PATCH', |
|
|
|
|
|
|
|
body : JSON.stringify(data) |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
if (res.ok){ |
|
|
|
if (res.ok){ |
|
|
|
yikes(); |
|
|
|
yikes(); |
|
|
|
location = new_loc; |
|
|
|
location = new_loc; |
|
|
|
@ -86,8 +81,8 @@ |
|
|
|
|
|
|
|
|
|
|
|
async function loadLocation(){ |
|
|
|
async function loadLocation(){ |
|
|
|
if (!location) return null; |
|
|
|
if (!location) return null; |
|
|
|
const url = api(`stock/location/${location.id}`) |
|
|
|
const url = api(`stock/location/${location.id}`); |
|
|
|
const res = await fetch(url,{credentials:'include'}); |
|
|
|
const res = await get(url); |
|
|
|
if (res.ok){ |
|
|
|
if (res.ok){ |
|
|
|
yikes(); |
|
|
|
yikes(); |
|
|
|
return res.json(); |
|
|
|
return res.json(); |
|
|
|
@ -97,9 +92,24 @@ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function loadPath(){ |
|
|
|
|
|
|
|
if (!location_id) return; |
|
|
|
|
|
|
|
const url = api(`stock/location/${location_id}`); |
|
|
|
|
|
|
|
const res = await get(url); |
|
|
|
|
|
|
|
if (res.ok){ |
|
|
|
|
|
|
|
yikes(); |
|
|
|
|
|
|
|
var path = res.json(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
error(res); |
|
|
|
|
|
|
|
return null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
async function loadProperties(){ |
|
|
|
async function loadProperties(){ |
|
|
|
const url = api('stock/properties') |
|
|
|
const url = api('stock/properties') |
|
|
|
const res = await fetch(url,{credentials:'include'}); |
|
|
|
const res = await get(url); |
|
|
|
if (res.ok){ |
|
|
|
if (res.ok){ |
|
|
|
var json = await res.json(); |
|
|
|
var json = await res.json(); |
|
|
|
var dict = {} |
|
|
|
var dict = {} |
|
|
|
@ -112,7 +122,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
async function loadUserLocations(){ |
|
|
|
async function loadUserLocations(){ |
|
|
|
const url = api('stock/locations/of_user') |
|
|
|
const url = api('stock/locations/of_user') |
|
|
|
const res = await fetch(url,{credentials:'include'}); |
|
|
|
const res = await get(url); |
|
|
|
if (res.ok){ |
|
|
|
if (res.ok){ |
|
|
|
top_level = await res.json(); |
|
|
|
top_level = await res.json(); |
|
|
|
yikes(); |
|
|
|
yikes(); |
|
|
|
@ -121,6 +131,7 @@ |
|
|
|
|
|
|
|
|
|
|
|
function load(){ |
|
|
|
function load(){ |
|
|
|
loadUserLocations(); |
|
|
|
loadUserLocations(); |
|
|
|
|
|
|
|
loadPath(); |
|
|
|
loadProperties(); |
|
|
|
loadProperties(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -140,11 +151,7 @@ |
|
|
|
const data = {}; |
|
|
|
const data = {}; |
|
|
|
data[field] = newValue; |
|
|
|
data[field] = newValue; |
|
|
|
const url = api(`stock/location/${location.id}`); |
|
|
|
const url = api(`stock/location/${location.id}`); |
|
|
|
const res = await fetch(url,{ |
|
|
|
const res = await patch(url,data); |
|
|
|
credentials: 'include', |
|
|
|
|
|
|
|
method:'PATCH', |
|
|
|
|
|
|
|
body:JSON.stringify(data) |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
if (res.ok){ |
|
|
|
if (res.ok){ |
|
|
|
yikes(); |
|
|
|
yikes(); |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
@ -154,10 +161,34 @@ |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function setItemUrl(){ |
|
|
|
|
|
|
|
var owner = `/${item.owner.type}/${item.owner.id}` |
|
|
|
|
|
|
|
var code = `/item/${item.owner_number}` |
|
|
|
|
|
|
|
let url = window.location.origin + '/stock' + owner + code; |
|
|
|
|
|
|
|
window.history.replaceState(window.history.state, '', url); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function setLocationUrl(){ |
|
|
|
|
|
|
|
let url = window.location.origin + '/stock/location/' + location.id; |
|
|
|
|
|
|
|
window.history.replaceState(window.history.state, '', url); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function unlistLocation(loc){ |
|
|
|
|
|
|
|
for (var owner of top_level){ |
|
|
|
|
|
|
|
if (owner.locations && dropNestedLocation(owner.locations,loc)) break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
onMount(load); |
|
|
|
onMount(load); |
|
|
|
</script> |
|
|
|
</script> |
|
|
|
|
|
|
|
|
|
|
|
<h2>{t('Stock')}</h2> |
|
|
|
<h2>{t('Stock')}</h2> |
|
|
|
|
|
|
|
<span class="warn"> |
|
|
|
|
|
|
|
{#if location_id}location id: {location_id}{/if} |
|
|
|
|
|
|
|
{#if owner}owner type: {owner},{/if} |
|
|
|
|
|
|
|
{#if owner_id}owner id: {owner_id},{/if} |
|
|
|
|
|
|
|
{#if item_id}item id: {item_id}{/if} |
|
|
|
|
|
|
|
</span> |
|
|
|
<div class="grid3"> |
|
|
|
<div class="grid3"> |
|
|
|
<div class="locations"> |
|
|
|
<div class="locations"> |
|
|
|
{#if top_level} |
|
|
|
{#if top_level} |
|
|
|
|