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

Adds hasRead in notification data #25

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 13 additions & 8 deletions src/card/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import React from "react";
import PropTypes from "prop-types";
import "./card.scss";
import React from 'react';
import PropTypes from 'prop-types';
import './card.scss';

const Card = ({
imagePosition,
classNamePrefix,
cardOption,
renderImage,
data,
hasRead,
}) => {
const { image, message, receivedTime, detailPage } = data;

const classNameGenerator = () => {
const prefix = classNamePrefix ? `${classNamePrefix}-` : "";
const prefix = classNamePrefix ? `${classNamePrefix}-` : '';
const classes = {
card: `${prefix}card`,
content: `${prefix}content`,
Expand All @@ -22,19 +23,20 @@ const Card = ({
message: `${prefix}message`,
text: `${prefix}text`,
time: `${prefix}time`,
hasRead: `${prefix}card-has-read`,
};
return classes;
};

const classes = classNameGenerator();

return (
<div className={classes.card}>
<div className={`${classes.card} ${hasRead ? classes.hasRead : ''}`}>
<a href={detailPage} className="card-link">
<div
className={classes.content}
style={
imagePosition === "right" ? { flexDirection: "row-reverse" } : {}
imagePosition === 'right' ? { flexDirection: 'row-reverse' } : {}
}
>
{renderImage && (
Expand All @@ -61,10 +63,11 @@ const Card = ({

Card.defaultProps = {
renderImage: true,
imagePosition: "left",
imagePosition: 'left',
data: null,
classNamePrefix: null,
cardOption: null,
hasRead: false,
};

Card.propTypes = {
Expand All @@ -76,7 +79,9 @@ Card.propTypes = {
}),
renderImage: PropTypes.bool,
cardOption: PropTypes.func,
imagePosition: PropTypes.oneOf(["left", "right"]),
imagePosition: PropTypes.oneOf(['left', 'right']),
classNamePrefix: PropTypes.string,
hasRead: PropTypes.bool,
};

export default Card;
64 changes: 40 additions & 24 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React, { Component, Fragment } from "react";
import PropTypes from "prop-types";
import axios from "axios";
import Card from "./card";
import Spinner from "./spinner";
import defaultIcon from "./assets/default_bell.svg";
import "./styles.scss";
import React, { Component, Fragment } from 'react';
import PropTypes from 'prop-types';
import axios from 'axios';
import Card from './card';
import Spinner from './spinner';
import defaultIcon from './assets/default_bell.svg';
import './styles.scss';

class Notifications extends Component {
constructor(props) {
Expand Down Expand Up @@ -32,12 +32,12 @@ class Notifications extends Component {
const { data, styles } = this.state;
const { fetchData } = this.props;

document.addEventListener("mousedown", (event) => {
document.addEventListener('mousedown', (event) => {
this.handleClickOutside(event);
});

// If data is a URL
if (typeof data === "string" && this.validateURL(data)) {
if (typeof data === 'string' && this.validateURL(data)) {
axios
.get(data)
.then((response) => this.setState({ data: response.data }))
Expand All @@ -59,7 +59,7 @@ class Notifications extends Component {
if (fetchData) {
// Infinite scroll to notification container
if (data.length > 0) {
scrollRef.addEventListener("scroll", () => {
scrollRef.addEventListener('scroll', () => {
if (
scrollRef.scrollTop + scrollRef.clientHeight >=
scrollRef.scrollHeight
Expand All @@ -80,7 +80,7 @@ class Notifications extends Component {
}

componentWillUnmount() {
document.removeEventListener("mousedown", (event) => {
document.removeEventListener('mousedown', (event) => {
this.handleClickOutside(event);
});
}
Expand All @@ -95,8 +95,8 @@ class Notifications extends Component {

validateURL = (myURL) => {
const pattern = new RegExp(
"^(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*(\\?[;&a-z\\d%_.~+=-]*)?(\\#[-a-z\\d_]*)?$",
"i"
'^(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*(\\?[;&a-z\\d%_.~+=-]*)?(\\#[-a-z\\d_]*)?$',
'i'
);
return pattern.test(myURL);
};
Expand All @@ -112,7 +112,7 @@ class Notifications extends Component {

classNameGenerator = () => {
const { classNamePrefix } = this.props;
const prefix = classNamePrefix ? `${classNamePrefix}-` : "";
const prefix = classNamePrefix ? `${classNamePrefix}-` : '';
const classes = {
notifications: `${prefix}notifications`,
icon: `${prefix}icon`,
Expand All @@ -131,8 +131,14 @@ class Notifications extends Component {
};

render() {
const { show, styles, loading, data, classes, notificationCount } =
this.state;
const {
show,
styles,
loading,
data,
classes,
notificationCount,
} = this.state;
const {
viewAllBtn,
icon,
Expand All @@ -151,10 +157,20 @@ class Notifications extends Component {
Array.isArray(data) &&
(CustomComponent
? data.map((item) => (
<CustomComponent key={item.message} {...this.props} data={item} />
<CustomComponent
key={item.message}
hasRead={item.hasRead}
{...this.props}
data={item}
/>
))
: data.map((item) => (
<Card key={item.message} {...this.props} data={item} />
<Card
key={item.message}
hasRead={item.hasRead}
{...this.props}
data={item}
/>
)));

return (
Expand All @@ -171,9 +187,9 @@ class Notifications extends Component {
{notificationCount > 0 && (
<div
className={classes.count}
style={notificationCount >= 100 ? { fontSize: "8px" } : null}
style={notificationCount >= 100 ? { fontSize: '8px' } : null}
>
{notificationCount < 100 ? notificationCount : "99+"}
{notificationCount < 100 ? notificationCount : '99+'}
</div>
)}
</div>
Expand All @@ -184,7 +200,7 @@ class Notifications extends Component {
style={{
...styles,
width,
visibility: show ? "visible" : "hidden",
visibility: show ? 'visible' : 'hidden',
opacity: show ? 1 : 0,
}}
>
Expand Down Expand Up @@ -239,11 +255,11 @@ Notifications.defaultProps = {
height: null,
width: null,
header: {
title: "Notifications",
option: { text: "Mark all as read", onClick: () => {} },
title: 'Notifications',
option: { text: 'Mark all as read', onClick: () => {} },
},
headerBackgroundColor: null,
classNamePrefix: "",
classNamePrefix: '',
icon: defaultIcon,
style: {},
};
Expand Down