wip(lab): Working on preloading pattern settings
This commit is contained in:
parent
facce24fa8
commit
5cfcd2e274
3 changed files with 42 additions and 2 deletions
|
@ -1,13 +1,16 @@
|
|||
import Page from 'shared/components/wrappers/page.js'
|
||||
import useApp from 'site/hooks/useApp.js'
|
||||
import WorkbenchWrapper from 'shared/components/wrappers/workbench.js'
|
||||
import { useRouter } from 'next/router'
|
||||
|
||||
const WorkbenchPage = ({ pattern }) => {
|
||||
const app = useApp()
|
||||
const router = useRouter()
|
||||
const { preload, from } = router.query
|
||||
|
||||
return (
|
||||
<Page app={app} noLayout>
|
||||
<WorkbenchWrapper app={app} pattern={pattern} />
|
||||
<WorkbenchWrapper {...{ app, pattern, preload, from }} />
|
||||
</Page>
|
||||
)
|
||||
}
|
||||
|
|
28
packages/freesewing.shared/components/workbench/preload.js
Normal file
28
packages/freesewing.shared/components/workbench/preload.js
Normal file
|
@ -0,0 +1,28 @@
|
|||
import yaml from 'js-yaml'
|
||||
import axios from 'axios'
|
||||
|
||||
const preload = {
|
||||
github: async (id, pattern) => {
|
||||
let result
|
||||
try {
|
||||
result = await axios.get(`https://api.github.com/gists/${id}`)
|
||||
}
|
||||
catch (err) {
|
||||
console.log(err)
|
||||
return [false, 'An unexpected error occured']
|
||||
}
|
||||
|
||||
if (result.data.files['pattern.yaml'].content) {
|
||||
let g = yaml.load(result.data.files['pattern.yaml'].content)
|
||||
if (g.design !== pattern.config.name) return [
|
||||
false, `You tried loading a configuration for ${g.design} into a ${design} development environment`
|
||||
]
|
||||
|
||||
return g
|
||||
}
|
||||
else return [false, 'This gist does not seem to be a valid pattern configuration']
|
||||
}
|
||||
}
|
||||
|
||||
export default preload
|
||||
|
|
@ -7,6 +7,7 @@ import unset from 'lodash.unset'
|
|||
import defaultSettings from 'shared/components/workbench/default-settings.js'
|
||||
import DraftError from 'shared/components/workbench/draft/error.js'
|
||||
import theme from 'pkgs/plugin-theme/src/index.js'
|
||||
import preloaders from 'shared/components/workbench/preload.js'
|
||||
|
||||
// Views
|
||||
import Measurements from 'shared/components/workbench/measurements/index.js'
|
||||
|
@ -57,7 +58,7 @@ const hasRequiredMeasurements = (pattern, gist) => {
|
|||
* keeping the gist state, which will trickly down
|
||||
* to all workbench subcomponents
|
||||
*/
|
||||
const WorkbenchWrapper = ({ app, pattern }) => {
|
||||
const WorkbenchWrapper = ({ app, pattern, preload=false, from=false }) => {
|
||||
|
||||
// State for gist
|
||||
const [gist, setGist] = useLocalStorage(`${pattern.config.name}_gist`, defaultGist(pattern, app.locale))
|
||||
|
@ -71,6 +72,14 @@ const WorkbenchWrapper = ({ app, pattern }) => {
|
|||
) updateGist(['_state', 'view'], 'measurements')
|
||||
})
|
||||
|
||||
// If we need to preload the gist, do so
|
||||
useEffect(async () => {
|
||||
if (preload && from && preloaders[from]) {
|
||||
const g = await preloaders[from](preload, pattern)
|
||||
// FIXME: Continue here
|
||||
}
|
||||
}, [preload, from])
|
||||
|
||||
// Helper methods to manage the gist state
|
||||
const updateGist = (path, content) => {
|
||||
const newGist = {...gist}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue