2022-03-13 16:57:59 +01:00
|
|
|
import React, { useState, useEffect, useContext, useReducer, createContext } from 'react';
|
2022-03-13 18:13:53 +01:00
|
|
|
import { nanoid } from 'nanoid';
|
2021-12-03 20:37:38 +01:00
|
|
|
import reducer from './reducer';
|
2022-02-03 18:55:38 +01:00
|
|
|
import useIdb from './useIdb';
|
2022-03-13 18:13:53 +01:00
|
|
|
import useLoadCollectionsFromIdb from './useLoadCollectionsFromIdb';
|
|
|
|
import useSyncCollectionsToIdb from './useSyncCollectionsToIdb';
|
2022-01-18 15:58:18 +01:00
|
|
|
import { sendRequest } from '../../network';
|
2021-12-03 20:37:38 +01:00
|
|
|
|
|
|
|
export const StoreContext = createContext();
|
|
|
|
|
|
|
|
const collection = {
|
|
|
|
"id": nanoid(),
|
2022-01-01 09:26:34 +01:00
|
|
|
"name": "spacex",
|
2021-12-03 20:37:38 +01:00
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"id": nanoid(),
|
|
|
|
"name": "Launches",
|
|
|
|
"depth": 1,
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"id": nanoid(),
|
|
|
|
"depth": 2,
|
|
|
|
"name": "Capsules",
|
|
|
|
"request": {
|
2022-01-21 08:55:39 +01:00
|
|
|
"type": "graphql",
|
2021-12-03 20:37:38 +01:00
|
|
|
"url": "https://api.spacex.land/graphql/",
|
|
|
|
"method": "POST",
|
|
|
|
"headers": [],
|
|
|
|
"body": {
|
|
|
|
"mimeType": "application/graphql",
|
|
|
|
"graphql": {
|
|
|
|
"query": "{\n launchesPast(limit: 10) {\n mission_name\n launch_date_local\n launch_site {\n site_name_long\n }\n links {\n article_link\n video_link\n }\n rocket {\n rocket_name\n first_stage {\n cores {\n flight\n core {\n reuse_count\n status\n }\n }\n }\n second_stage {\n payloads {\n payload_type\n payload_mass_kg\n payload_mass_lbs\n }\n }\n }\n ships {\n name\n home_port\n image\n }\n }\n}",
|
|
|
|
"variables": ""
|
|
|
|
}
|
|
|
|
}
|
2021-12-09 17:44:49 +01:00
|
|
|
},
|
|
|
|
"response": null
|
2021-12-03 20:37:38 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": nanoid(),
|
|
|
|
"depth": 2,
|
|
|
|
"name": "Missions",
|
|
|
|
"request": {
|
2022-01-21 08:55:39 +01:00
|
|
|
"type": "graphql",
|
2021-12-03 20:37:38 +01:00
|
|
|
"url": "https://api.spacex.land/graphql/",
|
|
|
|
"method": "POST",
|
|
|
|
"headers": [],
|
|
|
|
"body": {
|
|
|
|
"mimeType": "application/graphql",
|
|
|
|
"graphql": {
|
2021-12-09 17:44:49 +01:00
|
|
|
"query": "{\n launches {\n launch_site {\n site_id\n site_name\n }\n launch_success\n }\n}",
|
2021-12-03 20:37:38 +01:00
|
|
|
"variables": ""
|
|
|
|
}
|
|
|
|
}
|
2021-12-09 17:44:49 +01:00
|
|
|
},
|
|
|
|
"response": null
|
2021-12-03 20:37:38 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2022-01-01 09:26:34 +01:00
|
|
|
const collection2 = {
|
|
|
|
"id": nanoid(),
|
|
|
|
"name": "notebase",
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"id": nanoid(),
|
|
|
|
"name": "Notes",
|
|
|
|
"depth": 1,
|
|
|
|
"items": [
|
|
|
|
{
|
|
|
|
"id": nanoid(),
|
|
|
|
"depth": 2,
|
|
|
|
"name": "Create",
|
|
|
|
"request": {
|
2022-01-21 08:55:39 +01:00
|
|
|
"type": "graphql",
|
2022-01-01 09:26:34 +01:00
|
|
|
"url": "https://api.spacex.land/graphql/",
|
|
|
|
"method": "POST",
|
|
|
|
"headers": [],
|
|
|
|
"body": {
|
|
|
|
"mimeType": "application/graphql",
|
|
|
|
"graphql": {
|
|
|
|
"query": "{\n launchesPast(limit: 10) {\n mission_name\n launch_date_local\n launch_site {\n site_name_long\n }\n links {\n article_link\n video_link\n }\n rocket {\n rocket_name\n first_stage {\n cores {\n flight\n core {\n reuse_count\n status\n }\n }\n }\n second_stage {\n payloads {\n payload_type\n payload_mass_kg\n payload_mass_lbs\n }\n }\n }\n ships {\n name\n home_port\n image\n }\n }\n}",
|
|
|
|
"variables": ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"response": null
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": nanoid(),
|
|
|
|
"depth": 2,
|
|
|
|
"name": "Update",
|
|
|
|
"request": {
|
2022-01-21 08:55:39 +01:00
|
|
|
"type": "graphql",
|
2022-01-01 09:26:34 +01:00
|
|
|
"url": "https://api.spacex.land/graphql/",
|
|
|
|
"method": "POST",
|
|
|
|
"headers": [],
|
|
|
|
"body": {
|
|
|
|
"mimeType": "application/graphql",
|
|
|
|
"graphql": {
|
|
|
|
"query": "{\n launches {\n launch_site {\n site_id\n site_name\n }\n launch_success\n }\n}",
|
|
|
|
"variables": ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"response": null
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
]
|
|
|
|
};
|
|
|
|
|
2021-12-03 20:37:38 +01:00
|
|
|
const initialState = {
|
2022-02-03 18:55:38 +01:00
|
|
|
idbConnection: null,
|
2022-03-13 13:13:21 +01:00
|
|
|
collections: [],
|
2022-03-13 22:13:36 +01:00
|
|
|
activeRequestTabUid: null,
|
2022-01-18 15:58:18 +01:00
|
|
|
requestQueuedToSend: null,
|
2022-03-13 16:57:59 +01:00
|
|
|
requestTabs: [],
|
2022-03-17 05:11:27 +01:00
|
|
|
collectionsToSyncToIdb: []
|
2021-12-03 20:37:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export const StoreProvider = props => {
|
2022-01-18 15:58:18 +01:00
|
|
|
const [state, dispatch] = useReducer(reducer, initialState);
|
2022-03-13 13:13:21 +01:00
|
|
|
|
|
|
|
const {
|
2022-03-13 16:57:59 +01:00
|
|
|
collections,
|
|
|
|
idbConnection,
|
|
|
|
collectionsToSyncToIdb
|
2022-03-13 13:13:21 +01:00
|
|
|
} = state;
|
2022-01-18 15:58:18 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if(state.requestQueuedToSend) {
|
|
|
|
const {
|
|
|
|
request,
|
2022-03-13 22:13:36 +01:00
|
|
|
collectionUid
|
2022-01-18 15:58:18 +01:00
|
|
|
} = state.requestQueuedToSend;
|
|
|
|
|
2022-03-13 22:13:36 +01:00
|
|
|
sendRequest(request, collectionUid, dispatch)
|
2022-01-18 15:58:18 +01:00
|
|
|
}
|
|
|
|
}, [state.requestQueuedToSend]);
|
2021-12-03 20:37:38 +01:00
|
|
|
|
2022-02-03 18:55:38 +01:00
|
|
|
useIdb(dispatch);
|
2022-03-13 18:13:53 +01:00
|
|
|
useLoadCollectionsFromIdb(idbConnection, dispatch);
|
|
|
|
useSyncCollectionsToIdb(collectionsToSyncToIdb, collections, idbConnection, dispatch);
|
2022-02-03 18:55:38 +01:00
|
|
|
|
2021-12-03 20:37:38 +01:00
|
|
|
return <StoreContext.Provider value={[state, dispatch]} {...props} />;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useStore = () => {
|
|
|
|
const context = useContext(StoreContext);
|
|
|
|
|
|
|
|
if (context === undefined) {
|
|
|
|
throw new Error(`useStore must be used within a StoreProvider`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return context;
|
|
|
|
};
|
|
|
|
|
|
|
|
export default StoreProvider;
|