Skip to content

Commit

Permalink
fix: sorts annotations on Reader mode
Browse files Browse the repository at this point in the history
  • Loading branch information
joaosantos15 committed Aug 3, 2020
1 parent 1b0a8d4 commit aa95dbf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
20 changes: 14 additions & 6 deletions src/Components/Reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,43 @@ import { connectMetamask, signV4 } from '../services/ethereum'
import Annotation from '../Models/Annotation'
import Logo from '../images/logo'
import { ModalContext } from '../Contexts/ModalProvider';
import { getAnnotationsByReference } from '../services/publisher'
import { getAnnotationCIDsByReference } from '../services/graph'
import { getAnnotationData } from '../services/ipfs'
import { getAnnotationsByReference } from '../services/publisher'
import { getTweetData } from '../helpers'

const Reader = ({ setPage }) => {
const [web3Enabled, setWeb3Enabled] = useState(false)
const [title, setTitle] = useState("nothing...")
const [tweetAnnotations, setTweetAnnotations] = useState([])

const sortByDate = (anno) => {
window.annotations = anno
return anno.sort((a, b) => {
const dateA = (new Date(a.payload.issuanceDate)).getTime()
const dateB = (new Date(b.payload.issuanceDate)).getTime()
return (dateB - dateA)
})
}

useEffect(() => {
(async () => {
const { tweetId, tweetAuthor } = getTweetData()
let annotations = [];
try {
const annotations = [];
const annotationsFromPublisher = await getAnnotationsByReference({ reference: tweetId });
for (const annotation of annotationsFromPublisher) {
annotations.push(new Annotation({ payload: annotation }));
annotations.push(new Annotation({ payload: annotation }));
}
annotations.sort((a, b) => (new Date(a.getDate()) - new Date(b.getDate())));
annotations = sortByDate(annotations)
setTweetAnnotations(annotations);
} catch (error) {
// fallback to the graph and ipfs
const annotationCIDs = await getAnnotationCIDsByReference({ reference: tweetId });
const annotations = [];
for (const annotationCID of annotationCIDs) {
annotations.push(new Annotation({ payload: await getAnnotationData(annotationCID) }));
}
annotations.sort((a, b) => (new Date(a.getDate()) - new Date(b.getDate())));
annotations = sortByDate(annotations)
setTweetAnnotations(annotations);
}
})();
Expand Down
2 changes: 1 addition & 1 deletion src/services/publisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export async function post(body) {
});
}

export async function getAnnotationsByReference({ first = 10, skip = 0, reference }) {
export async function getAnnotationsByReference({ first = 100, skip = 0, reference }) {
return get({ limit: first, offset: skip, content: reference });
}

Expand Down

0 comments on commit aa95dbf

Please sign in to comment.