Skip to content

Commit

Permalink
fix: image put true if already true (#378)
Browse files Browse the repository at this point in the history
Co-authored-by: Antoine Dufils <[email protected]>
Co-authored-by: Teddy Roncin <[email protected]>
Co-authored-by: Noé Landré <[email protected]>
  • Loading branch information
4 people authored Sep 13, 2024
1 parent 1022279 commit 31d63b3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
12 changes: 9 additions & 3 deletions src/components/dashboard/ItemModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const ItemModal = ({
const [endDate, setEndDate] = useState(item?.availableUntil || null);
const [quantity, setQuantity] = useState(item?.stock || null);
const [infos, setInfos] = useState(item?.infos || null);
const [image, setImage] = useState<File | null>(null);
const [logo, setLogo] = useState<File | null>(null);
const [display, setDisplay] = useState(item?.display || false);

const [attribute, setAttribute] = useState(item?.attribute || null);
Expand Down Expand Up @@ -57,6 +57,11 @@ const ItemModal = ({
<Button
primary
onClick={() => {
let image;
if (item?.image || logo) {
image = true;
}

const body = {
id: id ?? '',
name: name ?? '',
Expand All @@ -66,14 +71,15 @@ const ItemModal = ({
reducedPrice: reducedPrice ?? 0,
availableFrom: startDate ? startDate : null,
availableUntil: endDate ? endDate : null,
image: image ?? false,
// we update the stock through a difference between the current stock and the quantity in order to avoid conflicts if an order is made at the same time
left: quantity! - item!.stock! ?? item!.stock!,
infos: infos ? infos : null,
display: display ?? false,
} as AdminItem;

dispatch(
updateItem(body, image, () => {
updateItem(body, logo, () => {
onClose!();
}),
);
Expand Down Expand Up @@ -123,7 +129,7 @@ const ItemModal = ({
onChange={(value) => setQuantity(value as unknown as number)}
/>
<Textarea label="Description" value={infos ?? ''} onChange={setInfos} />
<FileInput label="Logo" value={item ? getItemImageLink(item.id) : ''} onChange={setImage} type={['png']} />
<FileInput label="Logo" value={item ? getItemImageLink(item.id) : ''} onChange={setLogo} type={['png']} />
<Checkbox label="Display" value={display} onChange={setDisplay} />
</>
</Modal>
Expand Down
8 changes: 4 additions & 4 deletions src/modules/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export const fetchAdminItems = (): AppThunk => async (dispatch) => {
};

export const updateItem =
(item: AdminItem, image: File | null, callback: () => void): AppThunk =>
(item: AdminItem, logo: File | null, callback: () => void): AppThunk =>
async (dispatch) => {
try {
const result = await API.patch(`admin/items/${item.id}`, {
Expand All @@ -290,11 +290,11 @@ export const updateItem =
availableFrom: item.availableFrom,
availableUntil: item.availableUntil,
display: item.display.toString(),
image: !!image,
image: item.image,
});

if (result && image) {
await uploadFile(image, getItemImageName(result.id), ITEM_FOLDER);
if (result && logo) {
await uploadFile(logo, getItemImageName(result.id), ITEM_FOLDER);
}

callback();
Expand Down

0 comments on commit 31d63b3

Please sign in to comment.