-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#281): allow to collapse/expand workflow version section (#282)
Signed-off-by: Nastya Rusina <[email protected]>
- Loading branch information
Showing
12 changed files
with
370 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
export enum LocalCacheItem { | ||
// Test flag is created only for unit-tests | ||
TestUndefined = 'test-undefined', | ||
TestSettingBool = 'test-setting-bool', | ||
TestObject = 'test-object', | ||
|
||
// Production flags | ||
ShowWorkflowVersions = 'flyte.show-workflow-versions' | ||
} | ||
|
||
type LocalCacheConfig = { [k: string]: string }; | ||
|
||
/* | ||
* THe default value could be present as any simple type or as a valid JSON object | ||
* with all field names wrapped in double quotes | ||
**/ | ||
export const defaultLocalCacheConfig: LocalCacheConfig = { | ||
// Test | ||
'test-setting-bool': 'false', | ||
'test-object': '{"name":"Stella","age":"125"}', | ||
|
||
// Production | ||
'flyte.show-workflow-versions': 'true' | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// More info on Local storage: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage | ||
import { useState } from 'react'; | ||
import { defaultLocalCacheConfig, LocalCacheItem } from './defaultConfig'; | ||
|
||
export { LocalCacheItem } from './defaultConfig'; | ||
|
||
export function ClearLocalCache() { | ||
localStorage.clear(); | ||
} | ||
|
||
const getDefault = (setting: LocalCacheItem) => { | ||
const result = defaultLocalCacheConfig[setting]; | ||
if (!result) { | ||
console.error( | ||
`ERROR: LocalCacheItem ${setting} doesn't have default value provided in defaultLocalCacheConfig` | ||
); | ||
return null; | ||
} | ||
return JSON.parse(result); | ||
}; | ||
|
||
export function useLocalCache<T>(setting: LocalCacheItem) { | ||
const defaultValue = getDefault(setting); | ||
const [value, setValue] = useState<T>(() => { | ||
const data = localStorage.getItem(setting); | ||
const value = data ? JSON.parse(data) : defaultValue; | ||
if (typeof value === typeof defaultValue) { | ||
return value; | ||
} | ||
|
||
return defaultValue; | ||
}); | ||
|
||
const setLocalCache = (newValue: T) => { | ||
localStorage.setItem(setting, JSON.stringify(newValue)); | ||
setValue(newValue); | ||
}; | ||
|
||
const clearState = () => { | ||
localStorage.removeItem(setting); | ||
setValue(defaultValue); | ||
}; | ||
|
||
return [value, setLocalCache, clearState]; | ||
} | ||
|
||
export const onlyForTesting = { | ||
getDefault | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import * as React from 'react'; | ||
import { | ||
render, | ||
screen, | ||
act, | ||
fireEvent, | ||
getByTestId | ||
} from '@testing-library/react'; | ||
import { | ||
ClearLocalCache, | ||
LocalCacheItem, | ||
useLocalCache, | ||
onlyForTesting | ||
} from '.'; | ||
|
||
const SHOW_TEXT = 'SHOWED'; | ||
const HIDDEN_TEXT = 'HIDDEN'; | ||
|
||
const TestFrame = () => { | ||
const [show, setShow, clearShow] = useLocalCache( | ||
LocalCacheItem.TestSettingBool | ||
); | ||
|
||
const toShow = () => setShow(true); | ||
const toClear = () => clearShow(); | ||
|
||
return ( | ||
<div> | ||
<div>{show ? SHOW_TEXT : HIDDEN_TEXT}</div> | ||
<button onClick={toShow} data-testid="show" /> | ||
<button onClick={toClear} data-testid="clear" /> | ||
</div> | ||
); | ||
}; | ||
|
||
describe('LocalCache', () => { | ||
beforeAll(() => { | ||
ClearLocalCache(); | ||
}); | ||
|
||
afterAll(() => { | ||
ClearLocalCache(); | ||
}); | ||
|
||
it('Can be used by component as expected', () => { | ||
const { container } = render(<TestFrame />); | ||
const show = getByTestId(container, 'show'); | ||
const clear = getByTestId(container, 'clear'); | ||
|
||
expect(screen.getByText(HIDDEN_TEXT)).toBeTruthy(); | ||
|
||
// change value | ||
act(() => { | ||
fireEvent.click(show); | ||
}); | ||
expect(screen.getByText(SHOW_TEXT)).toBeTruthy(); | ||
|
||
// reset to default | ||
act(() => { | ||
fireEvent.click(clear); | ||
}); | ||
expect(screen.getByText(HIDDEN_TEXT)).toBeTruthy(); | ||
}); | ||
|
||
it('With no default value - assumes null', () => { | ||
const { getDefault } = onlyForTesting; | ||
expect(getDefault(LocalCacheItem.TestUndefined)).toBeNull(); | ||
}); | ||
|
||
it('Can store use default as an object', () => { | ||
const { getDefault } = onlyForTesting; | ||
expect(getDefault(LocalCacheItem.TestObject)).toMatchObject({ | ||
name: 'Stella', | ||
age: '125' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.