mirror of
https://github.com/R0m1k3/Regisflow.git
synced 2026-10-18 04:39:30 +02:00
Create a new sales registry app with identity verification features
Initial commit of the React application with UI components, form, camera, and indexedDB integration for sale data management. Replit-Commit-Author: Agent Replit-Commit-Session-Id: 91318273-c764-4fd4-be04-bdc12c38af32 Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/0715008c-7244-40f4-befc-26c014852236/02867739-9ff7-4441-aca2-12e0153376c5.jpg
This commit is contained in:
1 parent
4969159ef9
commit
4fac1078a1
83 files changed
+16585
No files matched your search
@@ -0,0 +1,19 @@
|
||||
import * as React from "react"
|
||||
|
||||
const MOBILE_BREAKPOINT = 768
|
||||
|
||||
export function useIsMobile() {
|
||||
const [isMobile, setIsMobile] = React.useState<boolean | undefined>(undefined)
|
||||
|
||||
React.useEffect(() => {
|
||||
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`)
|
||||
const onChange = () => {
|
||||
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
|
||||
}
|
||||
mql.addEventListener("change", onChange)
|
||||
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
|
||||
return () => mql.removeEventListener("change", onChange)
|
||||
}, [])
|
||||
|
||||
return !!isMobile
|
||||
}
|
||||
@@ -0,0 +1,191 @@
|
||||
import * as React from "react"
|
||||
|
||||
import type {
|
||||
ToastActionElement,
|
||||
ToastProps,
|
||||
} from "@/components/ui/toast"
|
||||
|
||||
const TOAST_LIMIT = 1
|
||||
const TOAST_REMOVE_DELAY = 1000000
|
||||
|
||||
type ToasterToast = ToastProps & {
|
||||
id: string
|
||||
title?: React.ReactNode
|
||||
description?: React.ReactNode
|
||||
action?: ToastActionElement
|
||||
}
|
||||
|
||||
const actionTypes = {
|
||||
ADD_TOAST: "ADD_TOAST",
|
||||
UPDATE_TOAST: "UPDATE_TOAST",
|
||||
DISMISS_TOAST: "DISMISS_TOAST",
|
||||
REMOVE_TOAST: "REMOVE_TOAST",
|
||||
} as const
|
||||
|
||||
let count = 0
|
||||
|
||||
function genId() {
|
||||
count = (count + 1) % Number.MAX_SAFE_INTEGER
|
||||
return count.toString()
|
||||
}
|
||||
|
||||
type ActionType = typeof actionTypes
|
||||
|
||||
type Action =
|
||||
| {
|
||||
type: ActionType["ADD_TOAST"]
|
||||
toast: ToasterToast
|
||||
}
|
||||
| {
|
||||
type: ActionType["UPDATE_TOAST"]
|
||||
toast: Partial<ToasterToast>
|
||||
}
|
||||
| {
|
||||
type: ActionType["DISMISS_TOAST"]
|
||||
toastId?: ToasterToast["id"]
|
||||
}
|
||||
| {
|
||||
type: ActionType["REMOVE_TOAST"]
|
||||
toastId?: ToasterToast["id"]
|
||||
}
|
||||
|
||||
interface State {
|
||||
toasts: ToasterToast[]
|
||||
}
|
||||
|
||||
const toastTimeouts = new Map<string, ReturnType<typeof setTimeout>>()
|
||||
|
||||
const addToRemoveQueue = (toastId: string) => {
|
||||
if (toastTimeouts.has(toastId)) {
|
||||
return
|
||||
}
|
||||
|
||||
const timeout = setTimeout(() => {
|
||||
toastTimeouts.delete(toastId)
|
||||
dispatch({
|
||||
type: "REMOVE_TOAST",
|
||||
toastId: toastId,
|
||||
})
|
||||
}, TOAST_REMOVE_DELAY)
|
||||
|
||||
toastTimeouts.set(toastId, timeout)
|
||||
}
|
||||
|
||||
export const reducer = (state: State, action: Action): State => {
|
||||
switch (action.type) {
|
||||
case "ADD_TOAST":
|
||||
return {
|
||||
...state,
|
||||
toasts: [action.toast, ...state.toasts].slice(0, TOAST_LIMIT),
|
||||
}
|
||||
|
||||
case "UPDATE_TOAST":
|
||||
return {
|
||||
...state,
|
||||
toasts: state.toasts.map((t) =>
|
||||
t.id === action.toast.id ? { ...t, ...action.toast } : t
|
||||
),
|
||||
}
|
||||
|
||||
case "DISMISS_TOAST": {
|
||||
const { toastId } = action
|
||||
|
||||
// ! Side effects ! - This could be extracted into a dismissToast() action,
|
||||
// but I'll keep it here for simplicity
|
||||
if (toastId) {
|
||||
addToRemoveQueue(toastId)
|
||||
} else {
|
||||
state.toasts.forEach((toast) => {
|
||||
addToRemoveQueue(toast.id)
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
...state,
|
||||
toasts: state.toasts.map((t) =>
|
||||
t.id === toastId || toastId === undefined
|
||||
? {
|
||||
...t,
|
||||
open: false,
|
||||
}
|
||||
: t
|
||||
),
|
||||
}
|
||||
}
|
||||
case "REMOVE_TOAST":
|
||||
if (action.toastId === undefined) {
|
||||
return {
|
||||
...state,
|
||||
toasts: [],
|
||||
}
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
toasts: state.toasts.filter((t) => t.id !== action.toastId),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const listeners: Array<(state: State) => void> = []
|
||||
|
||||
let memoryState: State = { toasts: [] }
|
||||
|
||||
function dispatch(action: Action) {
|
||||
memoryState = reducer(memoryState, action)
|
||||
listeners.forEach((listener) => {
|
||||
listener(memoryState)
|
||||
})
|
||||
}
|
||||
|
||||
type Toast = Omit<ToasterToast, "id">
|
||||
|
||||
function toast({ ...props }: Toast) {
|
||||
const id = genId()
|
||||
|
||||
const update = (props: ToasterToast) =>
|
||||
dispatch({
|
||||
type: "UPDATE_TOAST",
|
||||
toast: { ...props, id },
|
||||
})
|
||||
const dismiss = () => dispatch({ type: "DISMISS_TOAST", toastId: id })
|
||||
|
||||
dispatch({
|
||||
type: "ADD_TOAST",
|
||||
toast: {
|
||||
...props,
|
||||
id,
|
||||
open: true,
|
||||
onOpenChange: (open) => {
|
||||
if (!open) dismiss()
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
id: id,
|
||||
dismiss,
|
||||
update,
|
||||
}
|
||||
}
|
||||
|
||||
function useToast() {
|
||||
const [state, setState] = React.useState<State>(memoryState)
|
||||
|
||||
React.useEffect(() => {
|
||||
listeners.push(setState)
|
||||
return () => {
|
||||
const index = listeners.indexOf(setState)
|
||||
if (index > -1) {
|
||||
listeners.splice(index, 1)
|
||||
}
|
||||
}
|
||||
}, [state])
|
||||
|
||||
return {
|
||||
...state,
|
||||
toast,
|
||||
dismiss: (toastId?: string) => dispatch({ type: "DISMISS_TOAST", toastId }),
|
||||
}
|
||||
}
|
||||
|
||||
export { useToast, toast }
|
||||
@@ -0,0 +1,102 @@
|
||||
import { useState, useRef, useCallback } from 'react';
|
||||
import { PhotoType } from '@/types/sale';
|
||||
|
||||
export function useCamera() {
|
||||
const [isOpen, setIsOpen] = useState(false);
|
||||
const [currentPhotoType, setCurrentPhotoType] = useState<PhotoType | null>(null);
|
||||
const [isCapturing, setIsCapturing] = useState(false);
|
||||
|
||||
const videoRef = useRef<HTMLVideoElement>(null);
|
||||
const canvasRef = useRef<HTMLCanvasElement>(null);
|
||||
const streamRef = useRef<MediaStream | null>(null);
|
||||
|
||||
const startCamera = useCallback(async (photoType: PhotoType) => {
|
||||
try {
|
||||
const stream = await navigator.mediaDevices.getUserMedia({
|
||||
video: { facingMode: 'environment' }
|
||||
});
|
||||
|
||||
streamRef.current = stream;
|
||||
setCurrentPhotoType(photoType);
|
||||
setIsOpen(true);
|
||||
|
||||
if (videoRef.current) {
|
||||
videoRef.current.srcObject = stream;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Camera error:', error);
|
||||
throw new Error('Impossible d\'accéder à la caméra');
|
||||
}
|
||||
}, []);
|
||||
|
||||
const capturePhoto = useCallback((): Promise<string> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!videoRef.current || !canvasRef.current) {
|
||||
reject(new Error('Video or canvas not available'));
|
||||
return;
|
||||
}
|
||||
|
||||
setIsCapturing(true);
|
||||
|
||||
const video = videoRef.current;
|
||||
const canvas = canvasRef.current;
|
||||
const context = canvas.getContext('2d');
|
||||
|
||||
if (!context) {
|
||||
reject(new Error('Canvas context not available'));
|
||||
return;
|
||||
}
|
||||
|
||||
canvas.width = video.videoWidth;
|
||||
canvas.height = video.videoHeight;
|
||||
|
||||
context.drawImage(video, 0, 0);
|
||||
|
||||
canvas.toBlob(
|
||||
(blob) => {
|
||||
if (!blob) {
|
||||
reject(new Error('Failed to create image blob'));
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = (e) => {
|
||||
setIsCapturing(false);
|
||||
resolve(e.target?.result as string);
|
||||
};
|
||||
reader.onerror = () => {
|
||||
setIsCapturing(false);
|
||||
reject(new Error('Failed to read image'));
|
||||
};
|
||||
reader.readAsDataURL(blob);
|
||||
},
|
||||
'image/jpeg',
|
||||
0.8
|
||||
);
|
||||
});
|
||||
}, []);
|
||||
|
||||
const stopCamera = useCallback(() => {
|
||||
if (streamRef.current) {
|
||||
streamRef.current.getTracks().forEach(track => track.stop());
|
||||
streamRef.current = null;
|
||||
}
|
||||
|
||||
setIsOpen(false);
|
||||
setCurrentPhotoType(null);
|
||||
setIsCapturing(false);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
isOpen,
|
||||
currentPhotoType,
|
||||
isCapturing,
|
||||
videoRef,
|
||||
canvasRef,
|
||||
startCamera,
|
||||
capturePhoto,
|
||||
stopCamera
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { Sale } from '@/types/sale';
|
||||
import { FireworksDB, createAutoBackup } from '@/lib/storage';
|
||||
|
||||
export function useIndexedDB() {
|
||||
const [db] = useState(() => new FireworksDB());
|
||||
const [sales, setSales] = useState<Sale[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const loadSales = useCallback(async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
const allSales = await db.getAllSales();
|
||||
setSales(allSales.sort((a, b) => new Date(b.dateVente).getTime() - new Date(a.dateVente).getTime()));
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
console.error('Error loading sales:', err);
|
||||
setError('Erreur lors du chargement des ventes');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [db]);
|
||||
|
||||
const saveSale = useCallback(async (saleData: Omit<Sale, 'id' | 'timestamp'>) => {
|
||||
try {
|
||||
const savedSale = await db.saveSale(saleData);
|
||||
setSales(prev => [savedSale, ...prev]);
|
||||
|
||||
// Create auto backup
|
||||
const updatedSales = await db.getAllSales();
|
||||
await createAutoBackup(updatedSales);
|
||||
|
||||
setError(null);
|
||||
return savedSale;
|
||||
} catch (err) {
|
||||
console.error('Error saving sale:', err);
|
||||
setError('Erreur lors de l\'enregistrement');
|
||||
throw err;
|
||||
}
|
||||
}, [db]);
|
||||
|
||||
const deleteSale = useCallback(async (id: number) => {
|
||||
try {
|
||||
await db.deleteSale(id);
|
||||
setSales(prev => prev.filter(sale => sale.id !== id));
|
||||
|
||||
// Update auto backup
|
||||
const updatedSales = await db.getAllSales();
|
||||
await createAutoBackup(updatedSales);
|
||||
|
||||
setError(null);
|
||||
} catch (err) {
|
||||
console.error('Error deleting sale:', err);
|
||||
setError('Erreur lors de la suppression');
|
||||
throw err;
|
||||
}
|
||||
}, [db]);
|
||||
|
||||
useEffect(() => {
|
||||
const initDB = async () => {
|
||||
try {
|
||||
await db.init();
|
||||
await loadSales();
|
||||
} catch (err) {
|
||||
console.error('Error initializing database:', err);
|
||||
setError('Erreur d\'initialisation de la base de données');
|
||||
setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
initDB();
|
||||
}, [db, loadSales]);
|
||||
|
||||
return {
|
||||
sales,
|
||||
isLoading,
|
||||
error,
|
||||
saveSale,
|
||||
deleteSale,
|
||||
loadSales
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user