-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add backend endpoint and move the logic to endpoints
- Loading branch information
1 parent
7f73e5b
commit d4298d5
Showing
8 changed files
with
192 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { queryTrpc } from 'app/trpc'; | ||
import { useCallback } from 'react'; | ||
import { useOfflineQueue } from 'app/hooks/offline'; | ||
import { useItemsUpdater } from './useItemsUpdater'; | ||
|
||
interface State { | ||
items?: Array<{ id: string }>; | ||
} | ||
|
||
export const useImportItem = () => { | ||
const utils = queryTrpc.useContext(); | ||
const { mutate } = queryTrpc.importItemsGlobal.useMutation(); | ||
const { isConnected, addOfflineRequest } = useOfflineQueue(); | ||
const updateItems = useItemsUpdater(); | ||
|
||
const handleImportNewItems = useCallback( | ||
(newItem) => { | ||
if (isConnected) { | ||
return mutate(newItem, { | ||
onSuccess: () => { | ||
utils.getItemsGlobally.invalidate(); | ||
}, | ||
}); | ||
} | ||
|
||
addOfflineRequest('addItemGlobal', newItem); | ||
|
||
updateItems((prevState: State = {}) => { | ||
const prevItems = Array.isArray(prevState.items) ? prevState.items : []; | ||
|
||
return { | ||
...prevState, | ||
items: [newItem, ...prevItems], | ||
}; | ||
}); | ||
}, | ||
[updateItems], | ||
); | ||
|
||
return { handleImportNewItems }; | ||
}; |
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,80 @@ | ||
import { type Context } from 'hono'; | ||
import { addItemGlobalService } from '../../services/item/item.service'; | ||
import { protectedProcedure } from '../../trpc'; | ||
import * as validator from '@packrat/validations'; | ||
import Papa from 'papaparse'; | ||
|
||
export const importItemsGlobal = async (c: Context) => { | ||
try { | ||
const { name, weight, quantity, unit, type, ownerId } = await c.req.json(); | ||
|
||
const item = await addItemGlobalService( | ||
name, | ||
weight, | ||
quantity, | ||
unit, | ||
type, | ||
ownerId, | ||
); | ||
return c.json({ item }, 200); | ||
} catch (error) { | ||
return c.json({ error: `Failed to add item: ${error.message}` }, 500); | ||
} | ||
}; | ||
|
||
export function importItemsGlobalRoute() { | ||
return protectedProcedure | ||
.input(validator.importItemsGlobal) | ||
.mutation(async (opts) => { | ||
const { content, ownerId } = opts.input; | ||
return new Promise((resolve, reject) => { | ||
Papa.parse(content, { | ||
header: true, | ||
complete: async function (results) { | ||
const expectedHeaders = [ | ||
'Name', | ||
'Weight', | ||
'Unit', | ||
'Quantity', | ||
'Category', | ||
]; | ||
const parsedHeaders = results.meta.fields; | ||
try { | ||
const allHeadersPresent = expectedHeaders.every((header) => | ||
parsedHeaders.includes(header), | ||
); | ||
if (!allHeadersPresent) { | ||
return reject( | ||
new Error( | ||
'CSV does not contain all the expected Item headers', | ||
), | ||
); | ||
} | ||
|
||
for (const [index, item] of results.data.entries()) { | ||
if ( | ||
index === results.data.length - 1 && | ||
Object.values(item).every((value) => value === '') | ||
) { | ||
continue; | ||
} | ||
|
||
await addItemGlobalService( | ||
item.Name, | ||
item.Weight, | ||
item.Quantity, | ||
item.Unit, | ||
item.Category, | ||
ownerId, | ||
opts.ctx.executionCtx, | ||
); | ||
} | ||
return resolve('items'); | ||
} catch (error) { | ||
return reject(new Error(`Failed to add items: ${error.message}`)); | ||
} | ||
}, | ||
}); | ||
}); | ||
}); | ||
} |
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