Skip to content

Commit

Permalink
feat: read map config file directly from server
Browse files Browse the repository at this point in the history
  • Loading branch information
navid-kalaei committed May 24, 2024
1 parent a2c5d27 commit 5e7ac55
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 74 deletions.
48 changes: 0 additions & 48 deletions pages/_documents.tsx

This file was deleted.

71 changes: 55 additions & 16 deletions pages/api/v0/maps/[project]/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,65 @@ import path from 'path'
import { NextApiRequest, NextApiResponse } from 'next'

import MapPageConfigs from '../../../../../../dtos/MapPageConfigs'
import { MapColorModes } from '../../../../../../components/MapColorStyle'


const getPath = (project: string = 'main'): string => {
return `./public/projects/${project}/config.json`
}


export const parseConfigFile = (project: string = 'main'): MapPageConfigs => {
let mapPageConfigs: MapPageConfigs = {
"map": {
"location": {
"lat": 50.8129,
"lng": 5.6030,
"zoom": 6
},
"colorStyle": MapColorModes.GRAY
},
"popularTags": {
"min_count": 2
},
"sidebar": {
"title": "Kartevonmorgen"
}
}

let fileContent: string = ''
try {
fileContent = fs.readFileSync(
path.resolve(getPath(project as string)),
'utf8',
)
} catch (e) {
console.error('api map config: failed to read config file for project: ', project)
console.error(e)
try {
console.log('api map config: trying to read default config file')
fileContent = fs.readFileSync(
path.resolve(getPath()),
'utf8',
)
} catch (e) {
console.error('api map config: failed to read default config file')
console.error(e)
}
}

try {
mapPageConfigs = JSON.parse(fileContent.toString())
} catch(e) {
console.error('api map config: failed to parse config file')
console.error(e)
}

console.log(mapPageConfigs)
return mapPageConfigs
}


export default (req: NextApiRequest, res: NextApiResponse) => {
const {
query: { project },
Expand All @@ -23,22 +76,8 @@ export default (req: NextApiRequest, res: NextApiResponse) => {
return
}

// todo: eighter move it to env or consts
// todo: catch the error if the file is not found

let fileContent: string = ''
try {
fileContent = fs.readFileSync(
path.resolve(getPath(project as string)),
'utf8',
)
} catch (e) {
fileContent = fs.readFileSync(
path.resolve(getPath()),
'utf8',
)
}
const mapPageConfigs: MapPageConfigs = JSON.parse(fileContent.toString())

const mapPageConfigs: MapPageConfigs = parseConfigFile(project as string)

res
.status(200)
Expand Down
13 changes: 3 additions & 10 deletions pages/m/[...slug].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { MapLocationProps } from '../../components/Map'
import { TagsCount } from '../../dtos/TagCount'
import Sidebar from '../../components/Sidebar'
import { MapColorModes } from '../../components/MapColorStyle'
import { parseConfigFile } from '../api/v0/maps/[project]/config'


const { Content } = Layout
Expand Down Expand Up @@ -81,16 +82,8 @@ export const getServerSideProps: GetServerSideProps = async (ctx) => {

// we expect to have path always not empty with the first element of project name
const project = path[0]

// set configs
const pageConfigsReq = await AxiosInstance.GetRequest<MapPageConfigs>(
API_ENDPOINTS.getMapPageConfigs(project),
{
timeout: 120000,
}
)

const pageConfigs = AxiosInstance.GetSuccessData(pageConfigsReq)

const pageConfigs = parseConfigFile(project as string)

const mapLocationProps = pageConfigs.map.location
const sidebarConfigs = pageConfigs.sidebar
Expand Down

0 comments on commit 5e7ac55

Please sign in to comment.