import axios from 'axios';
import { Instance, types } from 'mobx-state-tree';

export const Item = types.model({
  id: types.string,
  title: types.string,
});

export type IItem = Instance<typeof Item>;

export const List = types
  .model({
    list: types.array(Item),
    loaded: types.boolean,
  })
  .actions((self) => {
    return {
      setList: (data: any) => {
        self.list = data;
        self.loaded = true;
      },
    };
  });

export type IList = Instance<typeof List>;

export async function ListStoreLoadData(store: IList): Promise<void> {
  const url = `${process.env.REACT_APP_BACKEND}api/dashboards`;
  const resp = await axios.get(url);
  if (!resp?.data) return;
  store.setList(resp.data);
}
