"use client";

import { useInterval } from "@/hooks/useInterval";
import { setPostLock } from "@/utils/store/features/data/post.lock";
import axios from "axios";
import React, { useCallback, useEffect } from "react";
import { useDispatch } from "react-redux";

const PostLock = ({ children }) => {
  const dispatch = useDispatch();

  const execute = useCallback(async () => {
    try {
      const response = await axios.get("/api/posts/lock");
      dispatch(setPostLock(response.data));
    } catch (error) {
      dispatch(setPostLock([]));
    }
  }, [dispatch]);

  useInterval(
    () => {
      execute();
    },
    5000,
    true
  );

  return <>{children}</>;
};

export default PostLock;
