@@ -22,6 +22,9 @@ import {
2222 ref as databaseRef ,
2323 push as pushToDatabase ,
2424 set as setToDatabase ,
25+ query ,
26+ orderByChild ,
27+ equalTo ,
2528} from 'firebase/database' ;
2629import {
2730 MdOutlinePublishedWithChanges ,
@@ -30,6 +33,7 @@ import {
3033import { Link } from 'react-router-dom' ;
3134
3235import UserContext from '#base/context/UserContext' ;
36+ import projectTypeOptions from '#base/configs/projectTypes' ;
3337import useMountedRef from '#hooks/useMountedRef' ;
3438import Modal from '#components/Modal' ;
3539import TextInput from '#components/TextInput' ;
@@ -56,6 +60,7 @@ import {
5660 PROJECT_TYPE_COMPLETENESS ,
5761 PROJECT_TYPE_CHANGE_DETECTION ,
5862} from '#utils/common' ;
63+ import { getValueFromFirebase } from '#utils/firebase' ;
5964
6065import CustomOptionInput from '#views/NewTutorial/CustomOptionInput' ;
6166import CustomOptionPreview from '#views/NewTutorial/CustomOptionInput/CustomOptionPreview' ;
@@ -80,8 +85,6 @@ import BasicProjectInfoForm from './BasicProjectInfoForm';
8085// eslint-disable-next-line postcss-modules/no-unused-class
8186import styles from './styles.css' ;
8287
83- import projectTypeOptions from '#base/configs/projectTypes' ;
84-
8588const defaultProjectFormValue : PartialProjectFormType = {
8689 // projectType: PROJECT_TYPE_BUILD_AREA,
8790 projectNumber : 1 ,
@@ -271,9 +274,69 @@ function NewProject(props: Props) {
271274 visibility,
272275 filter,
273276 filterText,
277+ projectTopic,
274278 ...valuesToCopy
275279 } = finalValues ;
276280
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+
277340 const finalFilter = filter === FILTER_OTHERS
278341 ? filterText
279342 : filter ;
@@ -654,7 +717,12 @@ function NewProject(props: Props) {
654717 />
655718 </ InputSection >
656719 ) }
657- { hasErrors && (
720+ { error ?. [ nonFieldError ] && (
721+ < div className = { styles . errorMessage } >
722+ { error ?. [ nonFieldError ] }
723+ </ div >
724+ ) }
725+ { ! nonFieldError && hasErrors && (
658726 < div className = { styles . errorMessage } >
659727 Please correct all the errors above before submission!
660728 </ div >
0 commit comments