Commit e9133867 authored by Øyvind Julsrud's avatar Øyvind Julsrud Committed by Torstein
Browse files

DEICH-5925: deichman.no: added koha endpoint and updated redux model

parent c630f820
......@@ -34,7 +34,7 @@ const ChildPatronAdminSearch = () => {
const router = useRouter();
const { date } = router.query;
const dispatch = useDispatch();
const { childPatrons } = useSelector(state => state.adminSearch);
const { patrons } = useSelector(state => state.adminSearch);
const formik = useFormik({
initialValues: initialValues(date),
......@@ -115,7 +115,7 @@ const ChildPatronAdminSearch = () => {
Resultat for <b>{formateDate(date)}</b>
</h2>
</Block>
{childPatrons?.length > 0 && (
{patrons?.length > 0 && (
<Block top={4}>
<Table full responsive>
<Table.Head>
......@@ -126,7 +126,7 @@ const ChildPatronAdminSearch = () => {
</Table.Row>
</Table.Head>
<Table.Body>
{childPatrons.map((obj, idx) => (
{patrons.map((obj, idx) => (
<Fragment key={idx}>
<Table.Row>
<Table.Cell label="Navn">
......
const routes = require("express").Router();
const CALL_ID_HEADER = "Deichman-CallID";
const kohaEndpoint = process.env.INTERNAL_URL_KOHA;
routes.post("/patronsearch/findbybirthdate", async (request, response) => {
const deichmanCallId = request.headers[CALL_ID_HEADER];
const jwtToken = request.session.jwt_token;
const { birthdate } = request.body;
try {
const kohaResponse = await fetch(
`${kohaEndpoint}/api/patronsearch/findbybirthdate`,
{
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8",
"Deichman-CallID": deichmanCallId,
Authorization: `Bearer ${jwtToken}`
},
body: JSON.stringify({ birthdate: birthdate })
}
);
if (kohaResponse.status === 200) {
const { patrons } = await kohaResponse.json();
response.status(200).send(patrons);
}
} catch (error) {
response.status(500).json({ error: "Unable to get patron" });
}
});
module.exports = routes;
......@@ -27,6 +27,7 @@ const resources = require("./resources");
const registration = require("./registration");
const search = require("./search");
const unauthenticated = require("./unauthenticated");
const ansatt = require("./ansatt");
// Apply Deichman-CallID: {GUID} to all requests
routes.use((req, res, next) => {
......@@ -59,5 +60,6 @@ routes.use("/resources", resources);
routes.use("/registration", registration);
routes.use("/search", search);
routes.use("/unauthenticated", unauthenticated);
routes.use("/ansatt", ansatt);
module.exports = routes;
// Actions
const GET_CHILD_PATRONS_SUCCESS = "admin/CHILD_PATRONS_SUCCESS";
const GET_CHILD_PATRONS_ERROR = "admin/CHILD_PATRONS_ERROR";
const dummyValues = [
{
name: "Barn 1",
category: "Barn",
dateOfBirth: "03.08.2016",
},
{
name: "Barn 2",
category: "Selvregistrert barn",
dateOfBirth: "03.08.2010",
},
{
name: "Test testesen",
category: "Barn",
dateOfBirth: "03.01.2021",
}
];
const initialState = {
childPatrons: []
patrons: []
};
// Reducer
export default function reducer(state = initialState, action = {}) {
switch (action.type) {
case GET_CHILD_PATRONS_SUCCESS:
return {
...state,
childPatrons: action.result
patrons: action.result
};
default:
......@@ -48,15 +28,28 @@ export function getChildPatronsError(err) {
return { type: GET_CHILD_PATRONS_ERROR, err };
}
export function fetchChildPatrons(date) {
const formatToKohaDate = date => {
const [d1, d2, m1, m2, y1, y2, y3, y4] = date.split("");
return `${y1}${y2}${y3}${y4}-${m1}${m2}-${d1}${d2}`;
};
export const fetchChildPatrons = birthdate => {
return async dispatch => {
try {
// DEBUG FETCH CALL
setTimeout(() => {
dispatch(getChildPatronsSuccess(dummyValues));
}, 100);
const response = await fetch("/api/ansatt/patronsearch/findbybirthdate", {
method: "POST",
headers: {
"Content-Type": "application/json; charset=utf-8"
},
credentials: "include",
body: JSON.stringify({birthdate: formatToKohaDate(birthdate)})
});
const patrons = await response.json();
dispatch(getChildPatronsSuccess(patrons));
} catch (err) {
dispatch(getChildPatronsError(err));
}
};
}
};
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment