From c919c86c62f59365a9177ad34ee051f3650a1a27 Mon Sep 17 00:00:00 2001 From: riya-2206 Date: Fri, 14 Jun 2024 16:40:18 +0530 Subject: [PATCH 1/2] changed api error handling response --- package-lock.json | 12 ++++++++ package.json | 1 + src/components/C4GT2024Page.js | 2 +- src/components/ProjectDescription.js | 46 +++++++++++++++++++++------- src/components/SubDescription.js | 29 ++++++++++++++---- src/pages/docs/2024/index.js | 15 +++++---- src/pages/docs/2024/org/index.js | 25 +++++++-------- 7 files changed, 92 insertions(+), 38 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7c2c2bb73..c39bbf09d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,6 +15,7 @@ "clsx": "^1.1.1", "docusaurus": "^1.14.7", "file-loader": "^6.2.0", + "markdown-to-jsx": "^7.4.7", "prism-react-renderer": "^1.2.1", "react": "^17.0.1", "react-dom": "^17.0.1", @@ -20305,6 +20306,17 @@ "node": ">=0.10.0" } }, + "node_modules/markdown-to-jsx": { + "version": "7.4.7", + "resolved": "https://registry.npmjs.org/markdown-to-jsx/-/markdown-to-jsx-7.4.7.tgz", + "integrity": "sha512-0+ls1IQZdU6cwM1yu0ZjjiVWYtkbExSyUIFU2ZeDIFuZM1W42Mh4OlJ4nb4apX4H8smxDHRdFaoIVJGwfv5hkg==", + "engines": { + "node": ">= 10" + }, + "peerDependencies": { + "react": ">= 0.14.0" + } + }, "node_modules/markdown-toc": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/markdown-toc/-/markdown-toc-1.2.0.tgz", diff --git a/package.json b/package.json index 422b268db..f52d46479 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "clsx": "^1.1.1", "docusaurus": "^1.14.7", "file-loader": "^6.2.0", + "markdown-to-jsx": "^7.4.7", "prism-react-renderer": "^1.2.1", "react": "^17.0.1", "react-dom": "^17.0.1", diff --git a/src/components/C4GT2024Page.js b/src/components/C4GT2024Page.js index 4d1e9c7d5..ce0005e7b 100644 --- a/src/components/C4GT2024Page.js +++ b/src/components/C4GT2024Page.js @@ -24,7 +24,7 @@ function C4GT2024Page() {
  • C4GT'24 diff --git a/src/components/ProjectDescription.js b/src/components/ProjectDescription.js index 8c36a67eb..55b6ffd24 100644 --- a/src/components/ProjectDescription.js +++ b/src/components/ProjectDescription.js @@ -4,20 +4,27 @@ import useParseMarkdown from "../hooks/useParseMarkdown"; function ProjectDescription({ currentIssue, - currentIssueData, setCurrentTab, setSelectedProject, setIssueNumber, - error, - setError + issueData }) { const history = useHistory(); const {API_AUTH_KEY,API_BASE_URL} = useParseMarkdown(); const [description, setDescription] = useState(null); const [mobile, setMobile] = useState(false); + const [error,setError] =useState(null) + const [currentIssueData,setCurrentIssueData] = useState(null) useEffect(() => { setError(()=>null); + setCurrentIssueData(()=>{ + let data = issueData?.filter((d,i)=>{ + if(d.org_name===currentIssue) return d.issues + else return null + }) + return data + }) fetch(`${API_BASE_URL}/issues/${currentIssue}`, { method: "GET", headers: { @@ -25,13 +32,16 @@ function ProjectDescription({ }, }) .then((response) => { - if (!response.ok) { - throw new Error(`Error! status: ${response.status}`); - } return response.json(); }) .then((data) => { - setDescription(() => data); + if(data?.message){ + throw new Error(`${data?.message}`); + } + else if(data?.error){ + throw new Error(`${data?.error}`); + } + else setDescription(() => data); }) .catch((error) => { setError(error); @@ -56,7 +66,20 @@ function ProjectDescription({ }, []); return ( <> - {!error && ( + {error ? ( +
    +

    {error.message}

    +
    + ) : description ? (
    {currentIssueData?.length != 0 && (
    - )} + ):<>} ); } diff --git a/src/components/SubDescription.js b/src/components/SubDescription.js index 354a93d31..f26004f5f 100644 --- a/src/components/SubDescription.js +++ b/src/components/SubDescription.js @@ -1,9 +1,10 @@ import React, { useEffect, useState } from "react"; import useParseMarkdown from "../hooks/useParseMarkdown"; -function SubProjectDescription({ selectedProject, issueNumber, currentIssue,error,setError }) { +function SubProjectDescription({ selectedProject, issueNumber, currentIssue }) { const {weeklyGoals, weeklyLearnings,API_AUTH_KEY,API_BASE_URL} = useParseMarkdown(); const [description, setDescription] = useState(null); + const [error,setError] =useState(null) useEffect(() => { setError(()=>null); @@ -14,13 +15,16 @@ function SubProjectDescription({ selectedProject, issueNumber, currentIssue,erro }, }) .then((response) => { - if (!response.ok) { - throw new Error(`Error! status: ${response.status}`); - } return response.json(); }) .then((data) => { - setDescription(() => data); + if(data?.message){ + throw new Error(`${data?.message}`); + } + else if(data?.error){ + throw new Error(`${data?.error}`); + } + else setDescription(() => data); }) .catch((error) => { setError(()=>error) @@ -28,7 +32,20 @@ function SubProjectDescription({ selectedProject, issueNumber, currentIssue,erro }, [selectedProject]); return (<> - {!error &&
    +

    {error.message}

    +
    + ) :
    diff --git a/src/pages/docs/2024/index.js b/src/pages/docs/2024/index.js index 57edee8d2..2f66c676f 100644 --- a/src/pages/docs/2024/index.js +++ b/src/pages/docs/2024/index.js @@ -19,13 +19,16 @@ function C4GT2024() { }, }) .then((response) => { - if (!response.ok) { - throw new Error(`Error! status: ${response.status}`); - } return response.json(); }) .then((data) => { - setIssueData(() => data); + if(data?.message){ + throw new Error(`${data?.message}`); + } + else if(data?.error){ + throw new Error(`${data?.error}`); + } + else setIssueData(() => data); }) .catch((error) => { setError(() => error); @@ -43,8 +46,8 @@ function C4GT2024() { height: "100vh", flexDirection: "column", }} - >

    Oops, something went wrong

    -

    {error.message}

    + > +

    {error.message}

    ) : ( diff --git a/src/pages/docs/2024/org/index.js b/src/pages/docs/2024/org/index.js index 63fecb012..01312c229 100644 --- a/src/pages/docs/2024/org/index.js +++ b/src/pages/docs/2024/org/index.js @@ -13,13 +13,13 @@ function C4GT2024() { const {API_AUTH_KEY,API_BASE_URL} = useParseMarkdown(); const [currentTab, setCurrentTab] = useState("description"); const [currentIssue, setCurrentIssue] = useState(null); - const [currentIssueData, setCurrentIssueData] = useState(null); const [issueData, setIssueData] = useState(null); const [selectedProject, setSelectedProject] = useState(null); const [issueNumber, setIssueNumber] = useState(null); const [error, setError] = useState(null); useEffect(() => { + setError(()=>null); const queryString = location.search; const urlParams = new URLSearchParams(queryString); const product = urlParams.get("id") ? urlParams.get("id") : "c4gt"; @@ -29,7 +29,6 @@ function C4GT2024() { const newUrl = `/docs/2024/org?id=${product}`; history.push(newUrl); setCurrentIssue(() => product); - setError(()=>null); fetch(`${API_BASE_URL}/issues`, { method: "GET", headers: { @@ -37,13 +36,16 @@ function C4GT2024() { }, }) .then((response) => { - if (!response.ok) { - throw new Error(`Error! status: ${response.status}`); - } return response.json(); }) .then((data) => { - setIssueData(() => data); + if(data?.message){ + throw new Error(`${data?.message}`); + } + else if(data?.error){ + throw new Error(`${data?.error}`); + } + else setIssueData(() => data); }) .catch((error) => { setError(()=>error); @@ -68,8 +70,8 @@ function C4GT2024() { height: "100vh", flexDirection: "column", }} - >

    Oops, something went wrong

    -

    {error.message}

    + > +

    {error.message}

    ) : ( @@ -106,7 +108,6 @@ function C4GT2024() { setSelectedProject(() => null); setIssueNumber(() => null); setCurrentTab(() => "description"); - setCurrentIssueData(() => data?.issues); }} >
    ) : ( <> @@ -185,8 +184,6 @@ function C4GT2024() { selectedProject={selectedProject} issueNumber={issueNumber} currentIssue={currentIssue} - error={error} - setError={setError} /> ) : ( <> From 0712cf9fa0cfac002b14a429e973c0c37e2ab6d5 Mon Sep 17 00:00:00 2001 From: riya-2206 Date: Fri, 14 Jun 2024 16:49:58 +0530 Subject: [PATCH 2/2] removed commented code --- src/components/C4GT2024Page.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/components/C4GT2024Page.js b/src/components/C4GT2024Page.js index ce0005e7b..08007dad9 100644 --- a/src/components/C4GT2024Page.js +++ b/src/components/C4GT2024Page.js @@ -24,7 +24,6 @@ function C4GT2024Page() {
  • C4GT'24