1
1
import {
2
2
type AsError ,
3
3
type ProductDetail ,
4
+ ProductDetailFlavour ,
5
+ ProductDetailType ,
4
6
type UnitDetail ,
7
+ UnitDetailDefaultProductPrivacy ,
5
8
type UnitProductPostBodyBodyFlavour ,
6
9
} from "@squonk/account-server-client" ;
7
10
import {
@@ -15,32 +18,36 @@ import { getGetUserInventoryQueryKey } from "@squonk/data-manager-client/invento
15
18
import { getGetProjectsQueryKey , useCreateProject } from "@squonk/data-manager-client/project" ;
16
19
17
20
import {
18
- Box ,
19
21
Button ,
20
- FormControlLabel ,
22
+ FormControl ,
23
+ FormLabel ,
21
24
MenuItem ,
25
+ Tooltip ,
22
26
Typography ,
23
27
useMediaQuery ,
24
28
useTheme ,
25
29
} from "@mui/material" ;
26
30
import { useQueryClient } from "@tanstack/react-query" ;
27
31
import { Field , Form , Formik , type FormikConfig } from "formik" ;
28
- import { Checkbox , TextField } from "formik-mui" ;
32
+ import { TextField } from "formik-mui" ;
29
33
import * as yup from "yup" ;
30
34
31
- import { DEFAULT_PRODUCT_FLAVOUR , PROJECT_SUB } from "../../constants/products" ;
32
- import { useCurrentProjectId } from "../../hooks/projectHooks" ;
33
- import { useEnqueueError } from "../../hooks/useEnqueueStackError" ;
34
- import { type Resolve } from "../../types" ;
35
- import { formatTierString } from "../../utils/app/products" ;
36
- import { getErrorMessage } from "../../utils/next/orvalError" ;
37
- import { FormikModalWrapper , type FormikModalWrapperProps } from "../modals/FormikModalWrapper" ;
35
+ import { useCurrentProjectId } from "../../../hooks/projectHooks" ;
36
+ import { useEnqueueError } from "../../../hooks/useEnqueueStackError" ;
37
+ import { type Resolve } from "../../../types" ;
38
+ import { formatTierString } from "../../../utils/app/products" ;
39
+ import { getErrorMessage } from "../../../utils/next/orvalError" ;
40
+ import { FormikModalWrapper , type FormikModalWrapperProps } from "../../modals/FormikModalWrapper" ;
41
+ import { PrivacySwitch } from "./PrivacySwitch" ;
42
+
43
+ const PROJECT_SUB = ProductDetailType . DATA_MANAGER_PROJECT_TIER_SUBSCRIPTION ;
38
44
39
45
export interface CreateProjectFormProps {
40
46
modal ?: Resolve <
41
47
Pick < FormikModalWrapperProps , "id" | "onClose" | "open" | "submitText" | "title" >
42
48
> ;
43
49
unitId : UnitDetail [ "id" ] | ( ( ) => Promise < UnitDetail [ "id" ] > ) ;
50
+ defaultPrivacy : UnitDetailDefaultProductPrivacy ;
44
51
product ?: ProductDetail ;
45
52
autoFocus ?: boolean ;
46
53
}
@@ -53,16 +60,32 @@ export interface Values {
53
60
54
61
type ProjectFormikProps = FormikConfig < Values > ;
55
62
63
+ const isPrivateDefaultValues : Record < UnitDetailDefaultProductPrivacy , boolean > = {
64
+ ALWAYS_PRIVATE : true ,
65
+ ALWAYS_PUBLIC : false ,
66
+ DEFAULT_PUBLIC : false ,
67
+ DEFAULT_PRIVATE : true ,
68
+ } ;
69
+
56
70
export const CreateProjectForm = ( {
57
71
modal,
58
72
unitId,
73
+ defaultPrivacy,
59
74
product,
60
75
autoFocus = true ,
61
76
} : CreateProjectFormProps ) => {
77
+ const evaluationAllowed = defaultPrivacy !== UnitDetailDefaultProductPrivacy . ALWAYS_PRIVATE ;
78
+ const defaultFlavour = (
79
+ defaultPrivacy === UnitDetailDefaultProductPrivacy . ALWAYS_PRIVATE ||
80
+ defaultPrivacy === UnitDetailDefaultProductPrivacy . DEFAULT_PRIVATE
81
+ ? ProductDetailFlavour . BRONZE
82
+ : ProductDetailFlavour . EVALUATION
83
+ ) satisfies ProductDetailFlavour ;
84
+
62
85
const initialValues : Values = {
63
86
projectName : "" ,
64
- flavour : product ?. flavour ?? DEFAULT_PRODUCT_FLAVOUR ,
65
- isPrivate : false ,
87
+ flavour : product ?. flavour ?? defaultFlavour ,
88
+ isPrivate : isPrivateDefaultValues [ defaultPrivacy ] ,
66
89
} ;
67
90
const theme = useTheme ( ) ;
68
91
const biggerThanSm = useMediaQuery ( theme . breakpoints . up ( "sm" ) ) ;
@@ -142,13 +165,19 @@ export const CreateProjectForm = ({
142
165
values,
143
166
} ) => (
144
167
< Form style = { { marginTop : theme . spacing ( ) } } >
145
- < Box
168
+ < FormControl
169
+ component = "fieldset"
146
170
sx = { {
147
171
display : "grid" ,
148
- gridTemplateColumns : biggerThanSm ? "1fr 1fr auto auto" : "1fr" ,
172
+ gridTemplateColumns : biggerThanSm ? "1fr 1fr auto" + ( modal ? "" : " auto") : "1fr" ,
149
173
gap : 1 ,
174
+ alignItems : "baseline" ,
150
175
} }
151
176
>
177
+ < FormLabel component = "legend" sx = { { mb : 1 } } >
178
+ Unit default privacy: { defaultPrivacy . split ( "_" ) . join ( " " ) . toLowerCase ( ) }
179
+ </ FormLabel >
180
+
152
181
< Field
153
182
fullWidth
154
183
autoFocus = { autoFocus }
@@ -169,7 +198,7 @@ export const CreateProjectForm = ({
169
198
name = "flavour"
170
199
onChange = { ( event : any ) => {
171
200
handleChange ( event ) ;
172
- if ( event . target . value === DEFAULT_PRODUCT_FLAVOUR ) {
201
+ if ( event . target . value === ProductDetailFlavour . EVALUATION ) {
173
202
void setFieldValue ( "isPrivate" , false ) ;
174
203
}
175
204
} }
@@ -179,7 +208,13 @@ export const CreateProjectForm = ({
179
208
) : (
180
209
productTypes ?. map ( ( product ) => {
181
210
return (
182
- < MenuItem key = { product . flavour } value = { product . flavour } >
211
+ < MenuItem
212
+ disabled = {
213
+ product . flavour === ProductDetailFlavour . EVALUATION && ! evaluationAllowed
214
+ }
215
+ key = { product . flavour }
216
+ value = { product . flavour }
217
+ >
183
218
{ formatTierString ( product . flavour ?? "Unknown Flavour" ) }
184
219
</ MenuItem >
185
220
) ;
@@ -188,19 +223,16 @@ export const CreateProjectForm = ({
188
223
</ Field >
189
224
) }
190
225
191
- < FormControlLabel
192
- control = { < Field color = "primary" component = { Checkbox } name = "isPrivate" type = "checkbox" /> }
193
- disabled = { values . flavour === DEFAULT_PRODUCT_FLAVOUR }
194
- label = "Private"
195
- labelPlacement = "start"
196
- />
226
+ < Tooltip title = "Toggle whether this project can be viewed by other platform users" >
227
+ < PrivacySwitch defaultPrivacy = { defaultPrivacy } flavour = { values . flavour } />
228
+ </ Tooltip >
197
229
198
230
{ ! modal && (
199
231
< Button disabled = { isSubmitting || ! isValid } onClick = { ( ) => void submitForm ( ) } >
200
232
Create
201
233
</ Button >
202
234
) }
203
- </ Box >
235
+ </ FormControl >
204
236
</ Form >
205
237
) ;
206
238
0 commit comments