timetable-app

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

useDayPeriodDataInput.ts (670B)


      1 import { useState } from 'react';
      2 import { safeParse } from 'valibot';
      3 
      4 import { InputSchema } from '../schema';
      5 
      6 import type { InputSchemaSafeParseResult } from '../schema';
      7 import type { Dispatch, SetStateAction } from 'react';
      8 
      9 type Result = {
     10 	input: string;
     11 	setInput: Dispatch<SetStateAction<string>>;
     12 	parseResult: InputSchemaSafeParseResult;
     13 };
     14 
     15 const useDayPeriodDataInput = (): Result => {
     16 	const [value, setValue] = useState('');
     17 
     18 	const rows = value.split('\n').map((row) => row.split('\t'));
     19 
     20 	const result = safeParse(InputSchema, rows);
     21 
     22 	return {
     23 		input: value,
     24 		setInput: setValue,
     25 		parseResult: result,
     26 	};
     27 };
     28 
     29 export default useDayPeriodDataInput;