@@ -4,7 +4,13 @@ import { useForm } from 'react-hook-form';
4
4
import toast from 'react-hot-toast' ;
5
5
import { BsArrowLeftRight } from 'react-icons/bs' ;
6
6
import { Link , useNavigate , useSearchParams } from 'react-router-dom' ;
7
- import { AddInventoryItemDto , OrganizationDto } from 'shared-types' ;
7
+ import {
8
+ AddInventoryItemDto ,
9
+ CreateWarehouseDto ,
10
+ CreateWarehouseInOrgDto ,
11
+ OrganizationDto ,
12
+ WarehouseDto ,
13
+ } from 'shared-types' ;
8
14
import useProductsDetails from '../hooks/useProductsDetails' ;
9
15
import { Utils } from '../utils' ;
10
16
import Button from './Button' ;
@@ -13,8 +19,8 @@ import FormError from './Form/FormError';
13
19
import FormInput from './Form/FormInput' ;
14
20
15
21
type Inputs = {
16
- quantity : number ;
17
- location : string ;
22
+ name : string ;
23
+ address : string ;
18
24
} ;
19
25
20
26
function InventoryAddForm ( ) {
@@ -23,78 +29,47 @@ function InventoryAddForm() {
23
29
const [ loading , setLoading ] = useState ( false ) ;
24
30
const [ error , setError ] = useState < string | null > ( null ) ;
25
31
const navigate = useNavigate ( ) ;
26
- const [ searchParams , setSearchParams ] = useSearchParams ( ) ;
27
-
28
- const productId = searchParams . get ( 'productId' ) ! ;
29
- const { product, error : productFetchError } = useProductsDetails ( productId ) ;
30
-
31
- useEffect ( ( ) => {
32
- if ( productId && productFetchError != undefined ) {
33
- toast . error ( 'Provided product ID is invalid, please input product manual' ) ;
34
- setSearchParams ( undefined ) ;
35
- }
36
- } , [ productFetchError , productId , setSearchParams ] ) ;
37
32
38
33
function onSubmit ( inputs : Inputs ) {
39
34
setLoading ( true ) ;
40
35
setError ( null ) ;
41
36
42
- const dto : AddInventoryItemDto = {
43
- warehouseId : appContext . currentWarehouse . id ,
44
- productId : product . id ,
45
- ...inputs ,
37
+ const dto : CreateWarehouseInOrgDto = {
38
+ organizationId : appContext . organization . id ,
39
+ warehouse : inputs ,
46
40
} ;
47
41
48
- Utils . postFetcher < OrganizationDto > ( `/api/inventory /` , dto )
42
+ Utils . postFetcher < WarehouseDto > ( `/api/organizations/warehouses /` , dto )
49
43
. then ( ( ) => navigate ( '..' ) )
50
- . then ( ( ) => toast . success ( 'Successfully created item ' ) )
44
+ . then ( ( ) => toast . success ( 'Successfully created warehouse ' ) )
51
45
. catch ( ( err ) => setError ( Utils . requestErrorToString ( err ) ) )
52
46
. finally ( ( ) => setLoading ( false ) ) ;
53
47
}
54
48
55
49
return (
56
50
< form onSubmit = { handleSubmit ( onSubmit ) } >
57
51
< FormInput
58
- label = "Warehouse "
52
+ label = "Organization "
59
53
readOnly
60
54
required
61
- value = { appContext . currentWarehouse . name }
55
+ value = { appContext . organization . name }
62
56
/>
63
57
64
58
< FormInput
65
- label = "Product"
66
- disabled
67
- value = { product ?. name }
68
- noEndMargin
59
+ label = "Name"
60
+ placeholder = "US West Main"
61
+ type = "number"
69
62
minLength = { 2 }
70
63
maxLength = { 32 }
71
- required
64
+ { ... register ( 'name' ) }
72
65
/>
73
- < Link
74
- className = { classNames ( 'link-primary mb-1 ms-1 mt-3 flex items-center gap-2' , {
75
- 'animate-bounce' : product == undefined ,
76
- } ) }
77
- to = {
78
- Utils . dashboardUrl ( appContext . organization . id , appContext . currentWarehouse . id ) +
79
- `/products`
80
- }
81
- >
82
- < BsArrowLeftRight /> { product ? 'Change' : 'Select' } product
83
- </ Link >
84
66
85
67
< FormInput
86
- label = "Quantity"
87
- hint = "If you know current item quantity you can add it here"
88
- placeholder = "0"
89
- type = "number"
90
- { ...register ( 'quantity' , { setValueAs : ( v ) => ( v == null ? 0 : + v ) } ) }
91
- />
92
-
93
- < FormInput
94
- label = "Location"
95
- hint = "Where is this item located in warehouse"
96
- placeholder = "shelf / aisle / bin number"
97
- { ...register ( 'location' ) }
68
+ label = "Address"
69
+ placeholder = "18 Milton Street"
70
+ minLength = { 2 }
71
+ maxLength = { 32 }
72
+ { ...register ( 'address' ) }
98
73
/>
99
74
100
75
< FormError > { error } </ FormError >
@@ -103,9 +78,8 @@ function InventoryAddForm() {
103
78
role = "submit"
104
79
className = "mt-4"
105
80
loading = { loading }
106
- disabled = { product == undefined }
107
81
>
108
- Add product to inventory
82
+ Create warehouse
109
83
</ Button >
110
84
</ form >
111
85
) ;
0 commit comments