Commit f028409a authored by Labros Papadopoulos's avatar Labros Papadopoulos
Browse files

fix: geocode api key passing value

parent 442b613e
Loading
Loading
Loading
Loading
Loading

actions/geocode.ts

0 → 100644
+31 −0
Original line number Diff line number Diff line
'use server';

import { geocode } from 'opencage-api-client';

export interface GeoCoordinates {
  lat: number;
  lng: number;
}

export async function geocodeAddress(address: string): Promise<GeoCoordinates> {
  const geocodeApiKey = process.env.GEOCODE_API_KEY;

  if (!geocodeApiKey) {
    throw new Error('GEOCODE_API_KEY is not set on the server');
  }

  const geocodeResponse = await geocode({
    key: geocodeApiKey,
    q: address,
    language: 'en',
    limit: 1,
    no_annotations: 1,
  });

  if (!geocodeResponse || geocodeResponse.results.length === 0) {
    throw new Error('No geocoding results found for the provided address.');
  }

  const { lat, lng } = geocodeResponse.results[0].geometry;
  return { lat, lng };
}
+2 −16
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ import {
  getCategoriesNames,
} from '@/actions/oss';
import { ServiceSpecification } from '@/services/tmf633';
import { geocode } from 'opencage-api-client';
import { geocodeAddress } from '@/actions/geocode';
import { Organization, OrganizationCreate } from '@/services/tmf632';
import { IoEye, IoEyeOff } from 'react-icons/io5';

@@ -44,7 +44,6 @@ const schema = z.object({
});

type FormData = z.infer<typeof schema>;
const geocodeApiKey = process.env.GEOCODE_API_KEY;
const PlatformOssAddModal: React.FC<PlatformOssAddModalProps> = ({
  isOpen = false,
  onClose,
@@ -96,20 +95,7 @@ const PlatformOssAddModal: React.FC<PlatformOssAddModalProps> = ({

  const handleNextStep = async (data: FormData) => {
    try {
      const geocodeResponse = await geocode({
        key: geocodeApiKey, // Replace with API key
        q: data.GEOGRAPHIC_ADDRESS,
        language: 'en',
        limit: 1,
        no_annotations: 1,
      });

      if (!geocodeResponse || geocodeResponse.results.length === 0) {
        console.error('No geocoding results found for the provided address.');
        return;
      }

      const { lat, lng } = geocodeResponse.results[0].geometry;
      const { lat, lng } = await geocodeAddress(data.GEOGRAPHIC_ADDRESS);
      const organizationData = {
        name: data.NAME,
        tradingName: '',