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

79 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-04-13 20:50:56 +02:00
import SessionCache from './session-cache'
import { request } from 'graphql-request'
2021-04-13 20:50:56 +02:00
import DateTimeWrapper from './date-time-wrapper'
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 } },
data
)
return Promise.resolve(data)
2021-12-09 19:46:19 +01:00
}
)
}