connector-wordpress/source/front/graphql-wrapper.js

79 lines
1.9 KiB
JavaScript
Raw Normal View History

import SessionCache from './session-cache.js'
import { request } from 'graphql-request'
import DateTimeWrapper from './date-time-wrapper.js'
2021-01-08 14:08:40 +01:00
export function getUpcomingEvents({ url, limit }) {
const query = `
query ($limit: Int) {
events(limit: $limit) {
elements {
id,
title,
url,
beginsOn,
2021-04-11 22:42:50 +02:00
endsOn,
physicalAddress {
description,
locality
}
},
total
}
}
`
2021-12-09 19:46:19 +01:00
const dataInCache = SessionCache.get(sessionStorage, {
url,
query,
variables: { limit },
})
if (dataInCache !== null) {
return Promise.resolve(dataInCache)
2021-12-09 19:46:19 +01:00
}
return request(url, query, { limit }).then((data) => {
SessionCache.add(sessionStorage, { url, query, variables: { limit } }, data)
return Promise.resolve(data)
})
}
export function getUpcomingEventsByGroupName({ url, limit, groupName }) {
const query = `
query ($afterDatetime: DateTime, $groupName: String!, $limit: Int) {
group(preferredUsername: $groupName) {
organizedEvents(afterDatetime: $afterDatetime, limit: $limit) {
elements {
id,
title,
url,
beginsOn,
2021-04-11 22:42:50 +02:00
endsOn,
physicalAddress {
description,
locality
}
},
total
}
}
}
`
const afterDatetime = DateTimeWrapper.getCurrentDatetimeAsString()
2021-12-09 19:46:19 +01:00
const dataInCache = SessionCache.get(sessionStorage, {
url,
query,
variables: { afterDatetime, groupName, limit },
})
if (dataInCache !== null) {
return Promise.resolve(dataInCache)
2021-12-09 19:46:19 +01:00
}
return request(url, query, { afterDatetime, groupName, limit }).then(
(data) => {
SessionCache.add(
sessionStorage,
{ url, query, variables: { afterDatetime, groupName, limit } },
2023-11-11 20:28:16 +01:00
data,
2021-12-09 19:46:19 +01:00
)
return Promise.resolve(data)
2023-11-11 20:28:16 +01:00
},
2021-12-09 19:46:19 +01:00
)
}