Skip to content

Commit 0e8a8f2

Browse files
author
puranban
committed
Fix project draft topic save
1 parent 7be7d6d commit 0e8a8f2

File tree

2 files changed

+31
-62
lines changed

2 files changed

+31
-62
lines changed

manager-dashboard/app/views/NewProject/index.tsx

Lines changed: 30 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ import BasicProjectInfoForm from './BasicProjectInfoForm';
8585
// eslint-disable-next-line postcss-modules/no-unused-class
8686
import styles from './styles.css';
8787

88+
const PROJECT_ERROR_MESSAGE = 'A project with this name already exists, please use a different project name (Please note that the name comparison is not case sensitive)';
89+
8890
const defaultProjectFormValue: PartialProjectFormType = {
8991
// projectType: PROJECT_TYPE_BUILD_AREA,
9092
projectNumber: 1,
@@ -278,68 +280,7 @@ function NewProject(props: Props) {
278280
...valuesToCopy
279281
} = finalValues;
280282

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;
283+
const finalFilter = filter === FILTER_OTHERS ? filterText : filter;
343284

344285
if (valuesToCopy.projectType === PROJECT_TYPE_FOOTPRINT && valuesToCopy.inputType === 'aoi_file') {
345286
const res = await validateAoiOnOhsome(valuesToCopy.geometry, finalFilter);
@@ -389,6 +330,30 @@ function NewProject(props: Props) {
389330
}
390331

391332
const database = getDatabase();
333+
const projectTopicKey = projectTopic?.toLowerCase() as string;
334+
335+
const projectRef = databaseRef(database, 'v2/projects/');
336+
const prevProjectNameQuery = query(
337+
projectRef,
338+
orderByChild('projectTopicKey'),
339+
equalTo(projectTopicKey),
340+
);
341+
342+
const snapshot = await getValueFromFirebase(prevProjectNameQuery);
343+
if (!mountedRef.current) {
344+
return;
345+
}
346+
347+
if (snapshot.exists()) {
348+
setError((prevErr) => ({
349+
...getErrorObject(prevErr),
350+
[nonFieldError]: PROJECT_ERROR_MESSAGE,
351+
projectTopic: 'A project with this name already exists',
352+
}));
353+
setProjectSubmissionStatus(undefined);
354+
return;
355+
}
356+
392357
const projectDraftsRef = databaseRef(database, 'v2/projectDrafts/');
393358
const newProjectDraftsRef = await pushToDatabase(projectDraftsRef);
394359
if (!mountedRef.current) {
@@ -402,11 +367,14 @@ function NewProject(props: Props) {
402367

403368
const uploadData = {
404369
...valuesToCopy,
370+
projectTopic,
371+
projectTopicKey,
405372
filter: finalFilter,
406373
image: downloadUrl,
407374
createdBy: userId,
408375
teamId: visibility === 'public' ? null : visibility,
409376
};
377+
410378
await setToDatabase(newProjectRef, uploadData);
411379
if (!mountedRef.current) {
412380
return;

manager-dashboard/app/views/NewProject/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export type CustomOptionsForProject = {
5050
}[];
5151

5252
export interface ProjectFormType {
53+
projectTopicKey: string;
5354
projectTopic: string;
5455
projectType: ProjectType;
5556
projectRegion: string;

0 commit comments

Comments
 (0)