173 lines
5.8 KiB
JavaScript
173 lines
5.8 KiB
JavaScript
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;
|
|
|
|
return (
|
|
<div>
|
|
<Head>
|
|
<title>
|
|
{t("common:unsubscribe-case-title")} -{" "}
|
|
{t("common:service-name")}
|
|
</title>
|
|
</Head>
|
|
<div id="page_wrapper">
|
|
<CookieBanner />
|
|
<Header />
|
|
<div className="govuk-width-container govuk-!-padding-bottom-9">
|
|
<main className="govuk-main-wrapper govuk-!-padding-top-9 govuk-!-padding-bottom-9">
|
|
<div className="govuk-grid-row">
|
|
<div className="govuk-grid-column-two-thirds ">
|
|
<h1>{t("common:unsubscribe-case-title")}</h1>
|
|
<p>
|
|
{props.caseRef == null
|
|
? t(
|
|
"common:unsubscribe-case-none-label"
|
|
)
|
|
: `You have unsubscribed from receiving email
|
|
updates for ${props.caseRef}`}
|
|
</p>
|
|
|
|
<Link href="/">
|
|
{t("common:logged-out-link")}
|
|
</Link>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
</div>
|
|
<Footer footerLinks={footerLinks} ticketnumber={props} />
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
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_watchlistid eq " +
|
|
watchedCaseID +
|
|
"&$select=_pinswg_contact_value,_pinswg_watchedcase_value";
|
|
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);
|
|
// Return a JSON object for success
|
|
return {
|
|
success: true,
|
|
message: "Case deleted successfully",
|
|
data: 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,
|
|
error: "Not found",
|
|
caseRef: "Not found"
|
|
};
|
|
} else {
|
|
consoleLogger(error);
|
|
// Return a JSON object for general errors
|
|
return {
|
|
success: false,
|
|
error: error.message || "An error occurred during deletion",
|
|
details: error.response ? error.response.data : null
|
|
};
|
|
}
|
|
});
|
|
};
|
|
|
|
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[0] || {};
|
|
|
|
let hasUnsubscribed = Object.prototype.hasOwnProperty.call(
|
|
caseObj,
|
|
"pinswg_watchlistid"
|
|
);
|
|
|
|
console.log("is there a caseObj", hasUnsubscribed);
|
|
|
|
const deleteCase = await deleteWatchedCases(query.watchlistid);
|
|
|
|
//hasUnsubscribed && (await deleteWatchedCases(query.watchlistid));
|
|
|
|
console.log(deleteCase);
|
|
return {
|
|
props: {
|
|
caseRef: hasUnsubscribed
|
|
? caseObj[
|
|
"_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
|
|
]
|
|
: null
|
|
}
|
|
};
|
|
}
|
|
);
|
|
|
|
const mapStateToProps = (state) => {};
|
|
|
|
const mapDispatchToProps = (dispatch) => {};
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(UnsubscribeCase);
|