posts.tsx 1.29 KB
Newer Older
Kim, MinGyu's avatar
test    
Kim, MinGyu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React, { ReactNode, useState, useEffect } from "react";
import { Outlet, useLocation } from "react-router-dom";
import { postApi } from "../apis";
import { PostType } from "../types";

interface FormType {
  state: FormData;
}

export default function Posts() {
  const [posts, setPosts] = useState<PostType[]>([]);
  const location = useLocation() as FormType;
  const formdata = location.state;

  useEffect(() => {
    getDataList();
  }, []);

  //Read
  const getDataList = async () => {
    const res = await postApi.getData();
    setPosts(res);
  };

  //Create
  const createPost = async (filelist: FileList) => {
    if (!(filelist === undefined || filelist === null)) {
      if (filelist.length === 1) {
        formdata.append("picture", filelist?.[0]);

        const res = await postApi.createFileAndPost(formdata);

        return res;
      } else {
        for (var i = 0; i < filelist.length; i++) {
          formdata.append("picture", filelist?.[i]);
        }
        const res = await postApi.createFileAndPost(formdata);

        return res;
      }
    }
  };
  //Delete
  const deletePost = async (postId: string) => {
    const res = await postApi.deletePost(postId);
    return res;
  };
  console.log("sdfa", posts);
  return <Outlet context={{ posts, createPost, deletePost }} />;
}