Skip to content

Commit

Permalink
[Format]
Browse files Browse the repository at this point in the history
  • Loading branch information
Najah7 committed Mar 15, 2024
1 parent db61151 commit 45d3264
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 170 deletions.
133 changes: 71 additions & 62 deletions src/hooks/useFetchMilestone.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
"use client"
"use client";
import axios from "axios";
const protobuf = require("protobufjs");

import { Milestone } from "../../proto/typescript/pb_out/main";
import mockMilestones from "@/sample-data/milestones.json";

const useFetchMilestone = () => {

const create = async (newMileStone: Milestone): Promise<Milestone|void> => {
const create = async (newMileStone: Milestone): Promise<Milestone | void> => {
// マイルストーンを新規作成する関数
// 新しいマイルストーンをサーバーに送信する (milestoneIdは空文字で送信)
// サーバー側で付与されたmilestoneID または milestone 丸ごとを返す
const token = localStorage.getItem("token");

const MilestoneCreateRequest = protobuf.roots.default.lookupType("main.MilestoneCreateRequest");

const MilestoneCreateRequest = protobuf.roots.default.lookupType(
"main.MilestoneCreateRequest",
);

const payload = { milestone: newMileStone };

const errMsg = MilestoneCreateRequest.verify(payload);
Expand All @@ -23,84 +24,92 @@ const useFetchMilestone = () => {
const milestone = MilestoneCreateRequest.create(payload);
const buffer = MilestoneCreateRequest.encode(milestone).finish();

await axios.post(
`${process.env.BACKEND_DOMAIN}/api/v1/milestone/create`,
buffer,
{
headers: {
Authorization: `token ${token}`,
},
}
)
.then((response) => {
const MilestoneCreateResponse = protobuf.roots.default.lookupType("main.MilestoneCreateResponse");
const res = MilestoneCreateResponse.decode(new Uint8Array(response.data));
return MilestoneCreateResponse.toObject(res);
})
.catch((error) => {
console.log(error);
})
}

await axios
.post(`${process.env.BACKEND_DOMAIN}/api/v1/milestone/create`, buffer, {
headers: {
Authorization: `token ${token}`,
},
})
.then((response) => {
const MilestoneCreateResponse = protobuf.roots.default.lookupType(
"main.MilestoneCreateResponse",
);
const res = MilestoneCreateResponse.decode(
new Uint8Array(response.data),
);
return MilestoneCreateResponse.toObject(res);
})
.catch((error) => {
console.log(error);
});
};

const update = async (newMileStone: Milestone): Promise<Milestone | void> => {
// マイルストーンを更新する関数
// 更新したいマイルストーンをサーバーに送信
// 更新されたマイルストーン または ステータス または void を返す
const token = localStorage.getItem("token");

const MilestoneUpdateRequest = protobuf.roots.default.lookupType("main.MilestoneUpdateRequest");
const MilestoneUpdateRequest = protobuf.roots.default.lookupType(
"main.MilestoneUpdateRequest",
);
const payload = { milestone: newMileStone };
const errMsg = MilestoneUpdateRequest.verify(payload);
if (errMsg) throw Error(`MilestoneUpdateRequest: ${errMsg}`);

const milestone = MilestoneUpdateRequest.create(payload);
const buffer = MilestoneUpdateRequest.encode(milestone).finish();
await axios.post(
`${process.env.BACKEND_DOMAIN}/api/v1/milestone/update`,
buffer,
{
headers: {
Authorization: `token ${token}`,
},
}
)
.then((response) => {
const MilestoneCreateResponse = protobuf.roots.default.lookupType("main.MilestoneCreateResponse");
const res = MilestoneCreateResponse.decode(new Uint8Array(response.data));
return res
})
.catch((error) => {
console.log(error);
})
}

await axios
.post(`${process.env.BACKEND_DOMAIN}/api/v1/milestone/update`, buffer, {
headers: {
Authorization: `token ${token}`,
},
})
.then((response) => {
const MilestoneCreateResponse = protobuf.roots.default.lookupType(
"main.MilestoneCreateResponse",
);
const res = MilestoneCreateResponse.decode(
new Uint8Array(response.data),
);
return res;
})
.catch((error) => {
console.log(error);
});
};

// because delete is a reserved word, we use _delete
const _delete = async (milestoneId: string): Promise<string | void> => {
// マイルストーンを削除する関数
// 指定されたmilestoneIdのマイルストーンを削除する
// ステータス または void を返す
const token = localStorage.getItem("token");

await axios.delete(
`${process.env.BACKEND_DOMAIN}/api/v1/milestone/delete${milestoneId}`,
{
await axios
.delete(
`${process.env.BACKEND_DOMAIN}/api/v1/milestone/delete${milestoneId}`,
{
headers: {
Authorization: `token ${token}`,
Authorization: `token ${token}`,
},
}
)
.then((response) => {
const MilestoneDeleteResponse = protobuf.roots.default.lookupType("main.MilestoneDeleteResponse");
const res = MilestoneDeleteResponse.decode(new Uint8Array(response.data));
return res
})
.catch((error) => {
console.log(error);
})

}
},
)
.then((response) => {
const MilestoneDeleteResponse = protobuf.roots.default.lookupType(
"main.MilestoneDeleteResponse",
);
const res = MilestoneDeleteResponse.decode(
new Uint8Array(response.data),
);
return res;
})
.catch((error) => {
console.log(error);
});
};

