Created
May 28, 2025 15:29
-
-
Save yehonatan56/85640dec214731bd365dd091629e9e14 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { useState } from 'react'; | |
| import { apiCall } from '../apiTasks/apiUtils.ts'; | |
| export default function useAddBookForm() { | |
| const [name, setName] = useState<string>(''); | |
| const [author, setAuthor] = useState<string>(''); | |
| const [description, setDescription] = useState<string>(''); | |
| const [image, setImage] = useState<string>(''); | |
| const [qrCode, setQrCode] = useState<string>(''); | |
| const [amount, setAmount] = useState<number>(1); | |
| const [error, setError] = useState<string | null>(null); | |
| const saveStep1Data = (name: string, author: string) => { | |
| if (!name || !author) { | |
| setError('all fields is required'); | |
| return false; | |
| } | |
| setName(name); | |
| setAuthor(author); | |
| }; | |
| const saveStep2Data = (description: string) => { | |
| if (!description) { | |
| setError('all fields is required'); | |
| return false; | |
| } | |
| setDescription(description); | |
| }; | |
| const saveStep3Data = (image: string) => { | |
| if (!image) { | |
| setError('all fields is required'); | |
| return false; | |
| } | |
| setImage(image); | |
| }; | |
| const saveStep4Data = (qrCode: string) => { | |
| if (!qrCode) { | |
| setError('all fields is required'); | |
| return false; | |
| } | |
| setQrCode(qrCode); | |
| }; | |
| const saveStep5Data = (amount: number) => { | |
| if (!amount || amount < 1) { | |
| setError('all fields is required'); | |
| return false; | |
| } | |
| setAmount(amount); | |
| }; | |
| const sendBookData = async () => { | |
| const bookData = { name, author, description, image, qrCode, amount }; | |
| console.log('Sending book data:', bookData); | |
| await apiCall('add book', null, bookData, { 'Content-Type': 'application/json', token: localStorage.getItem('token') }); | |
| }; | |
| return { saveStep1Data, saveStep2Data, saveStep3Data, saveStep4Data, saveStep5Data, sendBookData, error }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment