@@ -59,6 +59,7 @@ import {
5959 PROJECT_TYPE_FOOTPRINT ,
6060 PROJECT_TYPE_COMPLETENESS ,
6161 PROJECT_TYPE_CHANGE_DETECTION ,
62+ formatProjectTopic ,
6263} from '#utils/common' ;
6364import { getValueFromFirebase } from '#utils/firebase' ;
6465
@@ -278,68 +279,7 @@ function NewProject(props: Props) {
278279 ...valuesToCopy
279280 } = finalValues ;
280281
281- try {
282- const db = getDatabase ( ) ;
283- const projectRef = databaseRef ( db , 'v2/projects/' ) ;
284- const projectTopicKey = projectTopic ?. toLowerCase ( ) as string ;
285-
286- const prevProjectNameQuery = query (
287- projectRef ,
288- orderByChild ( 'projectTopicKey' ) ,
289- equalTo ( projectTopicKey ) ,
290- ) ;
291-
292- const snapshot = await getValueFromFirebase ( prevProjectNameQuery ) ;
293-
294- if ( snapshot . exists ( ) ) {
295- setError ( ( prevErr ) => ( {
296- ...getErrorObject ( prevErr ) ,
297- [ nonFieldError ] : 'A project with this name already exists, please use a different project name (Please note that the name comparision is not case sensitive)' ,
298- projectTopic : 'A project with this name already exists, please use a different project name (Please note that the name comparision is not case sensitive)' ,
299- } ) ) ;
300- setProjectSubmissionStatus ( undefined ) ;
301- return ;
302- }
303-
304- const newProjectRef = await pushToDatabase ( projectRef ) ;
305- const newKey = newProjectRef . key ;
306-
307- if ( ! mountedRef . current ) {
308- return ;
309- }
310-
311- if ( ! newKey ) {
312- setError ( ( err ) => ( {
313- ...getErrorObject ( err ) ,
314- [ nonFieldError ] : 'Failed to push new key for the project' ,
315- } ) ) ;
316- setProjectSubmissionStatus ( 'failed' ) ;
317- return ;
318- }
319-
320- const uploadData = {
321- ...finalValues ,
322- projectTopicKey,
323- createdAt : ( new Date ( ) ) . getTime ( ) ,
324- } ;
325-
326- const putProjectRef = databaseRef ( db , `v2/projects/${ newKey } ` ) ;
327- await setToDatabase ( putProjectRef , uploadData ) ;
328- } catch ( submissionError : unknown ) {
329- if ( ! mountedRef . current ) {
330- return ;
331- }
332- // eslint-disable-next-line no-console
333- console . error ( submissionError ) ;
334- setError ( ( err ) => ( {
335- ...getErrorObject ( err ) ,
336- [ nonFieldError ] : 'Some error occurred' ,
337- } ) ) ;
338- }
339-
340- const finalFilter = filter === FILTER_OTHERS
341- ? filterText
342- : filter ;
282+ const finalFilter = filter === FILTER_OTHERS ? filterText : filter ;
343283
344284 if ( valuesToCopy . projectType === PROJECT_TYPE_FOOTPRINT && valuesToCopy . inputType === 'aoi_file' ) {
345285 const res = await validateAoiOnOhsome ( valuesToCopy . geometry , finalFilter ) ;
@@ -388,7 +328,34 @@ function NewProject(props: Props) {
388328 return ;
389329 }
390330
331+ // NOTE: All the user don't have permission to access draft project
332+ // FIXME: The firebase rules need to be changed to perform this on draft project
391333 const database = getDatabase ( ) ;
334+ const projectTopicKeyLowercase = ( projectTopic ?. trim ( ) ) ?. toLowerCase ( ) as string ;
335+ const projectTopicKey = formatProjectTopic ( projectTopicKeyLowercase ) ;
336+ const projectRef = databaseRef ( database , 'v2/projects/' ) ;
337+
338+ const prevProjectNameQuery = query (
339+ projectRef ,
340+ orderByChild ( 'projectTopicKey' ) ,
341+ equalTo ( projectTopicKey ) ,
342+ ) ;
343+
344+ const snapshot = await getValueFromFirebase ( prevProjectNameQuery ) ;
345+ if ( ! mountedRef . current ) {
346+ return ;
347+ }
348+
349+ if ( snapshot . exists ( ) ) {
350+ setError ( ( prevErr ) => ( {
351+ ...getErrorObject ( prevErr ) ,
352+ [ nonFieldError ] : 'A project with this name already exists, please use a different project name (Please note that the name comparison is not case sensitive)' ,
353+ projectTopic : 'A project with this name already exists' ,
354+ } ) ) ;
355+ setProjectSubmissionStatus ( undefined ) ;
356+ return ;
357+ }
358+
392359 const projectDraftsRef = databaseRef ( database , 'v2/projectDrafts/' ) ;
393360 const newProjectDraftsRef = await pushToDatabase ( projectDraftsRef ) ;
394361 if ( ! mountedRef . current ) {
@@ -402,11 +369,14 @@ function NewProject(props: Props) {
402369
403370 const uploadData = {
404371 ...valuesToCopy ,
372+ projectTopic,
373+ projectTopicKey,
405374 filter : finalFilter ,
406375 image : downloadUrl ,
407376 createdBy : userId ,
408377 teamId : visibility === 'public' ? null : visibility ,
409378 } ;
379+
410380 await setToDatabase ( newProjectRef , uploadData ) ;
411381 if ( ! mountedRef . current ) {
412382 return ;
0 commit comments