Skip to content

Commit

Permalink
Fixed refresh issue for marriage proposal view. Added polling for not…
Browse files Browse the repository at this point in the history
…ification and marriage proposal overview
  • Loading branch information
Dexagod committed Sep 25, 2020
1 parent e289635 commit 5d43677
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 51 deletions.
1 change: 0 additions & 1 deletion src/Components/MarriageRequestComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ const MarriageRequestComponent = (props) => {

const validateSubmission = async () => {
for (let person of state){
console.log("person", person)
if (!person.webId) {
window.alert(person.webId + 'is not a valid webId');
return false
Expand Down
65 changes: 39 additions & 26 deletions src/Components/MarriageViewComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,60 @@ const INVITATIONREFUSED = ns.demo('refused')
const MarriageViewComponent = (props) => {
let allcontacts = props.contract.spouse.map(e => { e.type='spouse'; return e})
allcontacts = allcontacts.concat(props.contract.witness.map(e => { e.type='witness'; return e}))
const [contacts, setContacts] = useState(allcontacts.map(e => e.accepted=false))
allcontacts = allcontacts.map(e => { e['status'] = e['status'] || 'pending' ; return e})
const [contacts, setContacts] = useState(allcontacts)

useEffect(() => {
let mounted = true
async function getContactStatus(contactWebId){
let accepted, refused;
for await (const acceptedEvent of data[contactWebId][INVITATIONACCEPTED]){
if (`${await acceptedEvent}` === props.contract.id) accepted = true;
}
for await (const refusedEvent of data[contactWebId][INVITATIONREFUSED]){
if (`${await refusedEvent}` === props.contract.id) refused = true;
}
return accepted ? 'accepted' : (refused ? 'refused' : 'pending')
async function refreshContacts() {
updateContacts(allcontacts).then(updatedContacts => {
if(mounted) setContacts(updatedContacts)
})
}
async function updateStatus() {
for (let contact of allcontacts){
contact['status'] = await getContactStatus(contact.id)
}
if(mounted) setContacts(allcontacts)
}
updateStatus()
refreshContacts()
var interval = setInterval(() => {
refreshContacts()
}, 7000);
return () => {
mounted = false;
clearInterval(interval);
}
}, [props.contract])

function accept() {
acceptProposal(props.webId, props.contract.id, props.contract.creator)
async function getContactStatus(contactWebId){
let accepted, refused;
data.clearCache()
for await (const acceptedEvent of data[contactWebId][INVITATIONACCEPTED]){
if (`${await acceptedEvent}` === props.contract.id) accepted = true;
}
for await (const refusedEvent of data[contactWebId][INVITATIONREFUSED]){
if (`${await refusedEvent}` === props.contract.id) refused = true;
}
return accepted ? 'accepted' : (refused ? 'refused' : 'pending')
}

async function updateContacts(contactsToUpdate) {
const contacts = []
for (let contact of contactsToUpdate){
contact['status'] = await getContactStatus(contact.id)
contacts.push(contact)
}
return contacts
}

async function accept() {
await acceptProposal(props.webId, props.contract.id, props.contract.creator)
resetView()
}

function refuse() {
acceptProposal(props.webId, props.contract.id, props.contract.creator)
async function refuse() {
await acceptProposal(props.webId, props.contract.id, props.contract.creator)
resetView()
}

function resetView() {
const view = availableViews.marriageview
view.args = {contract: props.contract}
props.setview(view)
async function resetView() {
const updatedContacts = await updateContacts(contacts)
setContacts(updatedContacts)
}

function getContactButton(contact){
Expand All @@ -84,7 +98,6 @@ const MarriageViewComponent = (props) => {
}
return <Button className={`${styles.pending} valuebutton`} disabled> Pending </Button>
}

return (
<div id="marriageViewContainer" className='container'>
<h4> Marriage Proposal </h4>
Expand Down
2 changes: 1 addition & 1 deletion src/Components/NotificationsViewerComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const NotificationsViewerComponent = (props) => {
<h4> Notifications </h4>
<br />
{notifications.map(notification => {
return ( <NotificationCard notification={notification} {...props} /> )
return ( <NotificationCard notification={notification} {...props} key={notification.metadata.id}/> )
})}

</div>
Expand Down
45 changes: 29 additions & 16 deletions src/hooks/useNotifications.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
import { useState, useEffect } from 'react';
import { getNotificationMetadata, getNotification } from '../util/notifications';
import { getNotificationMetadata, getNotification, checkNewNotifications } from '../util/notifications';

const useNotifications = function(webId) {
const [notifications, setNotifications] = useState([]);

useEffect(() => {
let mounted = true
async function fetchNotifications(webId){
const notificationsMetadata = await getNotificationMetadata(webId)
if(!notificationsMetadata) return []
const notificationList = []
for await (const metadata of notificationsMetadata){
const notification = await getNotification(metadata.id)
notification.metadata = metadata
notificationList.push(notification)
}
if(mounted) setNotifications(notificationList)
}
if(webId) fetchNotifications(webId)
updateNotifications(webId, notifications)
const interval = setInterval(() => {
updateNotifications(webId, notifications)
}, 7000);
return () => {
mounted = false
clearInterval(interval);
}
}, [webId])
}, [webId, notifications])

return notifications


async function updateNotifications(webId, currentNotifications){
if(webId && await checkNewNotifications(webId, currentNotifications)) {
const updatedNotifications = await fetchNotifications(webId)
setNotifications(updatedNotifications)
}
}

async function fetchNotifications(webId){
const notificationsMetadata = await getNotificationMetadata(webId)
if(!notificationsMetadata) return []
const notificationList = []
for await (const metadata of notificationsMetadata){
const notification = await getNotification(metadata.id)
notification.metadata = metadata
notificationList.push(notification)
}
return notificationList
}
}

export default useNotifications
4 changes: 2 additions & 2 deletions src/util/MarriageController.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function createAcceptanceNotification(webId, proposalId, authorId){
a as:Invite ;
as:actor <${authorId}> ;
as:object <${webId}> ;
as:target <${proposalId}> .
as:target <${proposalId}> ;
] ;
as:target <${proposalId}> ;
as:summary "Acceptance of the offer to participate in the marriage contract" .
Expand All @@ -89,7 +89,7 @@ export function createRejectionNotification(webId, proposalId, authorId){
a as:Invite ;
as:actor <${authorId}> ;
as:object <${webId}> ;
as:target <${proposalId}> .
as:target <${proposalId}> ;
] ;
as:target <${proposalId}> ;
as:summary "Rejection of the offer to participate in the marriage contract" .
Expand Down
17 changes: 12 additions & 5 deletions src/util/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@ export default async function notify(notificationBody, subjects) {
for (let subject of subjects) {
const inbox = await getInbox(subject);
if (inbox) postFile(inbox, notificationBody)
else console.error(subject + ' does not have an inbox')
}
}

async function getInbox(subject){
const inbox = await data[subject][ns.ldp('inbox')]
if(!inbox) console.error(subject + ' does not profide an inbox.')
return inbox && inbox.value
}

export async function checkNewNotifications(webId, currentNotifications) {
if(!currentNotifications) return true
const inbox = await getInbox(webId)
if(!inbox) return false
const store = await getStore(inbox)
let notificationsMetadata = await store.getQuads(inbox, ns.ldp('contains'))
if (notificationsMetadata.length === currentNotifications.length) return false;
return true
}

export async function getNotificationMetadata(webId) {
const inbox = await getInbox(webId)
if(!inbox) {
console.error(webId + ' does not profide an inbox.')
return []
}
if(!inbox) return []
const store = await getStore(inbox)
let notificationsMetadata = await store.getQuads(inbox, ns.ldp('contains'))
notificationsMetadata = notificationsMetadata.map(quad => { return ( {id: quad.object.value})})
Expand Down

0 comments on commit 5d43677

Please sign in to comment.