Skip to content

Commit

Permalink
fix: default formData
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinchappell committed Nov 27, 2024
1 parent 4ec8600 commit ff7b4c3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/lib/js/common/utils/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
CHILD_TYPE_MAP,
ANIMATION_SPEED_SLOW,
ANIMATION_SPEED_BASE,
DEFAULT_FORMDATA,
} from '../../constants.js'
import mergeWith from 'lodash/mergeWith.js'

Expand Down Expand Up @@ -340,5 +341,4 @@ export function parseData(data = Object.create(null)) {
return data
}


export const cleanFormData = formData => (formData ? clone(parseData(formData)) : {})
export const cleanFormData = formData => (formData ? clone(parseData(formData)) : DEFAULT_FORMDATA())
14 changes: 3 additions & 11 deletions src/lib/js/components/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import RowsData from './rows/index.js'
import ColumnsData from './columns/index.js'
import FieldsData from './fields/index.js'
import ExternalsData from './externals.js'
import { SESSION_FORMDATA_KEY, version } from '../constants.js'
import { DEFAULT_FORMDATA, SESSION_FORMDATA_KEY, version } from '../constants.js'

export const Stages = StagesData
export const Rows = RowsData
Expand All @@ -16,23 +16,15 @@ export const Fields = FieldsData
export const Controls = ControlsData
export const Externals = ExternalsData

const defaultFormData = () => ({
id: uuid(),
stages: { [uuid()]: {} },
rows: {},
columns: {},
fields: {},
})

const getFormData = (formData, useSessionStorage = false) => {
if (formData) {
return clone(parseData(formData))
}
if (useSessionStorage) {
return sessionStorage.get(SESSION_FORMDATA_KEY) || defaultFormData()
return sessionStorage.get(SESSION_FORMDATA_KEY) || DEFAULT_FORMDATA()
}

return defaultFormData()
return DEFAULT_FORMDATA()
}

export class Components extends Data {
Expand Down
13 changes: 12 additions & 1 deletion src/lib/js/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pkg from '../../../package.json' with { type: 'json' }
import { uuid } from './common/utils/index.mjs'

const isProd = import.meta.env?.PROD

Expand All @@ -12,7 +13,9 @@ export const POLYFILLS = [
{ name: 'mutationObserver', src: '//cdn.jsdelivr.net/npm/mutationobserver-shim/dist/mutationobserver.min.js' },
{ name: 'fetch', src: 'https://unpkg.com/unfetch/polyfill' },
]
export const SVG_SPRITE_URL = isProd ? `https://cdn.jsdelivr.net/npm/formeo@${version}/dist/${formeoSpriteId}.svg` : `assets/img/${formeoSpriteId}.svg`
export const SVG_SPRITE_URL = isProd
? `https://cdn.jsdelivr.net/npm/formeo@${version}/dist/${formeoSpriteId}.svg`
: `assets/img/${formeoSpriteId}.svg`
export const FALLBACK_SVG_SPRITE_URL = `https://draggable.github.io/formeo/assets/img/${formeoSpriteId}.svg`
export const CSS_URL = `https://cdn.jsdelivr.net/npm/formeo@${version}/dist/formeo.min.css`
export const FALLBACK_CSS_URL = 'https://draggable.github.io/formeo/assets/css/formeo.min.css'
Expand Down Expand Up @@ -199,3 +202,11 @@ export const UUID_REGEXP = /(\b[0-9a-f]{8}\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}
export const bsColRegExp = /\bcol-\w+-\d+/g

export const iconPrefix = 'f-i-'

export const DEFAULT_FORMDATA = () => ({
id: uuid(),
stages: { [uuid()]: {} },
rows: {},
columns: {},
fields: {},
})

0 comments on commit ff7b4c3

Please sign in to comment.