Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MERGING release/23.10 into main #23

Merged
merged 9 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Sonar CI pipeline
on:
push:
branches:
- main
- 'release/**'
- develop
- 'feature/**'
pull_request:
types: [opened, synchronize, reopened]
jobs:
sonarcloud:
name: SonarCloud
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: SonarCloud Scan
uses: SonarSource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,9 @@ It is dedicated to be deployed as a module of [openimis-fe_js](https://github.co

- `home.HomePage.Container`: Use to override the container of the homepage completely (default: display `home.HomePage.Blocks`)
- `home.HomePage.Blocks`: Blocks displayed on the homepage. The current `user` is passed to each contribution. An example of block can be found on the [Claim management module](https://github.com/openimis/openimis-fe-claim_js)

## Configurations Options

- `HomePageContainer.showHomeMessage`: a boolean configuration flag that determines whether or not a special message will be displayed on the home page. If set to true, the application will fetch and display HTML content based on the URL specified in **HomePageContainer.homeMessageURL**. By default, this is set to false. It means that no additional message will be displayed on the home page.
- `HomePageContainer.homeMessageURL`: a string configuration that specifies the URL from which to fetch the HTML payload for display on the home page. By default, this is set to an empty string (""), meaning no URL is specified. This URL is used only when **HomePageContainer.showHomeMessage** is set to true.
- `HomePageContainer.showHealthFacilityMessage`: boolean to show HF status information. It shows days to the end of the contract of a HF assigned to a current user. Default false.
6 changes: 6 additions & 0 deletions sonar-project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
sonar.projectKey=openimis_openimis-fe-home_js
sonar.organization=openimis-1
sonar.projectName=openimis-fe-home_js

sonar.sources=src
sonar.sourceEncoding=UTF-8
114 changes: 110 additions & 4 deletions src/components/HomePageContainer.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,135 @@
import React from "react";

import { Box, Grid, Typography } from "@material-ui/core";
import { makeStyles } from "@material-ui/styles";
import { Contributions, useUserQuery } from "@openimis/fe-core";
import React from "react";

import {
Contributions,
useUserQuery,
ProgressOrError,
useModulesManager,
useTranslations,
} from "@openimis/fe-core";
import { useSelector } from "react-redux";

import { DEFAULT, MODULE_NAME, DAYS_HF_STATUS } from "../constants";
import { useFetchData } from "../hooks/useFetchData";
import { getTimeDifferenceInDaysFromToday } from "@openimis/fe-core";

const useStyles = makeStyles((theme) => ({
container: theme.page,
messageTitle: {
textAlign: "center",
color: "red",
fontSize: "16px"
},
messageDate: {
textAlign: "center",
fontSize: "16px",
},
healthFacilityLongTimeActive: {
textAlign: "center",
},
healthFacilityMediumTimeActive: {
textAlign: "center",
color: "gray",
},
healthFacilityShortTimeActive: {
textAlign: "center",
color: "red",
},
messageNotice: {
fontSize: "16px"
}
}));

const HomePageContainer = (props) => {
const HomePageContainer = () => {
const modulesManager = useModulesManager();
const userHealthFacility = useSelector(
(state) => state?.loc?.userHealthFacilityFullPath
);
const { formatMessage, formatMessageWithValues, formatDateFromISO } =
useTranslations(MODULE_NAME, modulesManager);
const showHomeMessage = modulesManager.getConf(
"fe-home",
"HomePageContainer.showHomeMessage",
DEFAULT.SHOW_HOME_MESSAGE
);
const homeMessageURL = modulesManager.getConf(
"fe-home",
"HomePageContainer.homeMessageURL",
DEFAULT.HOME_MESSAGE_URL
);
const showHealthFacilityMessage = modulesManager.getConf(
"fe-home",
"HomePageContainer.showHealthFacilityMessage",
DEFAULT.SHOW_HEALTH_FACILITY_MESSAGE
);

const { user } = useUserQuery();
const classes = useStyles();
const {
data: messageData,
loading: messageLoading,
error: messageError,
} = showHomeMessage ? useFetchData(homeMessageURL) : {};

if (!user) {
return null;
}

const dateToCheck = new Date(userHealthFacility?.contractEndDate ?? null);
const timeDelta = getTimeDifferenceInDaysFromToday(dateToCheck);
const getHealthFacilityStatus = (timeDelta) => {
if (timeDelta > DAYS_HF_STATUS.DAYS_LONG_TIME_ACTIVE) {
return classes.healthFacilityLongTimeActive;
} else if (timeDelta > DAYS_HF_STATUS.DAYS_MEDIUM_TIME_ACTIVE) {
return classes.healthFacilityMediumTimeActive;
} else {
return classes.healthFacilityShortTimeActive;
}
};

return (
<Grid container className={classes.container} spacing={2}>
<Grid item xs={12}>
<Box mt={2}>
<Typography variant="h4">
Welcome {user.otherNames} {user.lastName}!
{formatMessageWithValues("HomePageContainer.welcomeMessage", {
otherNames: user.otherNames,
lastName: user.lastName,
})}
</Typography>
</Box>
</Grid>
{showHealthFacilityMessage && (
<Grid item xs={12}>
<h2 className={getHealthFacilityStatus(timeDelta)}>
{userHealthFacility
? formatMessageWithValues(
"HomePageContainer.healthFacilityStatus",
{
date: `${formatDateFromISO(dateToCheck)}`,
days: `${timeDelta}`,
}
)
: formatMessage("HomePageContainer.noHealthFacilityAssigned")}
</h2>
</Grid>
)}
{showHomeMessage && (
<Grid item xs={12}>
<ProgressOrError progress={messageLoading} error={messageError} />
<h3 className={classes.messageTitle}>
{formatMessage("HomePageContainer.messageTitle")}
</h3>
<p className={classes.messageDate}> {messageData?.date} </p>
<div
className={classes.messageNotice}
dangerouslySetInnerHTML={{ __html: messageData?.notice }}
/>
</Grid>
)}
<Contributions contributionKey="home.HomePage.Blocks" user={user} />
</Grid>
);
Expand Down
11 changes: 11 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const DEFAULT = {
SHOW_HOME_MESSAGE: false,
HOME_MESSAGE_URL: "",
SHOW_HEALTH_FACILITY_MESSAGE: false,
};

export const DAYS_HF_STATUS = {
DAYS_LONG_TIME_ACTIVE: 180,
DAYS_MEDIUM_TIME_ACTIVE: 30,
};
export const MODULE_NAME = "home";
26 changes: 26 additions & 0 deletions src/hooks/useFetchData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { useState, useEffect } from "react";

export const useFetchData = (URL) => {
const [data, setData] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);

useEffect(() => {
const fetchData = async () => {
try {
setLoading(true);
const response = await fetch(URL);
const result = await response.json();
setData(result);
} catch (err) {
setError(err);
} finally {
setLoading(false);
}
};

fetchData();
}, [URL]);

return { data, loading, error };
};
3 changes: 2 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import HomePage from "./pages/HomePage";

import HomePageContainer from "./components/HomePageContainer";
import messages_en from "./translations/en.json";

const DEFAULT_CONFIG = {
"translations": [{ key: "en", messages: messages_en }],
"core.Router": [{ path: "home", component: HomePage }],
"home.HomePage.Container": HomePageContainer,
"home.HomePage.Blocks": [],
Expand Down
6 changes: 6 additions & 0 deletions src/translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"home.HomePageContainer.welcomeMessage": "Welcome {otherNames} {lastName}!",
"home.HomePageContainer.messageTitle": "Notice",
"home.HomePageContainer.healthFacilityStatus": "Your Hospital Contract is expiring on {date}: (Remaining days: {days})",
"home.HomePageContainer.noHealthFacilityAssigned": "User has no Health Facility assigned and there is no information on when the contract will expire."
}
Loading