11 StayBooking Client 1
11 StayBooking Client 1
Setup
-
Install VS Code (IDE), Node.js (JS runtime), Npm (package management, come with Node.js)
-
Create an empty project
Go somewhere on your computer that you want to create a folder for this project.
Run follow commands on command line one by one
Command Line1: npx create-react-app@latest staysbooking
npx: 这是一个npm(Node Package Manager)附带的工具,允许你运行npm包中的可执行文件而不需要全局安装这些包。npx会临时安装这些包,并在安装后立即运行它们。
create-react-app@latest: 这是用于创建React应用的官方脚手架工具。
@latest
指定使用最新版本的create-react-app。staysbookingfe: 这是你要创建的React应用的名称。在运行这条命令后,一个名为
staysbookingfe
的目录将被创建,并包含一个初始的React项目结构
Command Line2: cd staysbooking
Command Line3: npm start
npm start: 这是一个npm命令,用于启动在
package.json
文件中定义的启动脚本。在React应用中,npm start
通常会运行react-scripts start
命令,该命令启动一个开发服务器,并在默认的浏览器中打开应用。
Open your browser (if these commands doesn’t open it for you automatically), go to
http://localhost:3000/ you will see a defaut page.
- Install component library Ant Design
Run npm add antd at command line (No Error means installation successful)
On the TOP(first line) of /src/index.css file, add this line
@import '~antd/dist/antd.css';
in 2023: import "antd/dist/reset.css";
Communication with backend
https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX
Create src/utils.js
:
const domain = "the url you deployed your backend to";
export const login = (credential, asHost) => {
const loginUrl = `${domain}/authenticate/${asHost ? "host" : "guest"}`;
return fetch(loginUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(credential),
}).then((response) => {
if (response.status !== 200) {
throw Error("Fail to log in");
}
return response.json();
});
};
export const register = (credential, asHost) => {
const registerUrl = `${domain}/register/${asHost ? "host" : "guest"}`;
return fetch(registerUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(credential),
}).then((response) => {
if (response.status !== 200) {
throw Error("Fail to register");
}
});
};
export const getReservations = () => {
const authToken = localStorage.getItem("authToken");
const listReservationsUrl = `${domain}/reservations`;
return fetch(listReservationsUrl, {
headers: {
Authorization: `Bearer ${authToken}`,
},
}).then((response) => {
if (response.status !== 200) {
throw Error("Fail to get reservation list");
}
return response.json();
});
};
export const getStaysByHost = () => {
const authToken = localStorage.getItem("authToken");
const listStaysUrl = `${domain}/stays/`;
return fetch(listStaysUrl, {
headers: {
Authorization: `Bearer ${authToken}`,
},
}).then((response) => {
if (response.status !== 200) {
throw Error("Fail to get stay list");
}
return response.json();
});
};
export const searchStays = (query) => {
const authToken = localStorage.getItem("authToken");
const searchStaysUrl = new URL(`${domain}/search/`);
searchStaysUrl.searchParams.append("guest_number", query.guest_number);
searchStaysUrl.searchParams.append(
"checkin_date",
query.checkin_date.format("YYYY-MM-DD")
);
searchStaysUrl.searchParams.append(
"checkout_date",
query.checkout_date.format("YYYY-MM-DD")
);
searchStaysUrl.searchParams.append("lat", 37);
searchStaysUrl.searchParams.append("lon", -122);
return fetch(searchStaysUrl, {
headers: {
Authorization: `Bearer ${authToken}`,
},
}).then((response) => {
if (response.status !== 200) {
throw Error("Fail to search stays");
}
return response.json();
});
};
export const deleteStay = (stayId) => {
const authToken = localStorage.getItem("authToken");
const deleteStayUrl = `${domain}/stays/${stayId}`;
return fetch(deleteStayUrl, {
method: "DELETE",
headers: {
Authorization: `Bearer ${authToken}`,
},
}).then((response) => {
if (response.status !== 200) {
throw Error("Fail to delete stay");
}
});
};
export const bookStay = (data) => {
const authToken = localStorage.getItem("authToken");
const bookStayUrl = `${domain}/reservations`;
return fetch(bookStayUrl, {
method: "POST",
headers: {
Authorization: `Bearer ${authToken}`,
"Content-Type": "application/json",
},
body: JSON.stringify(data),
}).then((response) => {
if (response.status !== 200) {
throw Error("Fail to book reservation");
}
});
};
export const cancelReservation = (reservationId) => {
const authToken = localStorage.getItem("authToken");
const cancelReservationUrl = `${domain}/reservations/${reservationId}`;
return fetch(cancelReservationUrl, {
method: "DELETE",
headers: {
Authorization: `Bearer ${authToken}`,
},
}).then((response) => {
if (response.status !== 200) {
throw Error("Fail to cancel reservation");
}
});
};
export const getReservationsByStay = (stayId) => {
const authToken = localStorage.getItem("authToken");
const getReservationByStayUrl = `${domain}/stays/reservations/${stayId}`;
return fetch(getReservationByStayUrl, {
headers: {
Authorization: `Bearer ${authToken}`,
},
}).then((response) => {
if (response.status !== 200) {
throw Error("Fail to get reservations by stay");
}
return response.json();
});
};
export const uploadStay = (data) => {
const authToken = localStorage.getItem("authToken");
const uploadStayUrl = `${domain}/stays`;
return fetch(uploadStayUrl, {
method: "POST",
headers: {
Authorization: `Bearer ${authToken}`,
},
body: data,
}).then((response) => {
if (response.status !== 200) {
throw Error("Fail to upload stay");
}
});
};