-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7fd2d7e
commit 756c69e
Showing
4 changed files
with
67 additions
and
35 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
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 |
---|---|---|
@@ -1,25 +1,19 @@ | ||
'use client'; | ||
import React from 'react'; | ||
import { useSearchParams } from 'next/navigation'; | ||
import { Field } from './Field'; | ||
import { decode } from 'encoder'; | ||
import { useUrlState } from './useUrlState'; | ||
import { form } from './form' | ||
|
||
export const Status = ({ className }: { className?: string }) => { | ||
const searchParams = useSearchParams(); | ||
const state = getObj(searchParams); | ||
const { parse } = useUrlState(form) | ||
|
||
return ( | ||
<Field className={className}> | ||
<h2 className='text-semi-bold text-lg'>Types of data are presered</h2> | ||
<pre className="h-full p-2 rounded-sm bg-slate-100 | ||
dark:text-black text-wrap break-words whitespace-pre-wrap self-stretch"> | ||
{JSON.stringify(state, null, 2)} | ||
{JSON.stringify(parse(), null, 2)} | ||
</pre> | ||
</Field> | ||
); | ||
}; | ||
|
||
const getObj = (params: URLSearchParams) => | ||
Object.fromEntries( | ||
[...params.entries()].map(([key, value]) => [key, decode(value)]), | ||
); |
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,5 @@ | ||
export const form: {name: string, age: number | string, 'agree to terms': boolean} = { | ||
name: '', | ||
age: "", | ||
'agree to terms': false, | ||
}; |
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,41 @@ | ||
import { useSearchParams } from 'next/navigation'; | ||
import React from 'react' | ||
import { encode, decode } from 'encoder'; | ||
|
||
|
||
// TODO: tests | ||
export function useUrlState<T> (obj?: T) { | ||
const searchParams = useSearchParams(); | ||
|
||
const stringify = React.useCallback(function(state: T): string { | ||
const params = new URLSearchParams(); | ||
if (Array.isArray(state)) { | ||
state.forEach(([key, value]) => params.set(String(key), encode(value)) ) | ||
} else if (!!state && typeof state === 'object' ) { | ||
Object.entries(state).forEach(([key, value]) => { | ||
// TODO: fix ts | ||
const initialVal = (obj as typeof state)?.[key as keyof state] | ||
if (value !== initialVal ) { | ||
params.set(String(key), encode(value)); | ||
} else { | ||
params.delete(key) | ||
} | ||
}) | ||
} else { | ||
params.set('value', encode(state)) | ||
} | ||
|
||
return params.toString() | ||
}, []) | ||
|
||
const parse = React.useCallback(function() { | ||
// TODO: array, obj, primitive | ||
return {...obj, ...(Object.fromEntries( | ||
[...searchParams.entries()].map(([key, value]) => [ | ||
key, | ||
decode(value) || obj?.[key] | ||
])) as T)} | ||
}, [searchParams]) | ||
|
||
return { parse, stringify } | ||
} |