-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SurveyResponse: add page with survey responses with location on map
- Loading branch information
1 parent
e1ba31c
commit a6f4921
Showing
10 changed files
with
356 additions
and
24 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
111 changes: 111 additions & 0 deletions
111
src/pages/[projectSlug]/surveys/[surveyId]/responses/map/[surveyResponseId].tsx
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,111 @@ | ||
import { BlitzPage, useParam } from "@blitzjs/next" | ||
import { useQuery } from "@blitzjs/rpc" | ||
import { Suspense } from "react" | ||
import { Spinner } from "src/core/components/Spinner" | ||
import { PageHeader } from "src/core/components/pages/PageHeader" | ||
import { H2 } from "src/core/components/text" | ||
import { useSlugs } from "src/core/hooks" | ||
import { LayoutRs, MetaTags } from "src/core/layouts" | ||
import getOperatorsWithCount from "src/operators/queries/getOperatorsWithCount" | ||
import getSubsections from "src/subsections/queries/getSubsections" | ||
import { TMapProps } from "src/survey-public/components/types" | ||
import { | ||
getFeedbackDefinitionBySurveySlug, | ||
getResponseConfigBySurveySlug, | ||
} from "src/survey-public/utils/getConfigBySurveySlug" | ||
import getSurveyResponseTopicsByProject from "src/survey-response-topics/queries/getSurveyResponseTopicsByProject" | ||
import EditableSurveyResponseListItem from "src/survey-responses/components/feedback/EditableSurveyResponseListItem" | ||
import { SurveyFeedbackWithLocationOverviewMap } from "src/survey-responses/components/feedback/SurveyFeedbackWithLocationOverviewMap" | ||
|
||
import getFeedbackResponsesWithLocation from "src/survey-responses/queries/getFeedbackSurveyResponsesWithLocation" | ||
import { SurveyTabs } from "src/surveys/components/SurveyTabs" | ||
import getSurvey from "src/surveys/queries/getSurvey" | ||
|
||
export const SurveyResponseWithLocation = () => { | ||
const { projectSlug, subsectionSlug } = useSlugs() | ||
const surveyId = useParam("surveyId", "number") | ||
const surveyResponseId = useParam("surveyResponseId", "number") | ||
|
||
const [survey] = useQuery(getSurvey, { id: surveyId }) | ||
const [{ surveyResponsesFeedbackPartWithLocation }] = useQuery(getFeedbackResponsesWithLocation, { | ||
projectSlug, | ||
surveyId: survey.id, | ||
}) | ||
const [{ operators }] = useQuery(getOperatorsWithCount, { projectSlug }) | ||
const [{ surveyResponseTopics: topics }, { refetch: refetchTopics }] = useQuery( | ||
getSurveyResponseTopicsByProject, | ||
{ | ||
projectSlug: projectSlug!, | ||
}, | ||
) | ||
const [{ subsections }] = useQuery(getSubsections, { | ||
projectSlug: projectSlug!, | ||
subsectionSlug: subsectionSlug!, | ||
}) | ||
|
||
const { evaluationRefs } = getResponseConfigBySurveySlug(survey.slug) | ||
const feedbackDefinition = getFeedbackDefinitionBySurveySlug(survey.slug) | ||
|
||
const locationRef = evaluationRefs["feedback-location"] | ||
|
||
const mapProps = feedbackDefinition!.pages[0]!.questions.find((q) => q.id === locationRef)! | ||
.props as TMapProps | ||
|
||
const maptilerStyleUrl = mapProps.maptilerStyleUrl | ||
const defaultViewState = mapProps?.config?.bounds | ||
|
||
if (!surveyResponsesFeedbackPartWithLocation?.length) return | ||
|
||
const selectedSurveyResponse = surveyResponsesFeedbackPartWithLocation.find( | ||
(r) => r.id === surveyResponseId, | ||
) | ||
|
||
if (!selectedSurveyResponse) return | ||
|
||
return ( | ||
<> | ||
<MetaTags noindex title={`Beteiligung ${survey.title}`} /> | ||
<PageHeader title={survey.title} className="mt-12" description={<SurveyTabs />} /> | ||
|
||
<div className="mt-12 space-y-4"> | ||
<H2>Beiträge mit Ortsangabe </H2> | ||
|
||
<div className="flex flex-col lg:flex-row gap-2"> | ||
<section className="lg:w-[46%] shrink-0"> | ||
<SurveyFeedbackWithLocationOverviewMap | ||
maptilerStyleUrl={maptilerStyleUrl} | ||
defaultViewState={defaultViewState} | ||
selectedSurveyResponse={selectedSurveyResponse} | ||
surveyResponsesFeedbackPartWithLocation={surveyResponsesFeedbackPartWithLocation} | ||
locationRef={locationRef!} | ||
/> | ||
</section> | ||
<section className="rounded-md drop-shadow-md"> | ||
<EditableSurveyResponseListItem | ||
key={selectedSurveyResponse?.id} | ||
response={selectedSurveyResponse!} | ||
operators={operators} | ||
topics={topics} | ||
subsections={subsections} | ||
refetchResponsesAndTopics={refetchTopics} | ||
/> | ||
</section> | ||
</div> | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
const SurveyResponseWithLocationPage: BlitzPage = () => { | ||
return ( | ||
<LayoutRs> | ||
<Suspense fallback={<Spinner page />}> | ||
<SurveyResponseWithLocation /> | ||
</Suspense> | ||
</LayoutRs> | ||
) | ||
} | ||
|
||
SurveyResponseWithLocationPage.authenticate = true | ||
|
||
export default SurveyResponseWithLocationPage |
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
90 changes: 90 additions & 0 deletions
90
src/survey-responses/components/feedback/SurveyFeedbackWithLocationOverviewMap.tsx
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,90 @@ | ||
import { Routes } from "@blitzjs/next" | ||
import "maplibre-gl/dist/maplibre-gl.css" | ||
import router from "next/router" | ||
import React, { useState } from "react" | ||
import Map, { LngLatBoundsLike, Marker, NavigationControl, useMap } from "react-map-gl/maplibre" | ||
import { BackgroundSwitcher, LayerType } from "src/core/components/Map/BackgroundSwitcher" | ||
import SurveyStaticPin from "src/core/components/Map/SurveyStaticPin" | ||
|
||
type Props = { | ||
maptilerStyleUrl: string | ||
defaultViewState?: LngLatBoundsLike | ||
selectedSurveyResponse: any | ||
surveyResponsesFeedbackPartWithLocation: any[] | ||
locationRef: number | ||
} | ||
|
||
export const SurveyFeedbackWithLocationOverviewMap: React.FC<Props> = ({ | ||
maptilerStyleUrl, | ||
defaultViewState, | ||
selectedSurveyResponse, | ||
locationRef, | ||
surveyResponsesFeedbackPartWithLocation, | ||
}) => { | ||
const [selectedLayer, setSelectedLayer] = useState<LayerType>("vector") | ||
|
||
const handleLayerSwitch = (layer: LayerType) => { | ||
setSelectedLayer(layer) | ||
} | ||
|
||
const maptilerApiKey = "ECOoUBmpqklzSCASXxcu" | ||
|
||
const vectorStyle = `${maptilerStyleUrl}?key=${maptilerApiKey}` | ||
const satelliteStyle = `https://api.maptiler.com/maps/hybrid/style.json?key=${maptilerApiKey}` | ||
|
||
const handleSelect = (responseId: number) => { | ||
void router.push( | ||
Routes.SurveyResponseWithLocationPage({ | ||
projectSlug: "rs8", | ||
surveyId: 1, | ||
surveyResponseId: responseId, | ||
}), | ||
undefined, | ||
{ scroll: false }, | ||
) | ||
} | ||
|
||
return ( | ||
<div className="h-[100vh] overflow-clip rounded-md drop-shadow-md"> | ||
<Map | ||
id="mainMap" | ||
initialViewState={{ | ||
bounds: defaultViewState, | ||
}} | ||
scrollZoom={false} | ||
mapStyle={selectedLayer === "vector" ? vectorStyle : satelliteStyle} | ||
// @ts-expect-error: See https://github.com/visgl/react-map-gl/issues/2310 | ||
RTLTextPlugin={null} | ||
> | ||
{surveyResponsesFeedbackPartWithLocation.map((r) => ( | ||
<Marker | ||
key={r.id} | ||
draggable={false} | ||
style={{ cursor: "pointer" }} | ||
longitude={r.data[locationRef].lng} | ||
latitude={r.data[locationRef].lat} | ||
anchor="bottom" | ||
onClick={() => handleSelect(r.id)} | ||
> | ||
<SurveyStaticPin light /> | ||
</Marker> | ||
))} | ||
<Marker | ||
key={selectedSurveyResponse.id} | ||
draggable={false} | ||
longitude={selectedSurveyResponse.data[locationRef].lng} | ||
latitude={selectedSurveyResponse.data[locationRef].lat} | ||
anchor="bottom" | ||
> | ||
<SurveyStaticPin /> | ||
</Marker> | ||
<BackgroundSwitcher | ||
className="absolute left-4 top-4" | ||
value={selectedLayer} | ||
onChange={handleLayerSwitch} | ||
/> | ||
<NavigationControl showCompass={false} /> | ||
</Map> | ||
</div> | ||
) | ||
} |
Oops, something went wrong.