From 4be030ec28989b86ef2d10bdd8fe7bea2901673a Mon Sep 17 00:00:00 2001 From: toqvist Date: Wed, 3 Apr 2024 12:18:03 +0200 Subject: [PATCH] Import payload config --- astro/src/components/AddRetailerForm.tsx | 2 +- astro/src/types.ts | 60 ++++++++++++ payload/src/collections/Couriers.ts | 45 +++++++++ payload/src/collections/Dispatches.ts | 95 +++++++++++++++++++ payload/src/collections/Makers.ts | 34 +++++++ payload/src/collections/Products.ts | 20 ++++ payload/src/collections/Retailers.ts | 36 +++++++ .../src/customFields/geoPicker/GeoPicker.tsx | 68 +++++++++++++ payload/src/customFields/geoPicker/field.ts | 12 +++ payload/src/payload.config.ts | 16 +++- 10 files changed, 386 insertions(+), 2 deletions(-) create mode 100644 payload/src/collections/Couriers.ts create mode 100644 payload/src/collections/Dispatches.ts create mode 100644 payload/src/collections/Makers.ts create mode 100644 payload/src/collections/Products.ts create mode 100644 payload/src/collections/Retailers.ts create mode 100644 payload/src/customFields/geoPicker/GeoPicker.tsx create mode 100644 payload/src/customFields/geoPicker/field.ts diff --git a/astro/src/components/AddRetailerForm.tsx b/astro/src/components/AddRetailerForm.tsx index 235c25a..2e9ef23 100644 --- a/astro/src/components/AddRetailerForm.tsx +++ b/astro/src/components/AddRetailerForm.tsx @@ -49,7 +49,7 @@ export function AddRetailerForm() { Username - + This is your public display name. diff --git a/astro/src/types.ts b/astro/src/types.ts index 33c061b..92b02cf 100755 --- a/astro/src/types.ts +++ b/astro/src/types.ts @@ -11,6 +11,11 @@ export interface Config { posts: Post; users: User; media: Media; + couriers: Courier; + dispatches: Dispatch; + makers: Maker; + products: Product; + retailers: Retailer; }; globals: {}; } @@ -52,3 +57,58 @@ export interface Media { width?: number; height?: number; } +export interface Courier { + id: string; + name: string; + startingPoint?: string; + destination?: string; + departureDate?: string; + arrivalDate?: string; + weightAllowance?: number; + updatedAt: string; + createdAt: string; +} +export interface Dispatch { + id: string; + dispatchesCode: string; + products?: string[] | Product[]; + startingPoint?: string | Maker; + endPoint?: string | Retailer; + typeOfTransportation?: ('air' | 'car' | 'train' | 'boat')[]; + courier: string | Courier; + timeSensitive?: boolean; + status?: ('routeRequested' | 'inTransit' | 'completed')[]; + updatedAt: string; + createdAt: string; +} +export interface Product { + id: string; + productTitle: string; + updatedAt: string; + createdAt: string; +} +export interface Maker { + id: string; + name: string; + /** + * @minItems 2 + * @maxItems 2 + */ + location?: [number, number]; + products?: string[] | Product[]; + updatedAt: string; + createdAt: string; +} +export interface Retailer { + id: string; + name: string; + /** + * @minItems 2 + * @maxItems 2 + */ + location?: [number, number]; + products?: string[] | Product[]; + salesPoint?: string; + updatedAt: string; + createdAt: string; +} diff --git a/payload/src/collections/Couriers.ts b/payload/src/collections/Couriers.ts new file mode 100644 index 0000000..be81a66 --- /dev/null +++ b/payload/src/collections/Couriers.ts @@ -0,0 +1,45 @@ +import { CollectionConfig } from 'payload/types'; + +const Couriers: CollectionConfig = { + slug: 'couriers', + admin: { + useAsTitle: 'name', + }, + access: { + read: () => true, + }, + fields: [ + { + name: 'name', + type: 'text', + required: true, + }, + { + name: 'startingPoint', + label: 'Traveling from', + type: 'text', // TODO use geopicker here + }, + { + name: 'destination', + label: 'Traveling to', + type: 'text' // TODO user geopicker here + }, + { + name: 'departureDate', + label: 'Departure date', + type: 'date' + }, + { + name: 'arrivalDate', + label: 'Arrival date', + type: 'date' + }, + { + name: 'weightAllowance', + label: 'Weight Allowance (KG)', + type: 'number' + } + ], +}; + +export default Couriers; \ No newline at end of file diff --git a/payload/src/collections/Dispatches.ts b/payload/src/collections/Dispatches.ts new file mode 100644 index 0000000..10f1d9d --- /dev/null +++ b/payload/src/collections/Dispatches.ts @@ -0,0 +1,95 @@ +import { CollectionConfig } from 'payload/types'; + +const Dispatches: CollectionConfig = { + slug: 'dispatches', + admin: { + useAsTitle: 'dispatchesCode', + }, + access: { + read: () => true, + }, + fields: [ + { + name: 'dispatchesCode', + type: 'text', + required: true, + }, + { + name: 'products', + type: 'relationship', + relationTo: 'products', + hasMany: true, + }, + { + name: 'startingPoint', + type: 'relationship', + relationTo: 'makers', + hasMany: false, + }, + { + name: 'endPoint', + type: 'relationship', + relationTo: 'retailers', + hasMany: false, + }, + { + name: 'typeOfTransportation', + type: 'select', + hasMany: true, + options: [ + { + label: 'Air', + value: 'air' + }, + { + label: 'Car', + value: 'car' + }, + { + label: 'Train', + value: 'train' + }, + { + label: 'Boat', + value: 'boat' + } + ] + }, + { + name: 'courier', + type: 'relationship', + relationTo: 'couriers', + hasMany: false, + required: true + }, + { + name: 'timeSensitive', + type: 'checkbox', + }, + { + name: 'status', // required + type: 'select', // required + hasMany: true, + // admin: { + // isClearable: true, + // isSortable: true, // use mouse to drag and drop different values, and sort them according to your choice + // }, + options: [ + { + label: 'Route requested', + value: 'routeRequested', + }, + { + label: 'In transit', + value: 'inTransit', + }, + { + label: 'Completed', + value: 'completed', + }, + ], + } + ], +}; + +export default Dispatches; diff --git a/payload/src/collections/Makers.ts b/payload/src/collections/Makers.ts new file mode 100644 index 0000000..9289fba --- /dev/null +++ b/payload/src/collections/Makers.ts @@ -0,0 +1,34 @@ +import { CollectionConfig } from 'payload/types'; +import { geoPickerField } from "../customFields/geoPicker/field"; + +const Makers: CollectionConfig = { + slug: 'makers', + admin: { + useAsTitle: 'name', + }, + access: { + read: () => true, + }, + fields: [ + { + name: 'name', // required + type: 'text', // required + required: true, + }, + //geoPickerField, is a bit broken right now + { + name: 'location', + type: 'point', + label: 'Location', + }, + { + name: 'products', // required + type: 'relationship', // required + relationTo: 'products', // required + hasMany: true, + // TODO: make the name of the product visible in the dropdown + } + ], +}; + +export default Makers; \ No newline at end of file diff --git a/payload/src/collections/Products.ts b/payload/src/collections/Products.ts new file mode 100644 index 0000000..68e29e5 --- /dev/null +++ b/payload/src/collections/Products.ts @@ -0,0 +1,20 @@ +import { CollectionConfig } from 'payload/types'; + +const Products: CollectionConfig = { + slug: 'products', + admin: { + useAsTitle: 'productTitle', + }, + access: { + read: () => true, + }, + fields: [ + { + name: 'productTitle', // required + type: 'text', // required + required: true, + }, + ], +}; + +export default Products; \ No newline at end of file diff --git a/payload/src/collections/Retailers.ts b/payload/src/collections/Retailers.ts new file mode 100644 index 0000000..8a4333b --- /dev/null +++ b/payload/src/collections/Retailers.ts @@ -0,0 +1,36 @@ +import { CollectionConfig } from 'payload/types'; + +const Retailers: CollectionConfig = { + slug: 'retailers', + admin: { + useAsTitle: 'name', + }, + access: { + read: () => true, + }, + fields: [ + { + name: 'name', // required + type: 'text', // required + required: true, + }, + { + name: 'location', + type: 'point', + label: 'Location', + }, + { + name: 'products', // required + type: 'relationship', // required + relationTo: 'products', // required + hasMany: true, + }, + { + name: 'salesPoint', + type: 'text', + label: 'Where do you plan to sell/exhibit products?' + } + ], +}; + +export default Retailers; \ No newline at end of file diff --git a/payload/src/customFields/geoPicker/GeoPicker.tsx b/payload/src/customFields/geoPicker/GeoPicker.tsx new file mode 100644 index 0000000..3654b0f --- /dev/null +++ b/payload/src/customFields/geoPicker/GeoPicker.tsx @@ -0,0 +1,68 @@ +import * as React from 'react'; +import { useField } from 'payload/components/forms'; + +export const GeoPicker: React.FC<{ path: string }> = ({ path }) => { + const { value, setValue } = useField({ path }); + const [longitude, setLongitude] = React.useState(value[0] || 0); + const [latitude, setLatitude] = React.useState(value[1] || 0); + const [error, setError] = React.useState("") + + const handleCityEnter = async (e) => { + if (e.key === 'Enter') { + try { + const response = await fetch( + `https://nominatim.openstreetmap.org/search?format=json&q=${e.target.value}` + ); + const data = await response.json(); + if (data && data.length > 0) { + const { lat, lon } = data[0]; + setLatitude(lat); + setLongitude(lon); + setValue([lon, lat]); + } + } catch (err) { + setError(e) + console.error('Error fetching geolocation:', e); + } + } + }; + + const handleLatitudeChange = (e) => { + setLatitude(e.target.value); + }; + + const handleLongitudeChange = (e) => { + setLongitude(e.target.value); + }; + + return ( +
+
+ + +
+ {error != "" && +

{error}

+ } +
+
    +
  • + + +
  • +
  • + + +
  • +
+
+
+ ); +}; diff --git a/payload/src/customFields/geoPicker/field.ts b/payload/src/customFields/geoPicker/field.ts new file mode 100644 index 0000000..827eaf6 --- /dev/null +++ b/payload/src/customFields/geoPicker/field.ts @@ -0,0 +1,12 @@ +import { PointField } from 'payload/types'; +import { GeoPicker } from './GeoPicker'; + +export const geoPickerField: PointField = { + name: 'Location', + type: 'point', + admin: { + components: { + Field: GeoPicker, + }, + }, +} \ No newline at end of file diff --git a/payload/src/payload.config.ts b/payload/src/payload.config.ts index dcdbb61..f6c15dc 100644 --- a/payload/src/payload.config.ts +++ b/payload/src/payload.config.ts @@ -3,6 +3,11 @@ import path from "path"; import Posts from "@/collections/Posts"; import Users from "@/collections/Users"; import Media from "@/collections/Media"; +import Couriers from "./collections/Couriers"; +import Dispatches from "./collections/Dispatches"; +import Makers from "./collections/Makers"; +import Products from "./collections/Products"; +import Retailers from "./collections/Retailers"; export default buildConfig({ serverURL: process.env.PAYLOAD_URL, @@ -19,7 +24,16 @@ export default buildConfig({ }, }), }, - collections: [Posts, Users, Media], + collections: [ + Posts, + Users, + Media, + Couriers, + Dispatches, + Makers, + Products, + Retailers, + ], typescript: { outputFile: path.resolve("/", "types.ts"), },