Database.hooks.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import { useAppDispatch, useAppSelector } from '../../stores/store';
  2. import { useEffect, useState } from 'react';
  3. import { databaseActions, IDatabase } from '../../stores/reducers/database/slice';
  4. import { nanoid } from 'nanoid';
  5. import { FieldType } from '../../../services/backend';
  6. export const useDatabase = () => {
  7. const dispatch = useAppDispatch();
  8. const database = useAppSelector((state) => state.database);
  9. const newField = () => {
  10. dispatch(
  11. databaseActions.addField({
  12. field: {
  13. fieldId: nanoid(8),
  14. fieldType: FieldType.RichText,
  15. fieldOptions: {},
  16. title: 'new field',
  17. },
  18. })
  19. );
  20. };
  21. const renameField = (fieldId: string, newTitle: string) => {
  22. const field = database.fields[fieldId];
  23. field.title = newTitle;
  24. dispatch(
  25. databaseActions.updateField({
  26. field,
  27. })
  28. );
  29. };
  30. const newRow = () => {
  31. dispatch(databaseActions.addRow());
  32. };
  33. return {
  34. database,
  35. newField,
  36. renameField,
  37. newRow,
  38. };
  39. };