Skip to content

Commit

Permalink
allows storing data for later recall
Browse files Browse the repository at this point in the history
  • Loading branch information
AreYouConfused committed Jan 2, 2025
1 parent 56eb92e commit 931b605
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 8 deletions.
54 changes: 46 additions & 8 deletions src/components/QR/QRModal.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import { Copy, QrCode } from 'lucide-react';
import { QRCodeSVG } from 'qrcode.react';
import { useMemo } from 'react';
import { getFieldValue, useQRScoutState } from '../../store/store';
import { useMemo, useState } from 'react';

Check failure on line 3 in src/components/QR/QRModal.tsx

View workflow job for this annotation

GitHub Actions / deploy

'useMemo' is declared but its value is never read.
import { getFieldValue, useQRScoutState, clearSaveData, } from '../../store/store';
import { Button } from '../ui/button';
import {
Dialog,
DialogContent,
DialogFooter,
DialogTitle,
DialogTrigger,
DialogClose

Check failure on line 12 in src/components/QR/QRModal.tsx

View workflow job for this annotation

GitHub Actions / deploy

'DialogClose' is declared but its value is never read.
} from '../ui/dialog';
import { PreviewText } from './PreviewText';
import { useSaveState, saveData } from '../../store/store';

export interface QRModalProps {
disabled?: boolean;
Expand All @@ -23,19 +25,42 @@ export function QRModal(props: QRModalProps) {
'matchNumber',
)}`.toUpperCase();

const qrCodeData = useMemo(
() => fieldValues.map(f => f.value).join(','),
[fieldValues],
);
const [stored, isStored] = useSaveState(state => [state.saveData, state.isSaveData]);
const [index, setCodeIndex] = useState(0);

const currentFormData = fieldValues.map(f => f.value).join(',')
if (index >= stored.length && index > 0){
setCodeIndex(0)
}
const qrCodeData = stored[index]

return (
<Dialog>
<Button
disabled={props.disabled}
onClick={() => saveData(currentFormData)}>
Save
</Button>
<DialogTrigger asChild>
<Button disabled={props.disabled}>
<Button
//disabled={props.disabled}
onClick={() => {console.log("dd");/*saveData(currentFormData);*/ setCodeIndex(0)}}
>
<QrCode className="size-5" />
Commit
Show QR
</Button>
</DialogTrigger>

<Button
disabled={!isStored}
onClick={() => {
if (!confirm("Clear saved QR codes?")){
return
}
setCodeIndex(0); clearSaveData()}}
>
Clear data
</Button>
<DialogContent>
<DialogTitle className="text-3xl text-primary text-center font-rhr-ns tracking-wider ">
{title}
Expand All @@ -47,6 +72,19 @@ export function QRModal(props: QRModalProps) {
<PreviewText data={qrCodeData} />
</div>
<DialogFooter>

<Button
variant="ghost"
onClick={() => setCodeIndex(index - 1)}
disabled={index <= 0}
>PREV CODE
</Button>
<Button
variant="ghost"
onClick={() => setCodeIndex(index + 1)}
disabled={index >= stored.length - 1}
>NEXT CODE
</Button>
<Button
variant="ghost"
onClick={() => navigator.clipboard.writeText(qrCodeData)}
Expand Down
34 changes: 34 additions & 0 deletions src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,37 @@ export function inputSelector<T extends InputBase>(
return field as T;
};
}

export interface SaveState {
saveData: Array<string>;
isSaveData: boolean;
}
const initialSaveState: SaveState = {
saveData: [],
isSaveData: false
};
export const useSaveState = createStore<SaveState>(
initialSaveState,
'saveData',
{
version: 1,
},
);
export function saveData(newData: string) {
var data = useSaveState.getState().saveData;
if (data[data.length - 1] == newData){
if (!confirm("Last two saved items are the same. Save anyway?")){
return
}
}
data.push(newData);
useSaveState.setState({ saveData: data, isSaveData: true });
}

export function getSaveData() {
return useSaveState.getState().saveData;
}

export function clearSaveData(){
useSaveState.setState({ saveData: [], isSaveData: false });
}

0 comments on commit 931b605

Please sign in to comment.