From 3385f1410ba4e97847756e190e9ca71f2e1b2ccb Mon Sep 17 00:00:00 2001 From: Nishit Suwal <81785002+NSUWAL123@users.noreply.github.com> Date: Thu, 23 Nov 2023 19:12:42 +0545 Subject: [PATCH] fix: post task split type, dimension, num buildings (#1003) * feat (enums): task_split_type enum added * fix (createNewProject): splitTasks - algorithm value replaced with enums & createnewproject definetaskvalidation imported * fix (createNewProject): defineTasksValidation - comparing value replaced with enums * fix (createNewProject): projectDetailsType - types renamed to task_split_type, task_split_dimension & task_num_buildings addeed * fix (createNewProject): splitTasks - adding params conditionally on the basis of splitTasksSelection * fix (createNewroject): condtion replaced with enum --- src/frontend/src/api/CreateProjectService.ts | 3 +- .../createnewproject/SplitTasks.tsx | 98 +++++++++++-------- .../validation/DefineTaskValidation.tsx | 25 +++-- .../src/store/types/ICreateProject.ts | 5 +- src/frontend/src/types/enums.ts | 5 + 5 files changed, 83 insertions(+), 53 deletions(-) create mode 100644 src/frontend/src/types/enums.ts diff --git a/src/frontend/src/api/CreateProjectService.ts b/src/frontend/src/api/CreateProjectService.ts index 3ece9b4030..5b8a24c485 100755 --- a/src/frontend/src/api/CreateProjectService.ts +++ b/src/frontend/src/api/CreateProjectService.ts @@ -7,6 +7,7 @@ import { } from '../models/createproject/createProjectModel'; import { CommonActions } from '../store/slices/CommonSlice'; import { ValidateCustomFormResponse } from 'store/types/ICreateProject'; +import { task_split_type } from '../types/enums'; const CreateProjectService: Function = ( url: string, @@ -26,7 +27,7 @@ const CreateProjectService: Function = ( const resp: ProjectDetailsModel = postNewProjectDetails.data; await dispatch(CreateProjectActions.PostProjectDetails(resp)); - if (payload.splitting_algorithm === 'choose_area_as_task') { + if (payload.task_split_type === task_split_type['choose_area_as_task']) { await dispatch( UploadAreaService(`${import.meta.env.VITE_API_URL}/projects/${resp.id}/upload_multi_polygon`, fileUpload), ); diff --git a/src/frontend/src/components/createnewproject/SplitTasks.tsx b/src/frontend/src/components/createnewproject/SplitTasks.tsx index de3060112b..380a90fb42 100644 --- a/src/frontend/src/components/createnewproject/SplitTasks.tsx +++ b/src/frontend/src/components/createnewproject/SplitTasks.tsx @@ -8,7 +8,7 @@ import { useNavigate } from 'react-router-dom'; import { CreateProjectActions } from '../../store/slices/CreateProjectSlice'; import CoreModules from '../../shared/CoreModules'; import useForm from '../../hooks/useForm'; -import DefineTaskValidation from '../../components/createproject/validation/DefineTaskValidation'; +import DefineTaskValidation from '../../components/createnewproject/validation/DefineTaskValidation'; import NewDefineAreaMap from '../../views/NewDefineAreaMap'; import { useAppSelector } from '../../types/reduxTypes'; import { @@ -21,11 +21,16 @@ import environment from '../../environment'; import LoadingBar from '../../components/createproject/LoadingBar'; import { Modal } from '../../components/common/Modal'; import ProgressBar from '../../components/common/ProgressBar'; +import { task_split_type } from '../../types/enums'; const alogrithmList = [ - { name: 'define_tasks', value: 'divide_on_square', label: 'Divide on square' }, - { name: 'define_tasks', value: 'choose_area_as_task', label: 'Choose area as task' }, - { name: 'define_tasks', value: 'task_splitting_algorithm', label: 'Task Splitting Algorithm' }, + { name: 'define_tasks', value: task_split_type['divide_on_square'].toString(), label: 'Divide on square' }, + { name: 'define_tasks', value: task_split_type['choose_area_as_task'].toString(), label: 'Choose area as task' }, + { + name: 'define_tasks', + value: task_split_type['task_splitting_algorithm'].toString(), + label: 'Task Splitting Algorithm', + }, ]; let generateProjectLogIntervalCb: any = null; @@ -75,34 +80,41 @@ const SplitTasks = ({ flag, geojsonFile, setGeojsonFile, customLineUpload, custo .split('#') .map((item) => item.trim()) .filter(Boolean); + + let projectData = { + project_info: { + name: projectDetails.name, + short_description: projectDetails.short_description, + description: projectDetails.description, + }, + author: { + username: userDetails.username, + id: userDetails.id, + }, + odk_central: { + odk_central_url: projectDetails.odk_central_url, + odk_central_user: projectDetails.odk_central_user, + odk_central_password: projectDetails.odk_central_password, + }, + // dont send xform_title if upload custom form is selected + xform_title: projectDetails.formCategorySelection, + task_split_type: splitTasksSelection, + form_ways: projectDetails.formWays, + // "uploaded_form": projectDetails.uploaded_form, + data_extractWays: projectDetails.data_extractWays, + hashtags: arrayHashtag, + organisation_id: projectDetails.organisation_id, + }; + if (splitTasksSelection === task_split_type['task_splitting_algorithm']) { + projectData = { ...projectData, task_num_buildings: projectDetails.average_buildings_per_task }; + } else { + projectData = { ...projectData, task_split_dimension: projectDetails.dimension }; + } + console.log(projectData, 'projectData'); dispatch( CreateProjectService( `${import.meta.env.VITE_API_URL}/projects/create_project`, - { - project_info: { - name: projectDetails.name, - short_description: projectDetails.short_description, - description: projectDetails.description, - }, - author: { - username: userDetails.username, - id: userDetails.id, - }, - odk_central: { - odk_central_url: projectDetails.odk_central_url, - odk_central_user: projectDetails.odk_central_user, - odk_central_password: projectDetails.odk_central_password, - }, - // dont send xform_title if upload custom form is selected - xform_title: projectDetails.formCategorySelection, - dimension: projectDetails.dimension, - splitting_algorithm: splitTasksSelection, - form_ways: projectDetails.formWays, - // "uploaded_form": projectDetails.uploaded_form, - data_extractWays: projectDetails.data_extractWays, - hashtags: arrayHashtag, - organisation_id: projectDetails.organisation_id, - }, + projectData, drawnGeojsonFile, customFormFile, customPolygonUpload, @@ -114,7 +126,7 @@ const SplitTasks = ({ flag, geojsonFile, setGeojsonFile, customLineUpload, custo }; useEffect(() => { - if (splitTasksSelection === 'choose_area_as_task') { + if (splitTasksSelection === task_split_type['choose_area_as_task']) { dispatch(CreateProjectActions.SetDividedTaskGeojson(null)); } }, [splitTasksSelection]); @@ -135,14 +147,14 @@ const SplitTasks = ({ flag, geojsonFile, setGeojsonFile, customLineUpload, custo // Create a file object from the Blob const drawnGeojsonFile = new File([blob], 'data.json', { type: 'application/json' }); - if (splitTasksSelection === 'divide_on_square') { + if (splitTasksSelection === task_split_type['divide_on_square']) { dispatch( GetDividedTaskFromGeojson(`${import.meta.env.VITE_API_URL}/projects/preview_tasks/`, { geojson: drawnGeojsonFile, dimension: formValues?.dimension, }), ); - } else if (splitTasksSelection === 'task_splitting_algorithm') { + } else if (splitTasksSelection === task_split_type['task_splitting_algorithm']) { // const a = document.createElement('a'); // a.href = URL.createObjectURL(drawnGeojsonFile); // a.download = 'test.json'; @@ -289,17 +301,17 @@ const SplitTasks = ({ flag, geojsonFile, setGeojsonFile, customLineUpload, custo
{ - handleCustomChange('splitTaskOption', value); - dispatch(CreateProjectActions.SetSplitTasksSelection(value)); + handleCustomChange('task_split_type', parseInt(value)); + dispatch(CreateProjectActions.SetSplitTasksSelection(parseInt(value))); }} - errorMsg={errors.splitTaskOption} + errorMsg={errors.task_split_type} /> - {splitTasksSelection === 'divide_on_square' && ( + {splitTasksSelection === task_split_type['divide_on_square'] && ( <>

Dimension of square in metres:

@@ -315,14 +327,14 @@ const SplitTasks = ({ flag, geojsonFile, setGeojsonFile, customLineUpload, custo )} )} - {splitTasksSelection === 'task_splitting_algorithm' && ( + {splitTasksSelection === task_split_type['task_splitting_algorithm'] && ( <>

Average number of buildings per task:

handleCustomChange('average_buildings_per_task', e.target.value)} + onChange={(e) => handleCustomChange('average_buildings_per_task', parseInt(e.target.value))} className="fmtm-outline-none fmtm-border-[1px] fmtm-border-gray-600 fmtm-h-7 fmtm-w-16 fmtm-px-2 " />
@@ -333,8 +345,8 @@ const SplitTasks = ({ flag, geojsonFile, setGeojsonFile, customLineUpload, custo )} )} - {(splitTasksSelection === 'divide_on_square' || - splitTasksSelection === 'task_splitting_algorithm') && ( + {(splitTasksSelection === task_split_type['divide_on_square'] || + splitTasksSelection === task_split_type['task_splitting_algorithm']) && (
)} - {splitTasksSelection && ( + {(splitTasksSelection === task_split_type['divide_on_square'] || + splitTasksSelection === task_split_type['task_splitting_algorithm'] || + splitTasksSelection === task_split_type['choose_area_as_task']) && (

Total number of task: {totalSteps}

diff --git a/src/frontend/src/components/createnewproject/validation/DefineTaskValidation.tsx b/src/frontend/src/components/createnewproject/validation/DefineTaskValidation.tsx index 83027d4c25..37f8191dab 100644 --- a/src/frontend/src/components/createnewproject/validation/DefineTaskValidation.tsx +++ b/src/frontend/src/components/createnewproject/validation/DefineTaskValidation.tsx @@ -1,26 +1,35 @@ +import { task_split_type } from '../../../types/enums'; + interface ProjectValues { - splitting_algorithm: string; + task_split_type: number; dimension: number; + average_buildings_per_task: number; } interface ValidationErrors { - splitting_algorithm?: string; + task_split_type?: string; dimension?: string; + average_buildings_per_task?: string; } function DefineTaskValidation(values: ProjectValues) { const errors: ValidationErrors = {}; - - if (!values?.splitting_algorithm) { - errors.splitting_algorithm = 'Splitting Algorithm is Required.'; + if ( + values?.task_split_type !== task_split_type['choose_area_as_task'] && + values?.task_split_type !== task_split_type['divide_on_square'] && + values?.task_split_type !== task_split_type['task_splitting_algorithm'] + ) { + errors.task_split_type = 'Splitting Algorithm is required.'; } - if (values?.splitting_algorithm === 'Divide on Square' && !values?.dimension) { + if (values?.task_split_type === task_split_type['divide_on_square'] && !values?.dimension) { errors.dimension = 'Dimension is Required.'; } - if (values?.splitting_algorithm === 'Divide on Square' && values?.dimension && values.dimension < 9) { + if (values?.task_split_type === task_split_type['divide_on_square'] && values?.dimension && values.dimension < 9) { errors.dimension = 'Dimension should be greater than 10 or equal to 10.'; } + if (values?.task_split_type === task_split_type['task_splitting_algorithm'] && !values?.average_buildings_per_task) { + errors.average_buildings_per_task = 'Average number of buildings per task is required.'; + } - console.log(errors); return errors; } diff --git a/src/frontend/src/store/types/ICreateProject.ts b/src/frontend/src/store/types/ICreateProject.ts index bc7147c66c..c0c1b10297 100644 --- a/src/frontend/src/store/types/ICreateProject.ts +++ b/src/frontend/src/store/types/ICreateProject.ts @@ -95,7 +95,8 @@ export type EditProjectDetailsTypes = { }; export type ProjectDetailsTypes = { - dimension: number; + task_split_dimension: number; + task_num_buildings: number; no_of_buildings: number; odk_central_user?: string; odk_central_password?: string; @@ -105,7 +106,7 @@ export type ProjectDetailsTypes = { hashtags?: string; short_description?: string; description?: string; - splitting_algorithm?: string; + task_split_type?: number; xform_title?: string; data_extract_options?: string; data_extractWays?: string; diff --git a/src/frontend/src/types/enums.ts b/src/frontend/src/types/enums.ts new file mode 100644 index 0000000000..76bd6aeb7a --- /dev/null +++ b/src/frontend/src/types/enums.ts @@ -0,0 +1,5 @@ +export enum task_split_type { + divide_on_square = 0, + choose_area_as_task = 1, + task_splitting_algorithm = 2, +}