-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
画像アップロード機能 Feature/#25 image upload feature #54
Merged
Merged
Changes from 3 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import {Alert, Breadcrumbs, Button, Link, Stack, Typography} from "@mui/material"; | ||
import CardBackground from "@/components/layout/cardBackground"; | ||
import {imageFactory} from "@/src/models/ImageModel"; | ||
import ImageEditor from "@/components/images/imageEditor"; | ||
|
||
export default async function ImageEditPage({params}: { params: { id: string } }) { | ||
const imageId = parseInt(params.id, 10) | ||
const image = await imageFactory().show(imageId) | ||
|
||
if (isNaN(imageId) || !image) { | ||
return ( | ||
<Stack spacing={1} mx={2} my={3}> | ||
<Alert severity="error"> | ||
<Typography>画像が削除されました。</Typography> | ||
</Alert> | ||
|
||
<Button variant="contained" href="/images/"> | ||
画像管理に戻る | ||
</Button> | ||
</Stack> | ||
) | ||
} | ||
|
||
return ( | ||
<Stack spacing={1} mx={2} my={3}> | ||
<Breadcrumbs aria-label="breadcrumb" sx={{pl: 2}}> | ||
<Link underline="hover" color="inherit" href="/"> | ||
管理者のダッシュボード | ||
</Link> | ||
<Link underline="hover" color="inherit" href={"/images/"}> | ||
画像管理 | ||
</Link> | ||
<Typography color="text.primary">{image.id}</Typography> | ||
</Breadcrumbs> | ||
|
||
<CardBackground title={`画像の情報`}> | ||
<ImageEditor image={image}/> | ||
</CardBackground> | ||
</Stack> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import {Stack, Breadcrumbs, Link, Typography} from "@mui/material"; | ||
import CardBackground from "@/components/layout/cardBackground"; | ||
import ImageCreator from "@/components/images/imageCreator"; | ||
|
||
export default function TagCreatePage() { | ||
|
||
return ( | ||
<Stack spacing={1} mx={2} my={3}> | ||
<Breadcrumbs aria-label="breadcrumb" sx={{pl: 2}}> | ||
<Link underline="hover" color="inherit" href="/"> | ||
管理者のダッシュボード | ||
</Link> | ||
<Link underline="hover" color="inherit" href={"/images/"}> | ||
画像管理 | ||
</Link> | ||
<Typography color="text.primary">画像作成</Typography> | ||
</Breadcrumbs> | ||
<CardBackground | ||
title={"画像作成"} | ||
> | ||
<ImageCreator/> | ||
</CardBackground> | ||
</Stack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import {Stack, Breadcrumbs, Link, Typography, Avatar, Button} from "@mui/material"; | ||
import CardBackground from "@/components/layout/cardBackground"; | ||
import {imageFactory} from "@/src/models/ImageModel"; | ||
import React from "react"; | ||
|
||
export default async function TagsPage() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const images = await imageFactory().index() | ||
|
||
return ( | ||
<Stack spacing={1} mx={2} my={3}> | ||
<Breadcrumbs aria-label="breadcrumb" sx={{pl: 2}}> | ||
<Link underline="hover" color="inherit" href="/"> | ||
管理者のダッシュボード | ||
</Link> | ||
<Typography color="text.primary">画像管理</Typography> | ||
</Breadcrumbs> | ||
<CardBackground | ||
title={"すべてのタグ"} | ||
button={"作成"} | ||
link={"/images/create"} | ||
> | ||
<Stack spacing={1}> | ||
{images?.map((image) => ( | ||
<Button variant={"contained"} fullWidth href={`/images/${image.id}`} key={image.id}> | ||
<Stack direction={"row"} spacing={2} alignItems={"center"} sx={{width:"100%"}}> | ||
<Avatar src={`${process.env.NEXT_PUBLIC_API_URL}/images/${image.id}/file`}/> | ||
<Typography> | ||
{image.id} | ||
</Typography> | ||
</Stack> | ||
</Button> | ||
))} | ||
</Stack> | ||
</CardBackground> | ||
</Stack> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
'use client' | ||
import {Avatar, Stack} from "@mui/material"; | ||
import {useRouter} from "next/navigation"; | ||
import React, {useState} from "react"; | ||
import {imageFactory} from "@/src/models/ImageModel"; | ||
|
||
export default function ImageCreator() { | ||
const router = useRouter() | ||
// State to store the file | ||
const [file, setFile] = useState<File | null>(null); | ||
|
||
// State to store the base64 | ||
const [base64, setBase64] = useState<string | null>(null); | ||
|
||
// When the file is selected, set the file state | ||
const onFileChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
if (!e.target.files) { | ||
return; | ||
} | ||
|
||
setFile(e.target.files[0]); | ||
}; | ||
|
||
// On click, clear the input value | ||
const onClick = (e: React.MouseEvent<HTMLInputElement>) => { | ||
e.currentTarget.value = ""; | ||
}; | ||
|
||
const toBase64 = (file: File) => { | ||
return new Promise((resolve, reject) => { | ||
const fileReader = new FileReader(); | ||
|
||
fileReader.readAsDataURL(file); | ||
|
||
fileReader.onload = () => { | ||
if (typeof fileReader.result === 'string') { | ||
// Split the data URL at the comma and take the second part (the actual Base64 data) | ||
const base64Data = fileReader.result.split(",")[1]; | ||
resolve(base64Data); | ||
} else { | ||
reject('FileReader result is not a string'); | ||
} | ||
}; | ||
|
||
fileReader.onerror = (error) => { | ||
reject(error); | ||
}; | ||
}); | ||
}; | ||
|
||
// On submit, upload the file | ||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
|
||
if (!file) { | ||
return; | ||
} | ||
|
||
// Convert the file to base64 | ||
const base64 = await toBase64(file as File); | ||
|
||
setBase64(base64 as string); | ||
|
||
// You can upload the base64 to your server here | ||
await imageFactory().create({ | ||
data: base64 as string, | ||
// attachment: imageBase64 | ||
}) | ||
router.push('/images') | ||
|
||
// Clear the states after upload | ||
setFile(null); | ||
setBase64(null); | ||
}; | ||
|
||
return ( | ||
<Stack | ||
spacing={1} | ||
> | ||
<form method="POST" encType="multipart/form-data" onSubmit={handleSubmit}> | ||
<input | ||
type="file" | ||
name="avatar" | ||
accept="image/*" | ||
onChange={onFileChange} | ||
onClick={onClick} | ||
/> | ||
<button type="submit">Upload</button> | ||
</form> | ||
{base64 && ( | ||
<Avatar src={base64}/> | ||
)} | ||
</Stack> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use client' | ||
import {Avatar, Button, Stack, Typography} from "@mui/material"; | ||
import {useRouter} from "next/navigation"; | ||
import React from "react"; | ||
import {imageFactory, Image} from "@/src/models/ImageModel"; | ||
|
||
export type ImageEditorProps = { | ||
image: Image | ||
} | ||
|
||
export default function ImageEditor(props: ImageEditorProps) { | ||
const router = useRouter() | ||
|
||
const handleSubmit = async () => { | ||
await imageFactory().delete(props.image.id); | ||
router.back() | ||
router.refresh() | ||
}; | ||
|
||
return ( | ||
<Stack | ||
spacing={1} | ||
> | ||
<Stack direction={"row"} spacing={2} alignItems={"center"}> | ||
<Avatar src={`${process.env.NEXT_PUBLIC_API_URL}/images/${props.image.id}/file`}/> | ||
<Typography> | ||
画像ID:{props.image.id} | ||
</Typography> | ||
</Stack> | ||
<Button variant={"contained"} onClick={handleSubmit}> | ||
削除 | ||
</Button> | ||
</Stack> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TagCreatePage
になってる...!