import React from "react";
import { LandingWrapper } from "./LandingWrapper";
import { GlobalContext } from "@/context/Global";
import MkdSDK from "@/utils/MkdSDK";

const sdk = new MkdSDK();

export const MkdTrello = () => {
  const [workspaces, setWorkspaces] = React.useState([]);
  const { dispatch: globalDispatch } = React.useContext(GlobalContext);

  const getWorkspaces = async () => {
    try {
      const res = await sdk.getAllWorkspaces();
      setWorkspaces(res.list);
      globalDispatch({
        type: "SETWORKSPACES",
        payload: {
          workspaces: workspaces,
        },
      });
    } catch (err) {
      console.log("error" + " " + err);
    }
  };

  React.useEffect(() => {
    getWorkspaces();
  }, []);

  if (!!workspaces.length) {
    return <LandingWrapper list={workspaces} setWorkspaces={setWorkspaces} />;
  }
};
