dashboard/src/components/greeter.tsx

125 lines
2.9 KiB
TypeScript
Raw Normal View History

2020-07-08 19:36:36 +02:00
import styled from "styled-components";
const GreeterContainer = styled.div`
padding: 2rem 0;
`;
const GreetText = styled.h1`
font-weight: 900;
font-size: 3rem;
margin: 0.5rem 0;
2021-07-10 23:57:08 +02:00
color: ${(props) => props.theme.mainColor};
2020-07-08 19:36:36 +02:00
`;
const DateText = styled.h3`
font-weight: 400;
font-size: 1rem;
text-transform: uppercase;
margin: 0;
2021-07-10 23:57:08 +02:00
color: ${(props) => props.theme.accentColor};
2020-07-08 19:36:36 +02:00
`;
2021-06-14 13:17:31 +02:00
export interface IGreeterComponentProps {
data: IGreeterProps;
2021-06-14 13:17:31 +02:00
}
2021-04-03 16:54:44 +02:00
export interface IGreeterProps {
months: Array<string>;
days: Array<string>;
greetings: Array<IGreetingProps>;
dateformat: string;
}
interface IGreetingProps {
greeting: string;
start: number;
end: number;
}
/**
2021-06-14 11:29:03 +02:00
* Checks if a number is between two numbers
* @param {number} a number that's supposed to be checked
* @param {number} b minimum
* @param {number} c maximum
2021-04-03 16:54:44 +02:00
*/
2021-06-14 13:17:31 +02:00
export const isBetween = (a: number, b: number, c: number): boolean =>
2021-05-15 16:09:12 +02:00
a >= b && a <= c;
2021-03-21 19:59:18 +01:00
/**
* Returns a greeting based on the current time
2021-06-14 11:29:03 +02:00
* @param {Array<IGreetingProps>} greetings a list of greetings with start and end date
* @returns {string} a greeting
2021-03-21 19:59:18 +01:00
*/
2021-06-14 13:17:31 +02:00
export const getGreeting = (greetings: Array<IGreetingProps>): string => {
2021-05-15 16:09:12 +02:00
let hours = Math.floor(new Date().getHours());
2021-04-03 16:54:44 +02:00
let result = "";
2021-05-15 16:09:12 +02:00
greetings.forEach((greeting) => {
if (isBetween(hours, greeting.start, greeting.end))
result = greeting.greeting;
});
2021-04-03 16:54:44 +02:00
return result;
2020-07-08 19:36:36 +02:00
};
2021-03-21 19:59:18 +01:00
/**
* Returns the appropriate extension for a number (eg. 'rd' for '3' to make '3rd')
2021-06-14 11:29:03 +02:00
* @param {number} day number of a day within a month
* @returns {string} extension for that number
2021-03-21 19:59:18 +01:00
*/
2021-06-14 13:17:31 +02:00
export const getExtension = (day: number) => {
2021-07-11 15:58:42 +02:00
let extension = "th";
2020-07-08 19:36:36 +02:00
2021-06-14 13:17:31 +02:00
if (day % 10 === 1) {
2020-07-08 19:36:36 +02:00
extension = "st";
} else if (day % 10 === 2) {
extension = "nd";
} else if (day % 10 === 3) {
extension = "rd";
}
return extension;
};
2021-03-21 19:59:18 +01:00
/**
* Generates the current date
2021-06-14 11:29:03 +02:00
* @param {string} format format of the date string
* @returns {string} current date as a string
2021-03-21 19:59:18 +01:00
*/
2021-06-14 13:17:31 +02:00
export const getDateString = (
2021-05-15 16:09:12 +02:00
weekdays: Array<string>,
months: Array<string>,
2021-06-14 11:29:03 +02:00
format: string,
2021-05-15 16:09:12 +02:00
) => {
2020-07-08 19:36:36 +02:00
let currentDate = new Date();
2021-04-03 16:54:44 +02:00
let weekday = weekdays[currentDate.getUTCDay()];
let day = currentDate.getDate();
let month = months[currentDate.getUTCMonth()];
let extension = getExtension(day);
let year = currentDate.getFullYear();
2021-05-15 16:09:12 +02:00
return format
.replace("%wd", weekday)
.replace("%d", day.toString())
.replace("%e", extension)
.replace("%m", month)
.replace("%y", year.toString());
2020-07-08 19:36:36 +02:00
};
2021-03-21 19:59:18 +01:00
/**
* Renders the Greeter
2021-06-14 11:29:03 +02:00
* @param {IGreeterComponentProps} data required greeter data
* @returns {React.ReactNode} the greeter
2021-03-21 19:59:18 +01:00
*/
const Greeter = ({ data }: IGreeterComponentProps) => (
<GreeterContainer>
<DateText>
{getDateString(data.days, data.months, data.dateformat)}
</DateText>
<GreetText>{getGreeting(data.greetings)}</GreetText>
</GreeterContainer>
);
2020-07-08 19:36:36 +02:00
export default Greeter;