Skip to content

Commit

Permalink
fix(frontend): correct randomize store data persisting
Browse files Browse the repository at this point in the history
  • Loading branch information
MiracleHorizon committed Aug 5, 2024
1 parent df9ae1e commit c41cbac
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defaultSettings, mergeState } from '../store'
import { MAX_OPERATIONS } from '../constants'
import { mergeState } from '../store'
import { defaultSettings, MAX_OPERATIONS } from '../constants'

describe('ToolbarRandomizeMenu/store/mergeState', () => {
const defaultPayload = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
import type { Setting } from './types'

export const MAX_OPERATIONS = 6

export const defaultSettings: Setting[] = [
{
label: 'flip',
checked: true
},
{
label: 'flop',
checked: false
},
{
label: 'grayscale',
checked: false
},
{
label: 'negate',
checked: false
},
{
label: 'blur',
checked: true
},
{
label: 'rotate',
checked: true
},
{
label: 'modulate',
checked: false
},
{
label: 'gamma',
checked: true
},
{
label: 'tint',
checked: false
},
{
label: 'normalise',
checked: false
}
] as const
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { create } from 'zustand'
import { persist } from 'zustand/middleware'
import { array, boolean, object, string } from 'yup'

import { MAX_OPERATIONS } from './constants'
import { defaultSettings, MAX_OPERATIONS } from './constants'
import { SITE_TITLE } from '@site/config'
import { isSettingsValid } from './utility'
import type { Label, Setting } from './types'

/* eslint no-unused-vars: 0 */
Expand All @@ -16,49 +17,6 @@ interface Store {
toggleSettingChecked: (label: Label) => void
}

export const defaultSettings: Setting[] = [
{
label: 'flip',
checked: true
},
{
label: 'flop',
checked: false
},
{
label: 'grayscale',
checked: false
},
{
label: 'negate',
checked: false
},
{
label: 'blur',
checked: true
},
{
label: 'rotate',
checked: true
},
{
label: 'modulate',
checked: false
},
{
label: 'gamma',
checked: true
},
{
label: 'tint',
checked: false
},
{
label: 'normalise',
checked: false
}
] as const

export const useRandomizeStore = create(
persist<Store>(
(set, get) => ({
Expand Down Expand Up @@ -102,7 +60,7 @@ export const useRandomizeStore = create(
})
}),
{
name: 'scissors-randomize-settings',
name: `${SITE_TITLE.toLowerCase()}-randomize-settings`,
merge: mergeState
}
)
Expand Down Expand Up @@ -143,16 +101,3 @@ export function mergeState<State>(persistedState: unknown, currentState: State):
settings: defaultSettings
}
}

export const isSettingsValid = (settings: unknown): boolean => {
const optionSchema = object({
label: string()
.oneOf(defaultSettings.map(s => s.label))
.defined()
.required(),
checked: boolean().defined().required()
})
const settingsSchema = array(optionSchema).length(defaultSettings.length).defined().required()

return settingsSchema.isValidSync(settings)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { array, boolean, object, string } from 'yup'

import { defaultSettings } from './constants'

export const isSettingsValid = (settings: unknown): boolean => {
const optionSchema = object({
label: string()
.oneOf(defaultSettings.map(s => s.label))
.defined()
.required(),
checked: boolean().defined().required()
})
const settingsSchema = array(optionSchema).length(defaultSettings.length).defined().required()

return settingsSchema.isValidSync(settings)
}

0 comments on commit c41cbac

Please sign in to comment.