Skip to content

Commit

Permalink
Merge pull request CSID-DGU#75 from yebin-jeong/main
Browse files Browse the repository at this point in the history
[Feature] CSID-DGU#26 - url ์ˆ˜์ •
  • Loading branch information
yebin-jeong authored Dec 4, 2024
2 parents 844dbc8 + b4b908f commit ca0b362
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 128 deletions.
162 changes: 93 additions & 69 deletions src/views/akopolio/Edit/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const fetchUserData = async () => {
`${process.env.VUE_APP_BE_API_URL}/api/users/profile`,
{
method: 'GET',
credentials: 'include',
credentials: 'include',
}
);

Expand Down Expand Up @@ -45,13 +45,13 @@ export default {
const portfolioId = route.params.id;
const activityName = ref('');
const activityDate = ref('');
const selectedTags = ref([]);
const selectedTags = ref([]); // ์ดˆ๊ธฐ๊ฐ’ ๋ฐฐ์—ด๋กœ ์„ค์ •
const tags = ref([
'์ „๊ณต', '๊ต์–‘', '๊ต๋‚ด๋™์•„๋ฆฌ', '๊ต์™ธ๋™์•„๋ฆฌ', 'ํ•™ํšŒ', '๋ด‰์‚ฌํ™œ๋™', '์ธํ„ด', '์•„๋ฅด๋ฐ”์ดํŠธ',
'๋Œ€์™ธํ™œ๋™', '์„œํฌํ„ฐ์ฆˆ', '๊ธฐ์ž๋‹จ', '๊ฐ•์—ฐ/ํ–‰์‚ฌ', '์Šคํ„ฐ๋””', '๋ถ€ํŠธ์บ ํ”„', 'ํ”„๋กœ์ ํŠธ',
'์—ฐ๊ตฌ', 'ํ•™์ƒํšŒ', '๊ธฐํƒ€',
]);
const images = ref([]);
const images = ref([]); // ์ดˆ๊ธฐ๊ฐ’ ๋ฐฐ์—ด๋กœ ์„ค์ •
const star = ref({
situation: '',
task: '',
Expand All @@ -65,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 @@ -80,77 +78,69 @@ export default {
}
};

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

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

// Fetch ์š”์ฒญ
console.log('Fetching portfolio from URL:', apiUrl);

const response = await fetch(apiUrl);
console.log('Response status:', response.status); // ์ƒํƒœ ์ฝ”๋“œ ํ™•์ธ
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();

// ์‘๋‹ต ๋ฐ์ดํ„ฐ ํ™•์ธ
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 || [], // ์‚ฌ์ง„ URL ๋ฐฐ์—ด ์ €์žฅ
baseInfo: data.baseInfo || {},
experience: data.experience || { situation: '', task: '', action: '', result: '' },
pmi: data.pmi || { plus: '', minus: '', interesting: '' },
photoUrls: data.photoUrls || [],
};

// ์ด๋ฏธ์ง€ ๋ฐ์ดํ„ฐ ๋”ฐ๋กœ ์ €์žฅ
images.value = portfolio.value.photoUrls;


images.value = portfolio.value.photoUrls || [];
} catch (error) {
console.error('Error fetching portfolio:', error);
alert('ํฌํŠธํด๋ฆฌ์˜ค๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.');
portfolio.value = null; // ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ์ดˆ๊ธฐํ™”
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 @@ -162,95 +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 uploadImages = async () => {
const uploadedUrls = [];
for (const image of images.value) {
try {
const { data } = await axios.post(
`${process.env.VUE_APP_BE_API_URL}/file/IMAGE/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 uploadedUrl = data.url.split('?')[0];

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

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

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 (id) => {
if (!isFormComplete.value) {
alert('๋ชจ๋“  ํ•„๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.');
return;
}

try {
// ์ˆ˜์ •๋œ ํฌํŠธํด๋ฆฌ์˜ค ๋ฐ์ดํ„ฐ์™€ ์ด๋ฏธ์ง€ URL์„ ์„œ๋ฒ„์— ์ „์†ก
const response = await axios.put(
`${process.env.VUE_APP_BE_API_URL}/api/portfolios/${id}`, // id๋ฅผ URL์— ํฌํ•จ
`${process.env.VUE_APP_BE_API_URL}/api/portfolios/${id}`,
{
name: activityName.value, // ํ™œ๋™๋ช…
startDate: activityDate.value, // ํ™œ๋™ ์‹œ์ž‘์ผ
tags: selectedTags.value, // ์„ ํƒ๋œ ํƒœ๊ทธ๋“ค
experience: { // STAR ๋ฉ”์„œ๋“œ ์ ์šฉ
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: { // PMI ๋ฉ”์„œ๋“œ ์ ์šฉ
pmi: {
plus: pmi.value.plus,
minus: pmi.value.minus,
interesting: pmi.value.interesting,
},
photoUrls: images.value.map(image => image.name), // ์„œ๋ฒ„์— ์ „๋‹ฌํ•  ์ด๋ฏธ์ง€ ํŒŒ์ผ ์ด๋ฆ„๋งŒ ์ „๋‹ฌ
photoUrls: images.value.map(image => image.name),
},
{
headers: {
'Content-Type': 'application/json',
},
}
);

if (response.status === 200) {
alert('๋ฐ์ดํ„ฐ๊ฐ€ ์„ฑ๊ณต์ ์œผ๋กœ ์ €์žฅ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.');
} else {
alert('์ €์žฅ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.');
}

} catch (error) {
console.error('Error saving data:', error);
alert('๋ฐ์ดํ„ฐ ์ €์žฅ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.');
Expand All @@ -261,14 +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(async () => {
await fetchUserData(); // ์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ€์ ธ์˜ค๊ธฐ
await fetchPortfolioById(portfolioId); // ํฌํŠธํด๋ฆฌ์˜ค ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
images.value = [];
selectedTags.value = [];
await fetchUserData();
await fetchPortfolioById(portfolioId);
});
}}

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

0 comments on commit ca0b362

Please sign in to comment.