import { wrapper } from "../../store/store"; import { connect } from "react-redux"; import useTranslation from "next-translate/useTranslation"; import Head from "next/head"; import Link from "next/link"; import CookieBanner from "../../components/cookieBanner"; import Footer from "../../components/footer"; import Header from "../../components/header"; import { hashAPIPath } from "../../actions/core/hash"; import { getToken } from "../../actions/core/token"; import { azureHeaders } from "../../actions/core/headers"; import { consoleLogger } from "../../actions/core/logger"; import axios from "axios"; const UnsubscribeCase = (props) => { let { t, lang } = useTranslation(); const { footerLinks, pages } = props; const casesArr = props.caseRef; console.log(casesArr); return (
{t("common:unsubscribe-case-title")} -{" "} {t("common:service-name")}

{t("common:unsubscribe-case-title")}

{casesArr == null ? t( "common:unsubscribe-case-none-label" ) : t( "common:unsubscribe-case-all-label" ) + (casesArr.length > 0 ? "s" : "")}

{casesArr != null && (
    {casesArr.map((key, index) => (
  • { key[ "_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue" ] }
  • ))}
)} {t("common:logged-out-link")}
); }; const WEBAPI_URL = process.env.RELAY_ROOT || "https://dev-pedw-ns.servicebus.windows.net/dev-pedw-hc/"; const getMailCaseById = async (watchedCaseID) => { var token = await getToken(); var queryUrl = "pinswg_watchlists?$count=true&$filter=pinswg_emailnotifications eq true and _pinswg_contact_value eq " + watchedCaseID; return axios .get( WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), azureHeaders(token.access_token) ) .then(({ data }) => { return data; }) .catch((error) => { consoleLogger(error); res.status(400).json(error); }); }; const deleteWatchedCases = async (watchedCaseID) => { var watchedCaseID = watchedCaseID; var token = await getToken(); var queryUrl = "pinswg_watchlists(" + watchedCaseID + ")"; console.log(WEBAPI_URL + queryUrl + hashAPIPath(queryUrl)); var config = { method: "delete", url: WEBAPI_URL + queryUrl + hashAPIPath(queryUrl), headers: { "OData-MaxVersion": "4.0", "OData-Version": "4.0", "Accept": "application/json", "Prefer": 'odata.include-annotations="*",return=representation', "Authorization": "Bearer " + token.access_token, "Content-Type": "application/json" } }; return axios(config) .then(({ data }) => { //console.log("deleted watched case - " + watchedCaseID); res.status(200).json(data); }) .catch((error) => { console.log("err----:", error); if (error.response && error.response.status === 404) { console.warn("Record not found — nothing to delete."); return { success: false, caseRef: "Not found" }; } else { consoleLogger(error); //res.status(400).json(error); } }); }; export const getServerSideProps = wrapper.getServerSideProps( (store) => async (ctx) => { const { query, req, res } = ctx; console.log("the query", query.watchlistid); const searchResultsObj = await getMailCaseById(query.watchlistid); let caseObj = searchResultsObj.value.length > 0 || {}; console.log("hwo many in caseObj", searchResultsObj.value.length); let hasUnsubscribed = searchResultsObj.value.length > 0; console.log("is there a caseObj", searchResultsObj.value.length > 0); console.log("hasUnsubscribed ", hasUnsubscribed); //const deleteCase = await deleteWatchedCases(query.watchlistid); //hasUnsubscribed && (await deleteWatchedCases(query.watchlistid)); searchResultsObj.value.forEach(async function (watchListID) { console.log(watchListID.pinswg_watchlistid); deleteWatchedCases(watchListID.pinswg_watchlistid); }); //console.log(deleteCase); return { props: { caseRef: hasUnsubscribed ? searchResultsObj.value : null } }; } ); const mapStateToProps = (state) => {}; const mapDispatchToProps = (dispatch) => {}; export default connect(mapStateToProps, mapDispatchToProps)(UnsubscribeCase);