diff options
| author | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2021-03-13 15:28:29 +0100 |
|---|---|---|
| committer | Ruben Beltran del Rio <ruben@unlimited.pizza> | 2021-03-13 15:28:29 +0100 |
| commit | c1bc5993a694f6fd047a3881351827058042483b (patch) | |
| tree | 3d64130e1e6a2f86f70981df28cf22cd535bf2b2 /src/stores | |
| parent | bd8e98d7e24c4dbaee7db6ec7955f7c2f6d396a6 (diff) | |
Use routify and GraphQL server
Diffstat (limited to 'src/stores')
| -rw-r--r-- | src/stores/forum.js | 39 | ||||
| -rw-r--r-- | src/stores/forums.js | 48 |
2 files changed, 63 insertions, 24 deletions
diff --git a/src/stores/forum.js b/src/stores/forum.js new file mode 100644 index 0000000..e7d335e --- /dev/null +++ b/src/stores/forum.js @@ -0,0 +1,39 @@ +import { ApolloError } from '@apollo/client/core'; +import { readable } from 'svelte/store'; +import { client } from '$config/apollo'; +import { GET_FORUM } from '$data/queries'; + +const internals = { + + // The exported data structure + + initialValue: { + loading: true, + data: null, + error: undefined + } +}; + +export const getForum = function forum(id) { + + return readable(internals.initialValue, (set) => { + + client.watchQuery({ query: GET_FORUM, variables: { id } }).subscribe((result) => { + + if (result.errors) { + const error = new ApolloError({ graphQLErrors: result.errors }); + return set({ + loading: false, + data: null, + error + }); + } + + set({ + loading: false, + data: result.data.forum, + error: undefined + }); + }); + }); +}; diff --git a/src/stores/forums.js b/src/stores/forums.js index e7acb60..ade01a7 100644 --- a/src/stores/forums.js +++ b/src/stores/forums.js @@ -1,36 +1,36 @@ +import { ApolloError } from '@apollo/client/core'; import { readable } from 'svelte/store'; +import { client } from '$config/apollo'; +import { GET_FORUMS } from '$data/queries'; const internals = { - // Constants - - kChangeFeedEventName: 'changefeed:forums', - // The exported data structure - forums: [], - - // Handles messages from the event - - handleChangeFeed(data) { - - // No old value == add - if (!data.old_val) { - return internals.forums.push(data.new_val); - } + initialValue: { + loading: true, + data: [], + error: undefined + } +}; - // We have an old value, let's find it. - const index = internals.forums.findIndex((element) => element.id === data.old_val.id); +export const forums = readable(internals.initialValue, (set) => { - if (index > -1) { - if (data.new_val) { - return internals.forums.splice(index, 1, data.new_val || undefined); - } + client.watchQuery({ query: GET_FORUMS }).subscribe((result) => { - return internals.forums.splice(index, 1); + if (result.errors) { + const error = new ApolloError({ graphQLErrors: result.errors }); + return set({ + loading: false, + data: [], + error + }); } - } -}; -export const forums = readable(internals.forums, (set) => { + set({ + loading: false, + data: result.data.forums, + error: undefined + }); + }); }); |