Skip to content

Commit

Permalink
Merge pull request #278 from TripInfoWeb/dev_diary
Browse files Browse the repository at this point in the history
Refactor: 일기 등록 및 수정 API 재연동
  • Loading branch information
HyunJinNo authored Sep 9, 2024
2 parents 2d86ca7 + 58883e4 commit 4e3e4d1
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 10 deletions.
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/components/diary/write/DateRangeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const DateRangeModal = ({
rangeColors={["#00B488"]}
/>
<button
className="h-10 w-32 self-center rounded-full bg-main text-[0.9375rem] text-white hover:scale-105"
className="mt-4 h-10 w-32 self-center rounded-full bg-main text-[0.9375rem] text-white hover:scale-105"
type="button"
onClick={() => onChangeDateRange()}
>
Expand Down
54 changes: 46 additions & 8 deletions src/containers/diary/edit/DiaryEditorContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,29 @@ const DiaryEditorContainer = ({ diaryData }: Props) => {
const [dateRangeModal, setDateRangeModal] = useState<boolean>(false);
const [addressModal, setAddressModal] = useState<boolean>(false);
const [loading, setLoading] = useState<boolean>(false);
const methods = useForm({
const [originalThumbnailUrl, setOriginalThumbnailUrl] = useState<string>("");
const [originalContentUrl, setOriginalContentUrl] = useState<string[][]>([]);

const methods = useForm<{
userId: number;
title: string;
startDate: Date | null;
endDate: Date | null;
address: string[];
image: string;
moodLevels: number[];
contents: string[];
}>({
resolver: zodResolver(DiaryUpdateFormSchema),
defaultValues: {
userId: authStore.id,
title: "",
startDate: new Date(),
endDate: new Date(),
startDate: null,
endDate: null,
address: [""],
image: "",
moodLevels: [0],
contents: [""],
moodLevels: [],
contents: [],
},
mode: "onChange",
});
Expand All @@ -46,6 +58,14 @@ const DiaryEditorContainer = ({ diaryData }: Props) => {
.querySelector("img")
?.getAttribute("src") ?? "";

const contentImagesUrl = methods.getValues("contents").map((content) =>
parse(content)
.querySelectorAll("img")
.filter((img) => img.getAttribute("src") !== imageUrl)
.map((img) => img.getAttribute("src") ?? "")
.join(","),
);

if (imageUrl === "") {
alert("Day1에 최소 1장의 이미지를 등록해 주세요.");
return;
Expand All @@ -63,14 +83,25 @@ const DiaryEditorContainer = ({ diaryData }: Props) => {

const data: UpdateDiaryRequestDto = {
title: title,
titleImage: image,
startDatetime: startDate,
endDatetime: endDate,
deleteTitleImage: ![image, ...contentImagesUrl[0].split(",")].includes(
originalThumbnailUrl,
)
? originalThumbnailUrl
: "",
saveTitleImage: image,
startDatetime: startDate!,
endDatetime: endDate!,
diaryDayRequests: Array.from(
{ length: diaryEditorStore.days },
(_, index) => ({
content: sanitizeHtml(contents[index], sanitizeOption),
feelingStatus: FEELING_STATUS[moodLevels[index]],
deleteImagesUrl: originalContentUrl[index]
.filter(
(value) => !contentImagesUrl[index].split(",").includes(value),
)
.join(","),
saveImagesUrl: contentImagesUrl[index],
place: address[index],
}),
),
Expand Down Expand Up @@ -146,6 +177,13 @@ const DiaryEditorContainer = ({ diaryData }: Props) => {
),
);

setOriginalThumbnailUrl(diaryData.diaryContentResponse.titleImage);
setOriginalContentUrl(
diaryData.diaryContentResponse.diaryDayContentResponses.diaryDayContentDetail.map(
(value) => value.diaryDayContentImages.split(","),
),
);

// 화면에서 벗어났을 때 초기화
return () => {
diaryEditorStore.initialize();
Expand Down
9 changes: 9 additions & 0 deletions src/containers/diary/write/DiaryEditorContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ const DiaryEditorContainer = () => {
.querySelector("img")
?.getAttribute("src") ?? "";

const contentImagesUrl = methods.getValues("contents").map((content) =>
parse(content)
.querySelectorAll("img")
.filter((img) => img.getAttribute("src") !== imageUrl)
.map((img) => img.getAttribute("src") ?? "")
.join(","),
);

if (imageUrl === "") {
alert("Day1에 최소 1장의 이미지를 등록해 주세요.");
return;
Expand All @@ -77,6 +85,7 @@ const DiaryEditorContainer = () => {
(_, index) => ({
content: sanitizeHtml(contents[index], sanitizeOption),
feelingStatus: FEELING_STATUS[moodLevels[index]],
diaryDayContentImages: contentImagesUrl[index],
place: address[index],
}),
),
Expand Down
8 changes: 7 additions & 1 deletion src/types/DiaryDto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface CreateDiaryRequestDto {
diaryDayRequests: {
content: string;
feelingStatus: string;
diaryDayContentImages: string;
place: string;
}[];
}
Expand All @@ -27,6 +28,7 @@ export interface GetDiaryResponseDto {
diaryDayContentDetail: {
content: string;
feelingStatus: string;
diaryDayContentImages: string;
place: string;
}[];
};
Expand All @@ -47,6 +49,7 @@ export interface GetDiaryListResponseDto {
diaryDayContentDetail: {
content: string;
feelingStatus: string;
contentImage: string;
place: string;
}[];
};
Expand All @@ -61,12 +64,15 @@ export interface GetDiaryListResponseDto {
*/
export interface UpdateDiaryRequestDto {
title: string;
titleImage: string;
deleteTitleImage: string;
saveTitleImage: string;
startDatetime: Date;
endDatetime: Date;
diaryDayRequests: {
content: string;
feelingStatus: string;
deleteImagesUrl: string;
saveImagesUrl: string;
place: string;
}[];
}

0 comments on commit 4e3e4d1

Please sign in to comment.