Skip to content
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

fix: image put true if already true #378

Merged
merged 3 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading