Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Minn-Choi committed Dec 4, 2024
2 parents b2aa4e7 + ca0b362 commit 3d953c7
Show file tree
Hide file tree
Showing 6 changed files with 472 additions and 249 deletions.
211 changes: 133 additions & 78 deletions src/views/akopolio/Edit/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,54 @@ import { ref, computed, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import axios from 'axios';

const nickname = ref('');

const fetchUserData = async () => {
try {
const response = await fetch(
`${process.env.VUE_APP_BE_API_URL}/api/users/profile`,
{
method: 'GET',
credentials: 'include',
}
);

if (response.ok) {
const data = await response.json();
nickname.value = data.nickname;
} else {
console.error('์‚ฌ์šฉ์ž ์ •๋ณด ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ์‹คํŒจ:', response.status, response.statusText);
if (response.status === 401) {
alert('์ธ์ฆ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ๋กœ๊ทธ์ธํ•ด์ฃผ์„ธ์š”.');
window.location.href = '/login';
} else {
alert('์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ ์‹คํŒจ. ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.');
}
}
} catch (error) {
console.error('์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ ์˜ค๋ฅ˜:', error);
alert('์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.');
}
};

export default {
components: {
MainHeader,
MainFooter
MainFooter,
},
setup() {
const route = useRoute();
const portfolio = ref(null);
const portfolioId = route.params.id;
const activityName = ref('');
const activityDate = ref('');
const selectedTags = ref([]);
const selectedTags = ref([]); // ์ดˆ๊ธฐ๊ฐ’ ๋ฐฐ์—ด๋กœ ์„ค์ •
const tags = ref([
'์ „๊ณต', '๊ต์–‘', '๊ต๋‚ด๋™์•„๋ฆฌ', '๊ต์™ธ๋™์•„๋ฆฌ', 'ํ•™ํšŒ', '๋ด‰์‚ฌํ™œ๋™', '์ธํ„ด', '์•„๋ฅด๋ฐ”์ดํŠธ',
'๋Œ€์™ธํ™œ๋™', '์„œํฌํ„ฐ์ฆˆ', '๊ธฐ์ž๋‹จ', '๊ฐ•์—ฐ/ํ–‰์‚ฌ', '์Šคํ„ฐ๋””', '๋ถ€ํŠธ์บ ํ”„', 'ํ”„๋กœ์ ํŠธ',
'์—ฐ๊ตฌ', 'ํ•™์ƒํšŒ', '๊ธฐํƒ€',
]);
const images = ref([
{ previewUrl: "https://via.placeholder.com/150" } // ์˜ˆ์‹œ ์ด๋ฏธ์ง€
]);
const images = ref([]); // ์ดˆ๊ธฐ๊ฐ’ ๋ฐฐ์—ด๋กœ ์„ค์ •
const star = ref({
situation: '',
task: '',
Expand All @@ -36,12 +65,10 @@ export default {
});
const isDropdownOpen = ref(false);

// ๋“œ๋กญ๋‹ค์šด ํ† ๊ธ€
const toggleDropdown = () => {
isDropdownOpen.value = !isDropdownOpen.value;
};

// ํƒœ๊ทธ ์„ ํƒ/ํ•ด์ œ
const toggleTag = (tag) => {
const index = selectedTags.value.indexOf(tag);
if (index > -1) {
Expand All @@ -51,62 +78,69 @@ export default {
}
};

// ํ…์ŠคํŠธ ์˜์—ญ ์ž๋™ ํฌ๊ธฐ ์กฐ์ •
const autoResize = (event) => {
const textarea = event.target;
textarea.style.height = 'auto';
textarea.style.height = `${textarea.scrollHeight}px`;
};

// ํฌํŠธํด๋ฆฌ์˜ค ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
const fetchPortfolioById = async () => {
const fetchPortfolioById = async (id) => {
try {
const response = await fetch(
`${process.env.VUE_APP_BE_API_URL}/api/portfolios/${portfolioId}`
);
const apiUrl = `${process.env.VUE_APP_BE_API_URL}/api/portfolios/${id}`;
console.log('Fetching portfolio from URL:', apiUrl);

const response = await fetch(apiUrl);
console.log('Response status:', response.status);

if (!response.ok) {
console.error(`Response status: ${response.status}, ${response.statusText}`);
throw new Error(`Failed to fetch portfolio: ${response.statusText}`);
}

const data = await response.json();
activityName.value = data.activityName || '';
activityDate.value = data.activityDate || '';
selectedTags.value = data.selectedTags || [];
star.value = data.star || { situation: '', task: '', action: '', result: '' };
pmi.value = data.pmi || { plus: '', minus: '', interesting: '' };
images.value = data.images || [];
console.log('Received portfolio data:', data);

portfolio.value = {
baseInfo: data.baseInfo || {},
experience: data.experience || { situation: '', task: '', action: '', result: '' },
pmi: data.pmi || { plus: '', minus: '', interesting: '' },
photoUrls: data.photoUrls || [],
};

images.value = portfolio.value.photoUrls || [];
} catch (error) {
console.error('Error fetching portfolio:', error);
alert('ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.');
alert('ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.');
portfolio.value = null;
images.value = [];
}
};

const handleFileChange = (event) => {
const selectedFiles = Array.from(event.target.files);
if (images.value.length + selectedFiles.length > 5) {
if (Array.isArray(images.value) && images.value.length + selectedFiles.length > 5) {
alert('์ตœ๋Œ€ 5๊ฐœ์˜ ์ด๋ฏธ์ง€๋งŒ ์—…๋กœ๋“œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.');
return;
}

const newImagesPromises = selectedFiles.map((file) => {
const previewUrl = URL.createObjectURL(file);
const imageElement = new Image();
imageElement.src = previewUrl;

return new Promise((resolve) => {
imageElement.onload = () => {
const aspectRatio = imageElement.width / imageElement.height;
let containerWidth, containerHeight;

if (aspectRatio > 1) {
containerWidth = '300px';
containerHeight = `${300 / aspectRatio}px`;
} else {
containerWidth = `${300 * aspectRatio}px`;
containerHeight = '300px';
}

resolve({
file,
name: file.name,
Expand All @@ -118,88 +152,106 @@ export default {
};
});
});

Promise.all(newImagesPromises).then((newImages) => {
const uniqueNewImages = newImages.filter(
(newImage) =>
!images.value.some(
(existingImage) => existingImage.previewUrl === newImage.previewUrl
)
);

if (uniqueNewImages.length === 0) {
alert('์ด๋ฏธ ์„ ํƒ๋œ ์ด๋ฏธ์ง€์ž…๋‹ˆ๋‹ค.');
return;
}

images.value = [...images.value, ...uniqueNewImages];
});
};

const removeImage = (index) => {
URL.revokeObjectURL(images.value[index].previewUrl);
images.value.splice(index, 1);
};

const prefix = 'images';

const uploadImages = async () => {
const uploadedUrls = [];
for (const image of images.value) {
try {
const { data } = await axios.post(
`${process.env.VUE_APP_BE_API_URL}/file/${prefix}/presigned-url`,
{
fileName: image.name,
fileType: image.file.type,
}
);

await axios.put(data.url, image.file, {
headers: { 'Content-Type': image.file.type },

try {
console.log("Starting image upload...");

for (const image of images.value) {
const apiUrl = `${process.env.VUE_APP_BE_API_URL}/file/IMAGE/presigned-url`;
console.log('Requesting presigned URL from:', apiUrl);

const response = await axios.post(apiUrl, {
imageName: image.file.name,
});

const presignedUrl = response.data;
console.log("Received presigned URL:", presignedUrl);

await axios.put(presignedUrl, image.file, {
headers: {
"Content-Type": image.file.type,
},
});

const uploadedUrl = data.url.split('?')[0];

console.log("Image uploaded successfully!");

const uploadedUrl = presignedUrl.split("?")[0];
uploadedUrls.push(uploadedUrl);
} catch (error) {
console.error('Error uploading image:', error);
alert('์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.');
console.log("Uploaded image URL:", uploadedUrl);
}

console.log("Uploaded image URLs:", uploadedUrls);

} catch (error) {
console.error("Error uploading image:", error);
alert("์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.");
}
};


// ์ €์žฅ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ์„œ๋ฒ„์— ๋ฐ˜์˜
const saveData = async () => {
const saveData = async (id) => {
if (!isFormComplete.value) {
alert('๋ชจ๋“  ํ•„๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
return;
}

try {
// ์ˆ˜์ •๋œ ํฌํŠธํด๋ฆฌ์˜ค ๋ฐ์ดํ„ฐ์™€ ์ด๋ฏธ์ง€ URL์„ ์„œ๋ฒ„์— ์ „์†ก
const response = await fetch(
`${process.env.VUE_APP_BE_API_URL}/api/portfolios/${portfolioId}`,
const response = await axios.put(
`${process.env.VUE_APP_BE_API_URL}/api/portfolios/${id}`,
{
name: activityName.value,
startDate: activityDate.value,
tags: selectedTags.value,
experience: {
situation: star.value.situation,
task: star.value.task,
action: star.value.action,
result: star.value.result,
},
pmi: {
plus: pmi.value.plus,
minus: pmi.value.minus,
interesting: pmi.value.interesting,
},
photoUrls: images.value.map(image => image.name),
},
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
activityName: activityName.value,
activityDate: activityDate.value,
selectedTags: selectedTags.value,
star: star.value,
pmi: pmi.value,
images: images.value.map(image => image.name), // ์„œ๋ฒ„์— ์ „๋‹ฌํ•  ์ด๋ฏธ์ง€ ํŒŒ์ผ ์ด๋ฆ„๋งŒ ์ „๋‹ฌ
}),
}
);

if (!response.ok) {
throw new Error(`Failed to save data: ${response.statusText}`);
if (response.status === 200) {
alert('๋ฐ์ดํ„ฐ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
} else {
alert('์ €์žฅ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.');
}

alert('๋ฐ์ดํ„ฐ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
} catch (error) {
console.error('Error saving data:', error);
alert('๋ฐ์ดํ„ฐ ์ €์žฅ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.');
Expand All @@ -210,34 +262,37 @@ export default {
return (
activityName.value &&
activityDate.value &&
selectedTags.value.length > 0 &&
Array.isArray(selectedTags.value) && selectedTags.value.length > 0 &&
Object.values(star.value).every((field) => field) &&
Object.values(pmi.value).every((field) => field)
);
});

// ์ปดํฌ๋„ŒํŠธ ๋งˆ์šดํŠธ ์‹œ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
onMounted(() => {
fetchPortfolioById();
onMounted(async () => {
images.value = [];
selectedTags.value = [];
await fetchUserData();
await fetchPortfolioById(portfolioId);
});

return {
portfolio,
activityName,
activityDate,
tags,
selectedTags,
isDropdownOpen,
tags,
images,
star,
pmi,
images,
isDropdownOpen,
toggleDropdown,
toggleTag,
autoResize,
handleFileChange,
removeImage,
uploadImages,
handleFileChange,
removeImage,
uploadImages,
saveData,
isFormComplete
isFormComplete,
};
}
},
};
Loading

0 comments on commit 3d953c7

Please sign in to comment.