@@ -61,6 +61,27 @@ const NewRequest = ({ session }) => {
61
61
const [ buttonDisabled , setButtonDisabled ] = useState ( false )
62
62
const [ formSubmitting , setFormSubmitting ] = useState ( false )
63
63
64
+ useEffect ( ( ) => {
65
+ if ( createdRequestUUID ) {
66
+ router . push ( {
67
+ pathname : `/requests/${ createdRequestUUID } ` ,
68
+ query : { createRequestError : JSON . stringify ( createRequestError ) } ,
69
+ } , `/requests/${ createdRequestUUID } ` )
70
+ }
71
+ // eslint-disable-next-line react-hooks/exhaustive-deps
72
+ } , [ createdRequestUUID , createRequestError ] )
73
+
74
+ /**
75
+ * checking for the presence of a session has to come after all of the hooks so we don't violate the react-hooks/rules-of-hooks
76
+ * rule. ref: https://legacy.reactjs.org/docs/hooks-rules.html#only-call-hooks-at-the-top-level
77
+ * we return the loading state in two different locations because it's rendered based on separate conditions. when
78
+ * isLoading is true because we don't have a user, it doesn't ever become false. that's why we were previously returning
79
+ * the loading spinner indefinitely.
80
+ * using a guard clause with an early return inside the api methods also violates the react-hooks/rules-of-hooks rule.
81
+ */
82
+ if ( session === undefined ) return pageLoading
83
+ if ( session === null ) return unauthorizedUser
84
+
64
85
/**
65
86
* @param {object } event onChange event
66
87
* @param {string } property dot notated string representing the property in initialValue
@@ -118,32 +139,8 @@ const NewRequest = ({ session }) => {
118
139
setCreatedRequestUUID ( data . uuid )
119
140
}
120
141
121
- useEffect ( ( ) => {
122
- if ( createdRequestUUID ) {
123
- router . push ( {
124
- pathname : `/requests/${ createdRequestUUID } ` ,
125
- query : { createRequestError : JSON . stringify ( createRequestError ) } ,
126
- } , `/requests/${ createdRequestUUID } ` )
127
- }
128
- // eslint-disable-next-line react-hooks/exhaustive-deps
129
- } , [ createdRequestUUID , createRequestError ] )
130
-
131
142
// TODO(alishaevn): use react bs placeholder component
132
- if ( isLoadingInitialRequest || ! wareID || formSubmitting ) return < Loading wrapperClass = 'item-page mt-5' />
133
-
134
- if ( ! session ) {
135
- return (
136
- < Notice
137
- addClass = 'my-5'
138
- alert = { {
139
- body : [ 'Please log in to make new requests.' ] ,
140
- title : 'Unauthorized' ,
141
- variant : 'info'
142
- } }
143
- dismissible = { false }
144
- />
145
- )
146
- }
143
+ if ( isLoadingInitialRequest || ! wareID || formSubmitting ) return pageLoading
147
144
148
145
if ( isInitialRequestError ) {
149
146
return (
@@ -242,4 +239,18 @@ const StandardRequestOptions = ({ buttonDisabled, defaultRequiredDate, requestFo
242
239
)
243
240
}
244
241
242
+ const pageLoading = < Loading wrapperClass = 'item-page mt-5' />
243
+
244
+ const unauthorizedUser = (
245
+ < Notice
246
+ addClass = 'my-5'
247
+ alert = { {
248
+ body : [ 'Please log in to make new requests.' ] ,
249
+ title : 'Unauthorized' ,
250
+ variant : 'info'
251
+ } }
252
+ dismissible = { false }
253
+ />
254
+ )
255
+
245
256
export default NewRequest
0 commit comments