From 64d646b4c4c6699aaa4a2edd812e0ef96c6be3ee Mon Sep 17 00:00:00 2001 From: Nishit Suwal <81785002+NSUWAL123@users.noreply.github.com> Date: Mon, 6 Nov 2023 20:12:23 +0545 Subject: [PATCH] Fix tasks api replacement (#973) * fix (projectById): project/:id api splitted where now taskList is fetched from tasks/task-list/:id * fix (projectById): dispatch - dispatch projectById parameters changed * fix (qrCodeComponent): qrcode - fetch qrcode from /tasks/task-list/ api * code cleanup: comments removed --- src/frontend/src/api/Files.js | 5 +- src/frontend/src/api/Project.js | 48 ++++++++++--------- .../src/components/QrcodeComponent.jsx | 3 +- src/frontend/src/views/NewProjectDetails.jsx | 19 +------- src/frontend/src/views/ProjectDetails.jsx | 18 +------ src/frontend/src/views/ProjectInfo.tsx | 18 +------ src/frontend/src/views/Submissions.tsx | 18 +------ src/frontend/src/views/Tasks.tsx | 18 +------ 8 files changed, 40 insertions(+), 107 deletions(-) diff --git a/src/frontend/src/api/Files.js b/src/frontend/src/api/Files.js index 4abf293525..554c8c9c59 100755 --- a/src/frontend/src/api/Files.js +++ b/src/frontend/src/api/Files.js @@ -13,8 +13,8 @@ export const ProjectFilesById = (url, taskId) => { cancelToken: source.token, }); const resp = fileJson.data; - const taskIndex = resp.project_tasks.findIndex((task) => task.id == taskId); - const getQrcodeByIndex = resp.project_tasks[taskIndex].qr_code_base64; + const taskIndex = resp.findIndex((task) => task.id == taskId); + const getQrcodeByIndex = resp[taskIndex].qr_code_base64; setQrcode(getQrcodeByIndex); setLoading(false); } catch (error) { @@ -34,6 +34,5 @@ export const ProjectFilesById = (url, taskId) => { return cleanUp; }, [taskId]); - return { loading, qrcode }; }; diff --git a/src/frontend/src/api/Project.js b/src/frontend/src/api/Project.js index 302f2c17e3..d87aca84cd 100755 --- a/src/frontend/src/api/Project.js +++ b/src/frontend/src/api/Project.js @@ -1,17 +1,21 @@ import { ProjectActions } from '../store/slices/ProjectSlice'; import CoreModules from '../shared/CoreModules'; import environment from '../environment'; -export const ProjectById = (url, existingProjectList, projectId) => { + +export const ProjectById = (existingProjectList, projectId) => { return async (dispatch) => { - // dispatch(HomeActions.HomeProjectLoading(true)) - const fetchProjectById = async (url, existingProjectList) => { + const fetchProjectById = async (projectId, existingProjectList) => { try { - const project = await CoreModules.axios.get(url); + const project = await CoreModules.axios.get(`${import.meta.env.VITE_API_URL}/projects/${projectId}`); + const taskList = await CoreModules.axios.get( + `${import.meta.env.VITE_API_URL}/tasks/task-list?project_id=${projectId}`, + ); const taskBbox = await CoreModules.axios.get( `${import.meta.env.VITE_API_URL}/tasks/point_on_surface?project_id=${projectId}`, ); - const resp = project.data; - const persistingValues = resp.project_tasks.map((data) => { + const projectResp = project.data; + const taskListResp = taskList.data; + const persistingValues = taskListResp.map((data) => { return { id: data.id, project_task_name: data.project_task_name, @@ -24,7 +28,7 @@ export const ProjectById = (url, existingProjectList, projectId) => { }; }); // added centroid from another api to projecttaskboundries - const projectTaskBoundries = [{ id: resp.id, taskBoundries: persistingValues }]; + const projectTaskBoundries = [{ id: projectResp.id, taskBoundries: persistingValues }]; const mergedBboxIntoTask = projectTaskBoundries[0].taskBoundries.map((projectTask) => { const filteredTaskIdCentroid = taskBbox.data.find((task) => task.id === projectTask.id).point[0]; return { @@ -37,20 +41,20 @@ export const ProjectById = (url, existingProjectList, projectId) => { ); dispatch( ProjectActions.SetProjectInfo({ - id: resp.id, - outline_geojson: resp.outline_geojson, - priority: resp.priority || 2, - priority_str: resp.priority_str || 'MEDIUM', - title: resp.project_info?.[0]?.name, - location_str: resp.location_str, - description: resp.project_info[0]?.description, - short_description: resp.project_info[0]?.short_description, - num_contributors: resp.num_contributors, - total_tasks: resp.total_tasks, - tasks_mapped: resp.tasks_mapped, - tasks_validated: resp.tasks_validated, - xform_title: resp.xform_title, - tasks_bad: resp.tasks_bad, + id: projectResp.id, + outline_geojson: projectResp.outline_geojson, + priority: projectResp.priority || 2, + priority_str: projectResp.priority_str || 'MEDIUM', + title: projectResp.project_info?.[0]?.name, + location_str: projectResp.location_str, + description: projectResp.project_info[0]?.description, + short_description: projectResp.project_info[0]?.short_description, + num_contributors: projectResp.num_contributors, + total_tasks: projectResp.total_tasks, + tasks_mapped: projectResp.tasks_mapped, + tasks_validated: projectResp.tasks_validated, + xform_title: projectResp.xform_title, + tasks_bad: projectResp.tasks_bad, }), ); } catch (error) { @@ -58,7 +62,7 @@ export const ProjectById = (url, existingProjectList, projectId) => { } }; - await fetchProjectById(url, existingProjectList); + await fetchProjectById(projectId, existingProjectList); dispatch(ProjectActions.SetNewProjectTrigger()); }; }; diff --git a/src/frontend/src/components/QrcodeComponent.jsx b/src/frontend/src/components/QrcodeComponent.jsx index 55bd8275f2..ca143fccef 100755 --- a/src/frontend/src/components/QrcodeComponent.jsx +++ b/src/frontend/src/components/QrcodeComponent.jsx @@ -11,8 +11,9 @@ const TasksComponent = ({ type, task, defaultTheme }) => { const dispatch = CoreModules.useAppDispatch(); const [open, setOpen] = useState(false); const params = CoreModules.useParams(); + const { loading, qrcode } = ProjectFilesById( - `${import.meta.env.VITE_API_URL}/projects/${environment.decode(params.id)}`, + `${import.meta.env.VITE_API_URL}/tasks/task-list?project_id=${environment.decode(params.id)}`, task, ); diff --git a/src/frontend/src/views/NewProjectDetails.jsx b/src/frontend/src/views/NewProjectDetails.jsx index 35721a72d8..44583a94a0 100644 --- a/src/frontend/src/views/NewProjectDetails.jsx +++ b/src/frontend/src/views/NewProjectDetails.jsx @@ -77,26 +77,11 @@ const Home = () => { dispatch(ProjectActions.SetNewProjectTrigger()); if (state.projectTaskBoundries.findIndex((project) => project.id == environment.decode(encodedId)) == -1) { dispatch(ProjectActions.SetProjectTaskBoundries([])); - - dispatch( - ProjectById( - `${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedId)}`, - state.projectTaskBoundries, - environment.decode(encodedId), - ), - state.projectTaskBoundries, - ); + dispatch(ProjectById(state.projectTaskBoundries, environment.decode(encodedId))); // dispatch(ProjectBuildingGeojsonService(`${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedId)}/features`)) } else { dispatch(ProjectActions.SetProjectTaskBoundries([])); - dispatch( - ProjectById( - `${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedId)}`, - state.projectTaskBoundries, - environment.decode(encodedId), - ), - state.projectTaskBoundries, - ); + dispatch(ProjectById(state.projectTaskBoundries, environment.decode(encodedId))); } if (Object.keys(state.projectInfo)?.length == 0) { dispatch(ProjectActions.SetProjectInfo(projectInfo)); diff --git a/src/frontend/src/views/ProjectDetails.jsx b/src/frontend/src/views/ProjectDetails.jsx index cf9bbcc999..b809df7c31 100755 --- a/src/frontend/src/views/ProjectDetails.jsx +++ b/src/frontend/src/views/ProjectDetails.jsx @@ -86,26 +86,12 @@ const Home = () => { dispatch(ProjectActions.SetNewProjectTrigger()); if (state.projectTaskBoundries.findIndex((project) => project.id == environment.decode(encodedId)) == -1) { dispatch(ProjectActions.SetProjectTaskBoundries([])); + dispatch(ProjectById(state.projectTaskBoundries, environment.decode(encodedId))); - dispatch( - ProjectById( - `${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedId)}`, - state.projectTaskBoundries, - environment.decode(encodedId), - ), - state.projectTaskBoundries, - ); // dispatch(ProjectBuildingGeojsonService(`${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedId)}/features`)) } else { dispatch(ProjectActions.SetProjectTaskBoundries([])); - dispatch( - ProjectById( - `${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedId)}`, - state.projectTaskBoundries, - environment.decode(encodedId), - ), - state.projectTaskBoundries, - ); + dispatch(ProjectById(state.projectTaskBoundries, environment.decode(encodedId))); } if (Object.keys(state.projectInfo).length == 0) { dispatch(ProjectActions.SetProjectInfo(projectInfo)); diff --git a/src/frontend/src/views/ProjectInfo.tsx b/src/frontend/src/views/ProjectInfo.tsx index 8d3732f1d5..d1f1c51ff3 100644 --- a/src/frontend/src/views/ProjectInfo.tsx +++ b/src/frontend/src/views/ProjectInfo.tsx @@ -60,26 +60,12 @@ const ProjectInfo = () => { dispatch(ProjectActions.SetNewProjectTrigger()); if (state.projectTaskBoundries.findIndex((project) => project.id == environment.decode(encodedId)) == -1) { dispatch(ProjectActions.SetProjectTaskBoundries([])); + dispatch(ProjectById(state.projectTaskBoundries, environment.decode(encodedId))); - dispatch( - ProjectById( - `${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedId)}`, - state.projectTaskBoundries, - environment.decode(encodedId), - ), - state.projectTaskBoundries, - ); // dispatch(ProjectBuildingGeojsonService(`${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedId)}/features`)) } else { dispatch(ProjectActions.SetProjectTaskBoundries([])); - dispatch( - ProjectById( - `${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedId)}`, - state.projectTaskBoundries, - environment.decode(encodedId), - ), - state.projectTaskBoundries, - ); + dispatch(ProjectById(state.projectTaskBoundries, environment.decode(encodedId))); } if (Object.keys(state.projectInfo).length == 0) { dispatch(ProjectActions.SetProjectInfo(projectInfo)); diff --git a/src/frontend/src/views/Submissions.tsx b/src/frontend/src/views/Submissions.tsx index 9c19c52990..102c6d5d4b 100755 --- a/src/frontend/src/views/Submissions.tsx +++ b/src/frontend/src/views/Submissions.tsx @@ -31,24 +31,10 @@ const Submissions = () => { // Requesting Task Boundaries on Page Load useEffect(() => { if (state.projectTaskBoundries.findIndex((project) => project.id == environment.decode(encodedId)) == -1) { - dispatch( - ProjectById( - `${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedId)}`, - state.projectTaskBoundries, - environment.decode(encodedId), - ), - state.projectTaskBoundries, - ); + dispatch(ProjectById(state.projectTaskBoundries, environment.decode(encodedId))); } else { dispatch(ProjectActions.SetProjectTaskBoundries([])); - dispatch( - ProjectById( - `${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedId)}`, - state.projectTaskBoundries, - environment.decode(encodedId), - ), - state.projectTaskBoundries, - ); + dispatch(ProjectById(state.projectTaskBoundries, environment.decode(encodedId))); } if (Object.keys(state.projectInfo).length == 0) { dispatch(ProjectActions.SetProjectInfo(projectInfo)); diff --git a/src/frontend/src/views/Tasks.tsx b/src/frontend/src/views/Tasks.tsx index 92ffe17ff6..7188963c10 100644 --- a/src/frontend/src/views/Tasks.tsx +++ b/src/frontend/src/views/Tasks.tsx @@ -48,14 +48,7 @@ const TasksSubmission = () => { //Fetch project for the first time useEffect(() => { if (state.projectTaskBoundries.findIndex((project) => project.id == environment.decode(encodedProjectId)) == -1) { - dispatch( - ProjectById( - `${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedProjectId)}`, - state.projectTaskBoundries, - environment.decode(encodedProjectId), - ), - state.projectTaskBoundries, - ); + dispatch(ProjectById(state.projectTaskBoundries, environment.decode(encodedProjectId))); dispatch( ProjectBuildingGeojsonService( `${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedProjectId)}/features`, @@ -63,14 +56,7 @@ const TasksSubmission = () => { ); } else { dispatch(ProjectActions.SetProjectTaskBoundries([])); - dispatch( - ProjectById( - `${import.meta.env.VITE_API_URL}/projects/${environment.decode(encodedProjectId)}`, - state.projectTaskBoundries, - environment.decode(encodedProjectId), - ), - state.projectTaskBoundries, - ); + dispatch(ProjectById(state.projectTaskBoundries, environment.decode(encodedProjectId))); } if (Object.keys(state.projectInfo).length == 0) { dispatch(ProjectActions.SetProjectInfo(projectInfo));