Skip to content

Commit

Permalink
広告が変更&削除された時、minio上のオブジェクトも削除
Browse files Browse the repository at this point in the history
  • Loading branch information
Kubosaka committed Jul 28, 2024
1 parent aa61164 commit b97e543
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 23 deletions.
29 changes: 25 additions & 4 deletions view/next-project/src/components/sponsoractivities/DetailPage2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ const DetailPage2: FC<ModalProps> = (props) => {
props.setPageNum(1);
};
const [isOpen, setIsOpen] = useState<boolean>(false);
const [editActivityInformationId, setEditActivityInformationId] = useState<number>(0);
const [editActivityInformation, setEditActivityInformation] =
useState<SponsorActivityInformation>();
const [activityInformationData, setActivityInformationData] = useState<string>('');
const [isLoading, setIsLoading] = useState<boolean>(false);

Expand Down Expand Up @@ -82,6 +83,11 @@ const DetailPage2: FC<ModalProps> = (props) => {
};

const handleDelete = async (id: number, activityInformation: SponsorActivityInformation) => {
//オブジェクト削除
const formData = new FormData();
formData.append('fileName', `${activityInformation.fileName}`);
formData.append('year', year);

const deleteSponsorActivityInformationUrl =
process.env.CSR_API_URI + '/activity_informations/' + String(id);
const newSponsorActivityInformations = sponsorActivityInformations.filter(
Expand All @@ -92,6 +98,21 @@ const DetailPage2: FC<ModalProps> = (props) => {
} else {
const confirm = window.confirm('本当に削除してよろしいですか?');
if (confirm) {
const response = await fetch('/api/advertisements', {
method: 'DELETE',
body: formData,
})
.then((response) => {
if (response.ok) {
return true;
} else {
alert('削除に失敗');
return false;
}
})
.catch((error) => {
console.error('Error:', error);
});
const res = await del(deleteSponsorActivityInformationUrl);
} else {
window.alert('キャンセルしました');
Expand Down Expand Up @@ -327,7 +348,7 @@ const DetailPage2: FC<ModalProps> = (props) => {
<OutlinePrimaryButton
className='p-1'
onClick={() => {
setEditActivityInformationId(index);
setEditActivityInformation(activityInformation);
setIsOpen(true);
}}
>
Expand All @@ -348,7 +369,7 @@ const DetailPage2: FC<ModalProps> = (props) => {
<OutlinePrimaryButton
className='mx-auto my-2'
onClick={() => {
setEditActivityInformationId(index);
setEditActivityInformation(activityInformation);
setIsOpen(true);
}}
>
Expand All @@ -375,7 +396,7 @@ const DetailPage2: FC<ModalProps> = (props) => {
<UplaodFileModal
setIsOpen={setIsOpen}
id={props.sponsorActivitiesViewItem.sponsorActivity.id || 0}
ActivityInformationId={editActivityInformationId}
activityInformation={editActivityInformation}
sponsorActivityInformations={sponsorActivityInformations}
setSponsorActivityInformations={setSponsorActivityInformations}
setIsChange={props.setIsChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface ModalProps {
setIsOpen: (isOpen: boolean) => void;
children?: React.ReactNode;
id: number;
ActivityInformationId: number;
activityInformation?: SponsorActivityInformation;
year: string;
sponsorActivityInformations?: SponsorActivityInformation[];
setSponsorActivityInformations: (
Expand All @@ -20,21 +20,21 @@ interface ModalProps {
}

const UplaodFileModal: FC<ModalProps> = (props) => {
const { year } = props;
const { year, activityInformation } = props;
const fileInputRef = useRef<HTMLInputElement>(null);
const [imageFile, setImageFile] = useState<File | null>(null);
const [preview, setPreview] = useState({ uploadImageURL: '', type: '' });
const [activityInformation, setActivityInformation] = useState<SponsorActivityInformation>(
(props.sponsorActivityInformations &&
props.sponsorActivityInformations[props.ActivityInformationId]) || {
activityID: props.id,
bucketName: '',
fileName: '',
fileType: '',
designProgress: 1,
fileInformation: '',
},
);
const [registerActivityInformation, setRegisterActivityInformation] =
useState<SponsorActivityInformation>(
activityInformation || {
activityID: props.id,
bucketName: '',
fileName: '',
fileType: '',
designProgress: 1,
fileInformation: '',
},
);

const sponsorActivityInformations = props.sponsorActivityInformations || [];
// loadingの呼び出し
Expand All @@ -53,8 +53,8 @@ const UplaodFileModal: FC<ModalProps> = (props) => {
const fileName = targetFile.name;
const fileType = targetFile.type;

setActivityInformation({
...activityInformation,
setRegisterActivityInformation({
...registerActivityInformation,
bucketName: bucketName || '',
fileName: fileName,
fileType: fileType,
Expand All @@ -67,10 +67,37 @@ const UplaodFileModal: FC<ModalProps> = (props) => {
props.setIsOpen(false);
};

const objectDeleteHandle = async () => {
const formData = new FormData();
formData.append('fileName', `${activityInformation?.fileName}`);
formData.append('year', year);
const response = await fetch('/api/advertisements', {
method: 'DELETE',
body: formData,
})
.then((response) => {
if (response.ok) {
return true;
} else {
alert('削除に失敗');
return false;
}
})
.catch((error) => {
console.error('Error:', error);
});
};

const submit = async () => {
if (!imageFile) {
return;
}

//更新の場合削除
if (activityInformation?.fileName !== '') {
objectDeleteHandle();
}

setIsLoading(true);
const formData = new FormData();
formData.append('file', imageFile);
Expand Down Expand Up @@ -101,12 +128,12 @@ const UplaodFileModal: FC<ModalProps> = (props) => {
}

const sponsorActivitiesUrl =
process.env.CSR_API_URI + '/activity_informations/' + activityInformation.id;
const res = await put(sponsorActivitiesUrl, activityInformation);
process.env.CSR_API_URI + '/activity_informations/' + activityInformation?.id;
const res = await put(sponsorActivitiesUrl, registerActivityInformation);
const newSponsorActivityInformations = sponsorActivityInformations.map(
(sponsorActivityInformation) => {
if (sponsorActivityInformation.id === activityInformation.id) {
return activityInformation;
if (sponsorActivityInformation.id === registerActivityInformation.id) {
return registerActivityInformation;
}
return sponsorActivityInformation;
},
Expand Down
24 changes: 24 additions & 0 deletions view/next-project/src/pages/api/advertisements.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,30 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
return res.status(200).json({ message: '成功' });
});
}

// 変更の場合の画像を削除する
if (req.method === 'DELETE') {
const form = formidable();

form.parse(req, async (err, fields, files: any) => {
if (err) {
throw new Error('Error parsing form');
}

const year = fields.year && fields.year[0];
const fileName = fields.fileName && fields.fileName;
const filePath = `${year}/advertisements/${fileName}`;

try {
await minioClient.removeObject(BUCKET_NAME, filePath);
console.log('Removed the object');
} catch (err) {
res.status(400).json({ message: '失敗' });
throw new Error('Error uploading file (' + err + ')');
}
return res.status(200).json({ message: '成功' });
});
}
}

// バケットがない時に作成する関数(環境構築時のみ)
Expand Down

0 comments on commit b97e543

Please sign in to comment.