return {create, update, _delete};
}
return { create, update, _delete };
};

export default useFetchMilestone;
156 changes: 79 additions & 77 deletions src/hooks/useFetchUser.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,117 @@
"use client"
"use client";
import axios from "axios";
const protobuf = require("protobufjs");

import { UserData } from "../../proto/typescript/pb_out/main";

const useFetchUser = () => {
const get = async (userId: string): Promise<string | void> => {
const token = localStorage.getItem("token");

const get = async (userId: string): Promise<string | void > => {
const token = localStorage.getItem("token");

await axios.get(
`${process.env.BACKEND_DOMAIN}/api/v1/user/info/${userId}`,
{
headers: {
Authorization: `Bearer ${token}`,
},
}
)
.then((response) => {
const UserResponse = protobuf.roots.default.lookupType("main.UserResponse");
const res = UserResponse.decode(new Uint8Array(response.data));
return res
})
.catch((error) => {
console.log(error);
})
}
await axios
.get(`${process.env.BACKEND_DOMAIN}/api/v1/user/info/${userId}`, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.then((response) => {
const UserResponse =
protobuf.roots.default.lookupType("main.UserResponse");
const res = UserResponse.decode(new Uint8Array(response.data));
return res;
})
.catch((error) => {
console.log(error);
});
};

const list = async (): Promise<UserData[] | void> => {
const token = localStorage.getItem("token");

await axios.get(
await axios
.get(
`${process.env.BACKEND_DOMAIN}/api/v1/user/info`, // TODO: rename to /users
{
headers: {
Authorization: `Bearer ${token}`,
},
}
)
.then((response) => {
const UsersResponse = protobuf.roots.default.lookupType("main.UsersResponse");
const res = UsersResponse.decode(new Uint8Array(response.data));
return UsersResponse.toObject(res);
})
.catch((error) => {
console.log(error);
})
}

headers: {
Authorization: `Bearer ${token}`,
},
},
)
.then((response) => {
const UsersResponse =
protobuf.roots.default.lookupType("main.UsersResponse");
const res = UsersResponse.decode(new Uint8Array(response.data));
return UsersResponse.toObject(res);
})
.catch((error) => {
console.log(error);
});
};

const create = async (newUser: UserData): Promise<UserData | void> => {
const token = localStorage.getItem("token");

const UserCreateRequest = protobuf.roots.default.lookupType("main.UserCreateRequest");
const UserCreateRequest = protobuf.roots.default.lookupType(
"main.UserCreateRequest",
);
const payload = { user: newUser };
const errMsg = UserCreateRequest.verify(payload);
if (errMsg) throw Error(`UserCreateRequest: ${errMsg}`);

const user = UserCreateRequest.create(payload);
const buffer = UserCreateRequest.encode(user).finish();

await axios.post(
`${process.env.BACKEND_DOMAIN}/api/v1/user/create`,
buffer,
{
headers: {
Authorization: `Bearer ${token}`,
},
}
)
.then((response) => {
const UserCreateResponse = protobuf.roots.default.lookupType("main.UserCreateResponse");
const res = UserCreateResponse.decode(new Uint8Array(response.data));
return res
})
.catch((error) => {
await axios
.post(`${process.env.BACKEND_DOMAIN}/api/v1/user/create`, buffer, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.then((response) => {
const UserCreateResponse = protobuf.roots.default.lookupType(
"main.UserCreateResponse",
);
const res = UserCreateResponse.decode(new Uint8Array(response.data));
return res;
})
.catch((error) => {
console.log(error);
})
}
});
};

const update = async (newUser: UserData): Promise<UserData | void> => {
const token = localStorage.getItem("token");

const UserUpdateRequest = protobuf.roots.default.lookupType("main.UserUpdateRequest");
const UserUpdateRequest = protobuf.roots.default.lookupType(
"main.UserUpdateRequest",
);
const payload = { user: newUser };
const errMsg = UserUpdateRequest.verify(payload);

if (errMsg) throw Error(`UserUpdateRequest: ${errMsg}`);

const user = UserUpdateRequest.create(payload);
const buffer = UserUpdateRequest.encode(user).finish();

await axios.post(
`${process.env.BACKEND_DOMAIN}/api/v1/user/update`,
buffer,
{
headers: {
Authorization: `Bearer ${token}`,
},
}
)
.then((response) => {
const UserUpdateResponse = protobuf.roots.default.lookupType("main.UserUpdateResponse");
const res = UserUpdateResponse.decode(new Uint8Array(response.data));
return res
})
.catch((error) => {
await axios
.post(`${process.env.BACKEND_DOMAIN}/api/v1/user/update`, buffer, {
headers: {
Authorization: `Bearer ${token}`,
},
})
.then((response) => {
const UserUpdateResponse = protobuf.roots.default.lookupType(
"main.UserUpdateResponse",
);
const res = UserUpdateResponse.decode(new Uint8Array(response.data));
return res;
})
.catch((error) => {
console.log(error);
})
}
return {get, list, create, update};
}
});
};

return { get, list, create, update };
};

export default useFetchUser;
Loading

0 comments on commit 45d3264

Please sign in to comment.