-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
422 additions
and
30 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { GearItem } from "apiClient/gear"; | ||
import { useState } from "react"; | ||
|
||
import { PicturePickerModal } from "./PicturePickerModal"; | ||
import { PicturePlaceholder } from "./PicturePlaceholder"; | ||
import { faCamera } from "@fortawesome/free-solid-svg-icons"; | ||
import { faEdit } from "@fortawesome/free-solid-svg-icons"; | ||
|
||
import styled from "styled-components"; | ||
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; | ||
|
||
type Props = { | ||
gearItem: GearItem; | ||
refreshGear: () => void; | ||
}; | ||
|
||
export function GearPicture({ gearItem, refreshGear }: Props) { | ||
const [isModalOpen, setIsModalOpen] = useState<boolean>(false); | ||
|
||
return ( | ||
<> | ||
{gearItem.picture ? ( | ||
<PictureButton onClick={() => setIsModalOpen(true)}> | ||
<GearPic src={gearItem.picture} alt="Gear item" /> | ||
<TopRightIcon className="top-right-icon"> | ||
<FontAwesomeIcon icon={faEdit} /> | ||
</TopRightIcon> | ||
</PictureButton> | ||
) : ( | ||
<div className="border rounded-2 p-2 mb-3 bg-light"> | ||
<PicturePlaceholder | ||
onClick={() => setIsModalOpen(true)} | ||
icon={faCamera} | ||
text="Add picture" | ||
fontSize={3} | ||
/> | ||
</div> | ||
)} | ||
{isModalOpen && ( | ||
<PicturePickerModal | ||
isOpen={isModalOpen} | ||
close={() => setIsModalOpen(false)} | ||
item={gearItem} | ||
refreshGear={refreshGear} | ||
/> | ||
)} | ||
</> | ||
); | ||
} | ||
|
||
const PictureButton = styled.button` | ||
padding: 0; | ||
border: none; | ||
position: relative; | ||
&:hover .top-right-icon { | ||
opacity: 1; | ||
} | ||
`; | ||
|
||
const GearPic = styled.img` | ||
width: 100%; | ||
`; | ||
|
||
const TopRightIcon = styled.span` | ||
position: absolute; | ||
top: 0; | ||
right: 0; | ||
font-size: 3rem; | ||
padding: 1rem; | ||
opacity: 0; | ||
transition: opacity 0.3s ease; | ||
color: var(--bs-link-color); | ||
`; |
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,122 @@ | ||
import { GearItem, editGearItem } from "apiClient/gear"; | ||
import Modal from "react-bootstrap/Modal"; | ||
import { useGetGearTypePicturesQuery } from "redux/api"; | ||
import styled from "styled-components"; | ||
|
||
import { PicturePlaceholder } from "./PicturePlaceholder"; | ||
import { faUpload } from "@fortawesome/free-solid-svg-icons"; | ||
import { uploadFile } from "apiClient/client"; | ||
import { useRef, useState } from "react"; | ||
|
||
type Props = { | ||
isOpen: boolean; | ||
close: () => void; | ||
item: GearItem; | ||
refreshGear: () => void; | ||
}; | ||
|
||
export function PicturePickerModal({ | ||
isOpen, | ||
close, | ||
item, | ||
refreshGear, | ||
}: Props) { | ||
const { data: pictures, refetch: refetchPictures } = | ||
useGetGearTypePicturesQuery(item.type.id); | ||
const fileInputRef = useRef<HTMLInputElement | null>(null); | ||
const [selected, setSelected] = useState<string | undefined>(item.picture); | ||
|
||
return ( | ||
<Modal show={isOpen} onHide={close} size="xl"> | ||
<Modal.Header closeButton> | ||
<Modal.Title>Add picture for {item.id}</Modal.Title> | ||
</Modal.Header> | ||
<Modal.Body> | ||
<GridContainer> | ||
<div style={{ width: "200px" }}> | ||
<PicturePlaceholder | ||
icon={faUpload} | ||
onClick={() => { | ||
if (fileInputRef.current != null) { | ||
fileInputRef.current.click(); | ||
} | ||
}} | ||
text="Upload picture" | ||
size="200px" | ||
/> | ||
<input | ||
ref={fileInputRef} | ||
type="file" | ||
className="sr-only" | ||
onChange={(event) => { | ||
const file = event.target.files?.[0]; | ||
if (file == null) { | ||
return; | ||
} | ||
uploadFile(`/gear/${item.id}/picture/`, file).then(() => { | ||
refreshGear(); | ||
refetchPictures(); | ||
close(); | ||
}); | ||
}} | ||
accept="image/*" | ||
/> | ||
</div> | ||
{pictures?.map((pic) => ( | ||
<PicContainer | ||
key={pic} | ||
onClick={() => { | ||
setSelected(pic); | ||
}} | ||
selected={selected === pic} | ||
> | ||
<Pic src={pic} /> | ||
</PicContainer> | ||
))} | ||
</GridContainer> | ||
</Modal.Body> | ||
|
||
<Modal.Footer> | ||
<button className="btn btn-secondary" onClick={close}> | ||
Close | ||
</button> | ||
|
||
<button | ||
className="btn btn-primary" | ||
onClick={() => { | ||
editGearItem(item.id, { picture: selected }).then(() => { | ||
close(); | ||
refreshGear(); | ||
}); | ||
}} | ||
disabled={selected == null} | ||
> | ||
Save | ||
</button> | ||
</Modal.Footer> | ||
</Modal> | ||
); | ||
} | ||
|
||
const GridContainer = styled.div` | ||
display: grid; | ||
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); | ||
gap: 16px; | ||
`; | ||
|
||
const PicContainer = styled.button<{ selected?: boolean }>` | ||
width: 200px; | ||
height: 200px; | ||
padding: 0; | ||
outline: grey solid 1px; | ||
border: none; | ||
${({ selected }) => | ||
selected ? "outline: var(--bs-success) 6px solid !important" : ""} | ||
`; | ||
|
||
const Pic = styled.img` | ||
width: 100%; | ||
height: 100%; | ||
object-fit: cover; | ||
`; |
Oops, something went wrong.