Compare commits
No commits in common. "71a8dece86103c9f7179a705e741fc8777edea4a" and "f80f234b98dbd31d5613f15ec26c2d4c3e912efd" have entirely different histories.
71a8dece86
...
f80f234b98
13 changed files with 13 additions and 3906 deletions
|
|
@ -4,19 +4,16 @@ import { useRouter } from 'expo-router';
|
||||||
import { AppContainer, MyText, tw } from 'common-ui';
|
import { AppContainer, MyText, tw } from 'common-ui';
|
||||||
import TagForm from '@/src/components/TagForm';
|
import TagForm from '@/src/components/TagForm';
|
||||||
import { useCreateTag } from '@/src/api-hooks/tag.api';
|
import { useCreateTag } from '@/src/api-hooks/tag.api';
|
||||||
import { trpc } from '@/src/trpc-client';
|
|
||||||
|
|
||||||
interface TagFormData {
|
interface TagFormData {
|
||||||
tagName: string;
|
tagName: string;
|
||||||
tagDescription: string;
|
tagDescription: string;
|
||||||
isDashboardTag: boolean;
|
isDashboardTag: boolean;
|
||||||
relatedStores: number[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function AddTag() {
|
export default function AddTag() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { mutate: createTag, isPending: isCreating } = useCreateTag();
|
const { mutate: createTag, isPending: isCreating } = useCreateTag();
|
||||||
const { data: storesData } = trpc.admin.store.getStores.useQuery();
|
|
||||||
|
|
||||||
const handleSubmit = (values: TagFormData, image?: { uri?: string }) => {
|
const handleSubmit = (values: TagFormData, image?: { uri?: string }) => {
|
||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
|
|
@ -27,9 +24,6 @@ export default function AddTag() {
|
||||||
formData.append('tagDescription', values.tagDescription);
|
formData.append('tagDescription', values.tagDescription);
|
||||||
}
|
}
|
||||||
formData.append('isDashboardTag', values.isDashboardTag.toString());
|
formData.append('isDashboardTag', values.isDashboardTag.toString());
|
||||||
|
|
||||||
// Add related stores
|
|
||||||
formData.append('relatedStores', JSON.stringify(values.relatedStores));
|
|
||||||
|
|
||||||
// Add image if uploaded
|
// Add image if uploaded
|
||||||
if (image?.uri) {
|
if (image?.uri) {
|
||||||
|
|
@ -64,7 +58,6 @@ export default function AddTag() {
|
||||||
tagName: '',
|
tagName: '',
|
||||||
tagDescription: '',
|
tagDescription: '',
|
||||||
isDashboardTag: false,
|
isDashboardTag: false,
|
||||||
relatedStores: [],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
@ -77,7 +70,6 @@ export default function AddTag() {
|
||||||
initialValues={initialValues}
|
initialValues={initialValues}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
isLoading={isCreating}
|
isLoading={isCreating}
|
||||||
stores={storesData?.stores.map(store => ({ id: store.id, name: store.name })) || []}
|
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
</AppContainer>
|
</AppContainer>
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,11 @@ import { useRouter, useLocalSearchParams } from 'expo-router';
|
||||||
import { AppContainer, MyText, tw } from 'common-ui';
|
import { AppContainer, MyText, tw } from 'common-ui';
|
||||||
import TagForm from '@/src/components/TagForm';
|
import TagForm from '@/src/components/TagForm';
|
||||||
import { useGetTag, useUpdateTag } from '@/src/api-hooks/tag.api';
|
import { useGetTag, useUpdateTag } from '@/src/api-hooks/tag.api';
|
||||||
import { trpc } from '@/src/trpc-client';
|
|
||||||
|
|
||||||
interface TagFormData {
|
interface TagFormData {
|
||||||
tagName: string;
|
tagName: string;
|
||||||
tagDescription: string;
|
tagDescription: string;
|
||||||
isDashboardTag: boolean;
|
isDashboardTag: boolean;
|
||||||
relatedStores: number[];
|
|
||||||
existingImageUrl?: string;
|
existingImageUrl?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -21,7 +19,6 @@ export default function EditTag() {
|
||||||
|
|
||||||
const { data: tagData, isLoading: isLoadingTag, error: tagError } = useGetTag(tagIdNum!);
|
const { data: tagData, isLoading: isLoadingTag, error: tagError } = useGetTag(tagIdNum!);
|
||||||
const { mutate: updateTag, isPending: isUpdating } = useUpdateTag();
|
const { mutate: updateTag, isPending: isUpdating } = useUpdateTag();
|
||||||
const { data: storesData } = trpc.admin.store.getStores.useQuery();
|
|
||||||
|
|
||||||
const handleSubmit = (values: TagFormData, image?: { uri?: string }) => {
|
const handleSubmit = (values: TagFormData, image?: { uri?: string }) => {
|
||||||
if (!tagIdNum) return;
|
if (!tagIdNum) return;
|
||||||
|
|
@ -34,9 +31,6 @@ export default function EditTag() {
|
||||||
formData.append('tagDescription', values.tagDescription);
|
formData.append('tagDescription', values.tagDescription);
|
||||||
}
|
}
|
||||||
formData.append('isDashboardTag', values.isDashboardTag.toString());
|
formData.append('isDashboardTag', values.isDashboardTag.toString());
|
||||||
|
|
||||||
// Add related stores
|
|
||||||
formData.append('relatedStores', JSON.stringify(values.relatedStores));
|
|
||||||
|
|
||||||
// Add image if uploaded
|
// Add image if uploaded
|
||||||
if (image?.uri) {
|
if (image?.uri) {
|
||||||
|
|
@ -92,7 +86,6 @@ export default function EditTag() {
|
||||||
tagName: tag.tagName,
|
tagName: tag.tagName,
|
||||||
tagDescription: tag.tagDescription || '',
|
tagDescription: tag.tagDescription || '',
|
||||||
isDashboardTag: tag.isDashboardTag,
|
isDashboardTag: tag.isDashboardTag,
|
||||||
relatedStores: tag.relatedStores || [],
|
|
||||||
existingImageUrl: tag.imageUrl || undefined,
|
existingImageUrl: tag.imageUrl || undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -107,7 +100,6 @@ export default function EditTag() {
|
||||||
existingImageUrl={tag.imageUrl || undefined}
|
existingImageUrl={tag.imageUrl || undefined}
|
||||||
onSubmit={handleSubmit}
|
onSubmit={handleSubmit}
|
||||||
isLoading={isUpdating}
|
isLoading={isUpdating}
|
||||||
stores={storesData?.stores.map(store => ({ id: store.id, name: store.name })) || []}
|
|
||||||
/>
|
/>
|
||||||
</View>
|
</View>
|
||||||
</AppContainer>
|
</AppContainer>
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,6 @@ export interface CreateTagPayload {
|
||||||
tagDescription?: string;
|
tagDescription?: string;
|
||||||
imageUrl?: string;
|
imageUrl?: string;
|
||||||
isDashboardTag: boolean;
|
isDashboardTag: boolean;
|
||||||
relatedStores?: number[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface UpdateTagPayload {
|
export interface UpdateTagPayload {
|
||||||
|
|
@ -15,7 +14,6 @@ export interface UpdateTagPayload {
|
||||||
tagDescription?: string;
|
tagDescription?: string;
|
||||||
imageUrl?: string;
|
imageUrl?: string;
|
||||||
isDashboardTag: boolean;
|
isDashboardTag: boolean;
|
||||||
relatedStores?: number[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Tag {
|
export interface Tag {
|
||||||
|
|
@ -24,7 +22,6 @@ export interface Tag {
|
||||||
tagDescription: string | null;
|
tagDescription: string | null;
|
||||||
imageUrl: string | null;
|
imageUrl: string | null;
|
||||||
isDashboardTag: boolean;
|
isDashboardTag: boolean;
|
||||||
relatedStores: number[];
|
|
||||||
createdAt?: string;
|
createdAt?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,20 +3,14 @@ import { View, TouchableOpacity } from 'react-native';
|
||||||
import { Image } from 'expo-image';
|
import { Image } from 'expo-image';
|
||||||
import { Formik } from 'formik';
|
import { Formik } from 'formik';
|
||||||
import * as Yup from 'yup';
|
import * as Yup from 'yup';
|
||||||
import { MyTextInput, MyText, Checkbox, ImageUploader, tw, useFocusCallback, BottomDropdown } from 'common-ui';
|
import { MyTextInput, MyText, Checkbox, ImageUploader, tw, useFocusCallback } from 'common-ui';
|
||||||
import usePickImage from 'common-ui/src/components/use-pick-image';
|
import usePickImage from 'common-ui/src/components/use-pick-image';
|
||||||
import MaterialIcons from '@expo/vector-icons/MaterialIcons';
|
import MaterialIcons from '@expo/vector-icons/MaterialIcons';
|
||||||
|
|
||||||
interface StoreOption {
|
|
||||||
id: number;
|
|
||||||
name: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface TagFormData {
|
interface TagFormData {
|
||||||
tagName: string;
|
tagName: string;
|
||||||
tagDescription: string;
|
tagDescription: string;
|
||||||
isDashboardTag: boolean;
|
isDashboardTag: boolean;
|
||||||
relatedStores: number[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface TagFormProps {
|
interface TagFormProps {
|
||||||
|
|
@ -25,7 +19,6 @@ interface TagFormProps {
|
||||||
existingImageUrl?: string;
|
existingImageUrl?: string;
|
||||||
onSubmit: (values: TagFormData, image?: { uri?: string }) => void;
|
onSubmit: (values: TagFormData, image?: { uri?: string }) => void;
|
||||||
isLoading: boolean;
|
isLoading: boolean;
|
||||||
stores?: StoreOption[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const TagForm = forwardRef<any, TagFormProps>(({
|
const TagForm = forwardRef<any, TagFormProps>(({
|
||||||
|
|
@ -34,7 +27,6 @@ const TagForm = forwardRef<any, TagFormProps>(({
|
||||||
existingImageUrl = '',
|
existingImageUrl = '',
|
||||||
onSubmit,
|
onSubmit,
|
||||||
isLoading,
|
isLoading,
|
||||||
stores = [],
|
|
||||||
}, ref) => {
|
}, ref) => {
|
||||||
const [image, setImage] = useState<{ uri?: string } | null>(null);
|
const [image, setImage] = useState<{ uri?: string } | null>(null);
|
||||||
const [isDashboardTagChecked, setIsDashboardTagChecked] = useState<boolean>(Boolean(initialValues.isDashboardTag));
|
const [isDashboardTagChecked, setIsDashboardTagChecked] = useState<boolean>(Boolean(initialValues.isDashboardTag));
|
||||||
|
|
@ -128,27 +120,6 @@ const TagForm = forwardRef<any, TagFormProps>(({
|
||||||
<MyText style={tw`ml-3 text-gray-800`}>Mark as Dashboard Tag</MyText>
|
<MyText style={tw`ml-3 text-gray-800`}>Mark as Dashboard Tag</MyText>
|
||||||
</View>
|
</View>
|
||||||
|
|
||||||
{/* Related Stores Dropdown */}
|
|
||||||
<View style={tw`mb-6`}>
|
|
||||||
<MyText style={tw`text-lg font-bold mb-2 text-gray-800`}>
|
|
||||||
Related Stores
|
|
||||||
</MyText>
|
|
||||||
<BottomDropdown
|
|
||||||
label="Select Related Stores"
|
|
||||||
placeholder="Select stores..."
|
|
||||||
value={values.relatedStores.map(id => id.toString())}
|
|
||||||
options={stores.map(store => ({
|
|
||||||
label: store.name,
|
|
||||||
value: store.id.toString(),
|
|
||||||
}))}
|
|
||||||
onValueChange={(selectedValues) => {
|
|
||||||
const numericValues = (selectedValues as string[]).map(v => parseInt(v));
|
|
||||||
formikSetFieldValue('relatedStores', numericValues);
|
|
||||||
}}
|
|
||||||
multiple={true}
|
|
||||||
/>
|
|
||||||
</View>
|
|
||||||
|
|
||||||
<TouchableOpacity
|
<TouchableOpacity
|
||||||
onPress={() => handleSubmit()}
|
onPress={() => handleSubmit()}
|
||||||
disabled={isLoading}
|
disabled={isLoading}
|
||||||
|
|
|
||||||
|
|
@ -1 +0,0 @@
|
||||||
ALTER TABLE "mf"."product_tag_info" ADD COLUMN "related_stores" jsonb;
|
|
||||||
File diff suppressed because it is too large
Load diff
|
|
@ -498,13 +498,6 @@
|
||||||
"when": 1769958949864,
|
"when": 1769958949864,
|
||||||
"tag": "0070_known_ares",
|
"tag": "0070_known_ares",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
},
|
|
||||||
{
|
|
||||||
"idx": 71,
|
|
||||||
"version": "7",
|
|
||||||
"when": 1770321591876,
|
|
||||||
"tag": "0071_moaning_shadow_king",
|
|
||||||
"breakpoints": true
|
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
@ -5,13 +5,12 @@ import { eq } from "drizzle-orm";
|
||||||
import { ApiError } from "../lib/api-error";
|
import { ApiError } from "../lib/api-error";
|
||||||
import { imageUploadS3, generateSignedUrlFromS3Url } from "../lib/s3-client";
|
import { imageUploadS3, generateSignedUrlFromS3Url } from "../lib/s3-client";
|
||||||
import { deleteS3Image } from "../lib/delete-image";
|
import { deleteS3Image } from "../lib/delete-image";
|
||||||
import { initializeAllStores } from '../stores/store-initializer';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new product tag
|
* Create a new product tag
|
||||||
*/
|
*/
|
||||||
export const createTag = async (req: Request, res: Response) => {
|
export const createTag = async (req: Request, res: Response) => {
|
||||||
const { tagName, tagDescription, isDashboardTag, relatedStores } = req.body;
|
const { tagName, tagDescription, isDashboardTag } = req.body;
|
||||||
|
|
||||||
if (!tagName) {
|
if (!tagName) {
|
||||||
throw new ApiError("Tag name is required", 400);
|
throw new ApiError("Tag name is required", 400);
|
||||||
|
|
@ -34,18 +33,6 @@ export const createTag = async (req: Request, res: Response) => {
|
||||||
imageUrl = await imageUploadS3(req.file.buffer, req.file.mimetype, key);
|
imageUrl = await imageUploadS3(req.file.buffer, req.file.mimetype, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse relatedStores if it's a string (from FormData)
|
|
||||||
let parsedRelatedStores: number[] = [];
|
|
||||||
if (relatedStores) {
|
|
||||||
try {
|
|
||||||
parsedRelatedStores = typeof relatedStores === 'string'
|
|
||||||
? JSON.parse(relatedStores)
|
|
||||||
: relatedStores;
|
|
||||||
} catch (e) {
|
|
||||||
parsedRelatedStores = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const [newTag] = await db
|
const [newTag] = await db
|
||||||
.insert(productTagInfo)
|
.insert(productTagInfo)
|
||||||
.values({
|
.values({
|
||||||
|
|
@ -53,13 +40,9 @@ export const createTag = async (req: Request, res: Response) => {
|
||||||
tagDescription,
|
tagDescription,
|
||||||
imageUrl,
|
imageUrl,
|
||||||
isDashboardTag: isDashboardTag || false,
|
isDashboardTag: isDashboardTag || false,
|
||||||
relatedStores: parsedRelatedStores,
|
|
||||||
})
|
})
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
// Reinitialize stores to reflect changes in cache
|
|
||||||
await initializeAllStores();
|
|
||||||
|
|
||||||
return res.status(201).json({
|
return res.status(201).json({
|
||||||
tag: newTag,
|
tag: newTag,
|
||||||
message: "Tag created successfully",
|
message: "Tag created successfully",
|
||||||
|
|
@ -120,7 +103,7 @@ export const getTagById = async (req: Request, res: Response) => {
|
||||||
*/
|
*/
|
||||||
export const updateTag = async (req: Request, res: Response) => {
|
export const updateTag = async (req: Request, res: Response) => {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
const { tagName, tagDescription, isDashboardTag, relatedStores } = req.body;
|
const { tagName, tagDescription, isDashboardTag } = req.body;
|
||||||
|
|
||||||
// Get the current tag to check for existing image
|
// Get the current tag to check for existing image
|
||||||
const currentTag = await db.query.productTagInfo.findFirst({
|
const currentTag = await db.query.productTagInfo.findFirst({
|
||||||
|
|
@ -152,18 +135,6 @@ export const updateTag = async (req: Request, res: Response) => {
|
||||||
imageUrl = await imageUploadS3(req.file.buffer, req.file.mimetype, key);
|
imageUrl = await imageUploadS3(req.file.buffer, req.file.mimetype, key);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse relatedStores if it's a string (from FormData)
|
|
||||||
let parsedRelatedStores: number[] | undefined;
|
|
||||||
if (relatedStores !== undefined) {
|
|
||||||
try {
|
|
||||||
parsedRelatedStores = typeof relatedStores === 'string'
|
|
||||||
? JSON.parse(relatedStores)
|
|
||||||
: relatedStores;
|
|
||||||
} catch (e) {
|
|
||||||
parsedRelatedStores = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const [updatedTag] = await db
|
const [updatedTag] = await db
|
||||||
.update(productTagInfo)
|
.update(productTagInfo)
|
||||||
.set({
|
.set({
|
||||||
|
|
@ -171,14 +142,10 @@ export const updateTag = async (req: Request, res: Response) => {
|
||||||
tagDescription,
|
tagDescription,
|
||||||
imageUrl,
|
imageUrl,
|
||||||
isDashboardTag,
|
isDashboardTag,
|
||||||
relatedStores: parsedRelatedStores,
|
|
||||||
})
|
})
|
||||||
.where(eq(productTagInfo.id, parseInt(id)))
|
.where(eq(productTagInfo.id, parseInt(id)))
|
||||||
.returning();
|
.returning();
|
||||||
|
|
||||||
// Reinitialize stores to reflect changes in cache
|
|
||||||
await initializeAllStores();
|
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
tag: updatedTag,
|
tag: updatedTag,
|
||||||
message: "Tag updated successfully",
|
message: "Tag updated successfully",
|
||||||
|
|
@ -213,9 +180,6 @@ export const deleteTag = async (req: Request, res: Response) => {
|
||||||
// Note: This will fail if tag is still assigned to products due to foreign key constraint
|
// Note: This will fail if tag is still assigned to products due to foreign key constraint
|
||||||
await db.delete(productTagInfo).where(eq(productTagInfo.id, parseInt(id)));
|
await db.delete(productTagInfo).where(eq(productTagInfo.id, parseInt(id)));
|
||||||
|
|
||||||
// Reinitialize stores to reflect changes in cache
|
|
||||||
await initializeAllStores();
|
|
||||||
|
|
||||||
return res.status(200).json({
|
return res.status(200).json({
|
||||||
message: "Tag deleted successfully",
|
message: "Tag deleted successfully",
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -173,7 +173,6 @@ export const productTagInfo = mf.table('product_tag_info', {
|
||||||
tagDescription: varchar('tag_description', { length: 500 }),
|
tagDescription: varchar('tag_description', { length: 500 }),
|
||||||
imageUrl: varchar('image_url', { length: 500 }),
|
imageUrl: varchar('image_url', { length: 500 }),
|
||||||
isDashboardTag: boolean('is_dashboard_tag').notNull().default(false),
|
isDashboardTag: boolean('is_dashboard_tag').notNull().default(false),
|
||||||
relatedStores: jsonb('related_stores').$defaultFn(() => []),
|
|
||||||
createdAt: timestamp('created_at').notNull().defaultNow(),
|
createdAt: timestamp('created_at').notNull().defaultNow(),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,16 @@
|
||||||
// import redisClient from './redis-client';
|
// import redisClient from './redis-client';
|
||||||
import redisClient from 'src/lib/redis-client';
|
import redisClient from 'src/lib/redis-client';
|
||||||
import { db } from '../db/db_index';
|
import { db } from '../db/db_index';
|
||||||
import { productTagInfo, productTags } from '../db/schema';
|
import { productTagInfo } from '../db/schema';
|
||||||
import { eq, inArray } from 'drizzle-orm';
|
import { eq } from 'drizzle-orm';
|
||||||
import { generateSignedUrlFromS3Url } from 'src/lib/s3-client';
|
import { generateSignedUrlFromS3Url } from 'src/lib/s3-client';
|
||||||
|
|
||||||
// Tag Type (matches getDashboardTags return)
|
// Tag Type (matches getDashboardTags return)
|
||||||
interface Tag {
|
interface Tag {
|
||||||
id: number;
|
id: number;
|
||||||
tagName: string;
|
tagName: string;
|
||||||
tagDescription: string | null;
|
|
||||||
imageUrl: string | null;
|
imageUrl: string | null;
|
||||||
isDashboardTag: boolean;
|
isDashboardTag: boolean;
|
||||||
relatedStores: number[];
|
|
||||||
productIds: number[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function initializeProductTagStore(): Promise<void> {
|
export async function initializeProductTagStore(): Promise<void> {
|
||||||
|
|
@ -25,32 +22,11 @@ export async function initializeProductTagStore(): Promise<void> {
|
||||||
.select({
|
.select({
|
||||||
id: productTagInfo.id,
|
id: productTagInfo.id,
|
||||||
tagName: productTagInfo.tagName,
|
tagName: productTagInfo.tagName,
|
||||||
tagDescription: productTagInfo.tagDescription,
|
|
||||||
imageUrl: productTagInfo.imageUrl,
|
imageUrl: productTagInfo.imageUrl,
|
||||||
isDashboardTag: productTagInfo.isDashboardTag,
|
isDashboardTag: productTagInfo.isDashboardTag,
|
||||||
relatedStores: productTagInfo.relatedStores,
|
|
||||||
})
|
})
|
||||||
.from(productTagInfo);
|
.from(productTagInfo);
|
||||||
|
|
||||||
// Fetch product IDs for each tag
|
|
||||||
const tagIds = tagsData.map(t => t.id);
|
|
||||||
const productTagsData = await db
|
|
||||||
.select({
|
|
||||||
tagId: productTags.tagId,
|
|
||||||
productId: productTags.productId,
|
|
||||||
})
|
|
||||||
.from(productTags)
|
|
||||||
.where(inArray(productTags.tagId, tagIds));
|
|
||||||
|
|
||||||
// Group product IDs by tag
|
|
||||||
const productIdsByTag = new Map<number, number[]>();
|
|
||||||
for (const pt of productTagsData) {
|
|
||||||
if (!productIdsByTag.has(pt.tagId)) {
|
|
||||||
productIdsByTag.set(pt.tagId, []);
|
|
||||||
}
|
|
||||||
productIdsByTag.get(pt.tagId)!.push(pt.productId);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Store each tag in Redis
|
// Store each tag in Redis
|
||||||
for (const tag of tagsData) {
|
for (const tag of tagsData) {
|
||||||
const signedImageUrl = tag.imageUrl ? await generateSignedUrlFromS3Url(tag.imageUrl) : null;
|
const signedImageUrl = tag.imageUrl ? await generateSignedUrlFromS3Url(tag.imageUrl) : null;
|
||||||
|
|
@ -58,11 +34,8 @@ export async function initializeProductTagStore(): Promise<void> {
|
||||||
const tagObj: Tag = {
|
const tagObj: Tag = {
|
||||||
id: tag.id,
|
id: tag.id,
|
||||||
tagName: tag.tagName,
|
tagName: tag.tagName,
|
||||||
tagDescription: tag.tagDescription,
|
|
||||||
imageUrl: signedImageUrl,
|
imageUrl: signedImageUrl,
|
||||||
isDashboardTag: tag.isDashboardTag,
|
isDashboardTag: tag.isDashboardTag,
|
||||||
relatedStores: (tag.relatedStores as number[]) || [],
|
|
||||||
productIds: productIdsByTag.get(tag.id) || [],
|
|
||||||
};
|
};
|
||||||
|
|
||||||
await redisClient.set(`tag:${tag.id}`, JSON.stringify(tagObj));
|
await redisClient.set(`tag:${tag.id}`, JSON.stringify(tagObj));
|
||||||
|
|
@ -139,33 +112,4 @@ export async function getDashboardTags(): Promise<Tag[]> {
|
||||||
console.error('Error getting dashboard tags:', error);
|
console.error('Error getting dashboard tags:', error);
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
export async function getTagsByStoreId(storeId: number): Promise<Tag[]> {
|
|
||||||
try {
|
|
||||||
// Get all keys matching the pattern "tag:*"
|
|
||||||
const keys = await redisClient.KEYS('tag:*');
|
|
||||||
|
|
||||||
if (keys.length === 0) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get all tags using MGET for better performance
|
|
||||||
const tagsData = await redisClient.MGET(keys);
|
|
||||||
|
|
||||||
const storeTags: Tag[] = [];
|
|
||||||
for (const tagData of tagsData) {
|
|
||||||
if (tagData) {
|
|
||||||
const tag = JSON.parse(tagData) as Tag;
|
|
||||||
if (tag.relatedStores.includes(storeId)) {
|
|
||||||
storeTags.push(tag);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return storeTags;
|
|
||||||
} catch (error) {
|
|
||||||
console.error(`Error getting tags for store ${storeId}:`, error);
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,28 +0,0 @@
|
||||||
import { router, publicProcedure } from '../trpc-index';
|
|
||||||
import { z } from 'zod';
|
|
||||||
import { getTagsByStoreId } from '../../stores/product-tag-store';
|
|
||||||
import { ApiError } from '../../lib/api-error';
|
|
||||||
|
|
||||||
export const tagsRouter = router({
|
|
||||||
getTagsByStore: publicProcedure
|
|
||||||
.input(z.object({
|
|
||||||
storeId: z.number(),
|
|
||||||
}))
|
|
||||||
.query(async ({ input }) => {
|
|
||||||
const { storeId } = input;
|
|
||||||
|
|
||||||
// Get tags from cache that are related to this store
|
|
||||||
const tags = await getTagsByStoreId(storeId);
|
|
||||||
|
|
||||||
|
|
||||||
return {
|
|
||||||
tags: tags.map(tag => ({
|
|
||||||
id: tag.id,
|
|
||||||
tagName: tag.tagName,
|
|
||||||
tagDescription: tag.tagDescription,
|
|
||||||
imageUrl: tag.imageUrl,
|
|
||||||
productIds: tag.productIds,
|
|
||||||
})),
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
@ -12,7 +12,6 @@ import { userCouponRouter } from './coupon';
|
||||||
import { paymentRouter } from './payments';
|
import { paymentRouter } from './payments';
|
||||||
import { storesRouter } from './stores';
|
import { storesRouter } from './stores';
|
||||||
import { fileUploadRouter } from './file-upload';
|
import { fileUploadRouter } from './file-upload';
|
||||||
import { tagsRouter } from './tags';
|
|
||||||
|
|
||||||
export const userRouter = router({
|
export const userRouter = router({
|
||||||
address: addressRouter,
|
address: addressRouter,
|
||||||
|
|
@ -28,7 +27,6 @@ export const userRouter = router({
|
||||||
payment: paymentRouter,
|
payment: paymentRouter,
|
||||||
stores: storesRouter,
|
stores: storesRouter,
|
||||||
fileUpload: fileUploadRouter,
|
fileUpload: fileUploadRouter,
|
||||||
tags: tagsRouter,
|
|
||||||
});
|
});
|
||||||
|
|
||||||
export type UserRouter = typeof userRouter;
|
export type UserRouter = typeof userRouter;
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import React, { useState } from "react";
|
import React from "react";
|
||||||
import { View, Dimensions, ScrollView, TouchableOpacity } from "react-native";
|
import { View, Dimensions } from "react-native";
|
||||||
import { useRouter, useLocalSearchParams } from "expo-router";
|
import { useRouter, useLocalSearchParams } from "expo-router";
|
||||||
import {
|
import {
|
||||||
theme,
|
theme,
|
||||||
|
|
@ -19,46 +19,10 @@ import FloatingCartBar from "@/components/floating-cart-bar";
|
||||||
const { width: screenWidth } = Dimensions.get("window");
|
const { width: screenWidth } = Dimensions.get("window");
|
||||||
const itemWidth = (screenWidth - 48) / 2;
|
const itemWidth = (screenWidth - 48) / 2;
|
||||||
|
|
||||||
interface Tag {
|
|
||||||
id: number;
|
|
||||||
tagName: string;
|
|
||||||
productIds?: number[];
|
|
||||||
}
|
|
||||||
|
|
||||||
interface ChipProps {
|
|
||||||
tag: Tag;
|
|
||||||
isSelected: boolean;
|
|
||||||
onPress: () => void;
|
|
||||||
}
|
|
||||||
|
|
||||||
const Chip: React.FC<ChipProps> = ({ tag, isSelected, onPress }) => {
|
|
||||||
const productCount = tag.productIds?.length || 0;
|
|
||||||
|
|
||||||
return (
|
|
||||||
<TouchableOpacity
|
|
||||||
onPress={onPress}
|
|
||||||
style={tw`px-4 py-2 rounded-lg border ${
|
|
||||||
isSelected
|
|
||||||
? 'bg-brand500 border-brand500'
|
|
||||||
: 'bg-white border-brand500'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
<MyText
|
|
||||||
style={tw`font-medium text-sm ${
|
|
||||||
isSelected ? 'text-white' : 'text-brand500'
|
|
||||||
}`}
|
|
||||||
>
|
|
||||||
{tag.tagName} ({productCount})
|
|
||||||
</MyText>
|
|
||||||
</TouchableOpacity>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export default function StoreDetail() {
|
export default function StoreDetail() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const { id: storeId } = useLocalSearchParams();
|
const { id: storeId } = useLocalSearchParams();
|
||||||
const storeIdNum = parseInt(storeId as string);
|
const storeIdNum = parseInt(storeId as string);
|
||||||
const [selectedTagId, setSelectedTagId] = useState<number | null>(null);
|
|
||||||
|
|
||||||
const { data: storeData, isLoading, refetch, error } =
|
const { data: storeData, isLoading, refetch, error } =
|
||||||
trpc.user.stores.getStoreWithProducts.useQuery(
|
trpc.user.stores.getStoreWithProducts.useQuery(
|
||||||
|
|
@ -66,21 +30,6 @@ export default function StoreDetail() {
|
||||||
{ enabled: !!storeIdNum }
|
{ enabled: !!storeIdNum }
|
||||||
);
|
);
|
||||||
|
|
||||||
const { data: tagsData, isLoading: isLoadingTags } =
|
|
||||||
trpc.user.tags.getTagsByStore.useQuery(
|
|
||||||
{ storeId: storeIdNum },
|
|
||||||
{ enabled: !!storeIdNum }
|
|
||||||
);
|
|
||||||
|
|
||||||
// Filter products based on selected tag
|
|
||||||
const filteredProducts = selectedTagId
|
|
||||||
? storeData?.products.filter(product => {
|
|
||||||
const selectedTag = tagsData?.tags.find(t => t.id === selectedTagId);
|
|
||||||
return selectedTag?.productIds?.includes(product.id) ?? false;
|
|
||||||
}) || []
|
|
||||||
: storeData?.products || [];
|
|
||||||
|
|
||||||
|
|
||||||
useManualRefresh(() => {
|
useManualRefresh(() => {
|
||||||
refetch();
|
refetch();
|
||||||
});
|
});
|
||||||
|
|
@ -114,7 +63,7 @@ export default function StoreDetail() {
|
||||||
return (
|
return (
|
||||||
<View style={tw`flex-1 bg-gray-50 relative`}>
|
<View style={tw`flex-1 bg-gray-50 relative`}>
|
||||||
<MyFlatList
|
<MyFlatList
|
||||||
data={filteredProducts}
|
data={storeData?.products || []}
|
||||||
numColumns={2}
|
numColumns={2}
|
||||||
renderItem={({ item }) => (
|
renderItem={({ item }) => (
|
||||||
<ProductCard
|
<ProductCard
|
||||||
|
|
@ -160,44 +109,11 @@ export default function StoreDetail() {
|
||||||
</MyText>
|
</MyText>
|
||||||
)}
|
)}
|
||||||
</View>
|
</View>
|
||||||
{/* Tags Section */}
|
<View style={tw`flex-row items-center mt-6 mb-2`}>
|
||||||
{tagsData && tagsData.tags.length > 0 && (
|
<MaterialIcons name="grid-view" size={20} color="#374151" />
|
||||||
<ScrollView
|
<MyText style={tw`text-lg font-bold text-gray-900 ml-2`}>
|
||||||
horizontal
|
Products from this Store
|
||||||
showsHorizontalScrollIndicator={false}
|
</MyText>
|
||||||
contentContainerStyle={tw`gap-2 mt-6`}
|
|
||||||
>
|
|
||||||
{tagsData.tags.map((tag) => (
|
|
||||||
<Chip
|
|
||||||
key={tag.id}
|
|
||||||
tag={tag}
|
|
||||||
isSelected={selectedTagId === tag.id}
|
|
||||||
onPress={() => setSelectedTagId(selectedTagId === tag.id ? null : tag.id)}
|
|
||||||
/>
|
|
||||||
))}
|
|
||||||
</ScrollView>
|
|
||||||
)}
|
|
||||||
|
|
||||||
<View style={tw`flex-row items-center justify-between mt-6 mb-2`}>
|
|
||||||
<View style={tw`flex-row items-center`}>
|
|
||||||
<MaterialIcons name="grid-view" size={20} color="#374151" />
|
|
||||||
<MyText style={tw`text-lg font-bold text-gray-900 ml-2`}>
|
|
||||||
{selectedTagId
|
|
||||||
? `${tagsData?.tags.find(t => t.id === selectedTagId)?.tagName} items`
|
|
||||||
: `${filteredProducts.length} products`}
|
|
||||||
</MyText>
|
|
||||||
</View>
|
|
||||||
{selectedTagId && (
|
|
||||||
<TouchableOpacity
|
|
||||||
onPress={() => setSelectedTagId(null)}
|
|
||||||
style={tw`flex-row items-center`}
|
|
||||||
>
|
|
||||||
<MyText style={tw`text-brand500 text-sm font-medium mr-1`}>
|
|
||||||
Clear
|
|
||||||
</MyText>
|
|
||||||
<MaterialIcons name="close" size={16} color={theme.colors.brand500} />
|
|
||||||
</TouchableOpacity>
|
|
||||||
)}
|
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue