Skip to content

Commit

Permalink
Fix not being able to go to second step and fix data flow between com…
Browse files Browse the repository at this point in the history
…ponents
  • Loading branch information
lemilonkh committed Mar 19, 2024
1 parent b209029 commit 795c5c1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 18 deletions.
65 changes: 47 additions & 18 deletions app/src/app/[lng]/onboarding/setup/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,12 @@ type PopulationEntry = {
datasource_id: string;
};

type OnboardingData = {
name: string;
locode: string;
year: number;
};

const numberOfYearsDisplayed = 10;

/// Finds entry which has the year closest to the selected inventory year
Expand Down Expand Up @@ -110,14 +116,18 @@ function SetupStep({
t,
setValue,
watch,
ocCityData,
setOcCityData,
setData,
}: {
errors: FieldErrors<Inputs>;
register: UseFormRegister<Inputs>;
t: TFunction;
setValue: any;
watch: Function;
setOcCityData: Function;
ocCityData?: OCCityAttributes;
setOcCityData: (cityData: OCCityAttributes) => void;
setData: (data: OnboardingData) => void;
}) {
const currentYear = new Date().getFullYear();
const years = Array.from(
Expand All @@ -142,11 +152,29 @@ function SetupStep({
setOnInputClicked(false);
dispatch(set(city));
setLocode(city.actor_id);
setOcCityData(city);

if (year) {
setData({
name: city.name,
locode: city.actor_id,
year: year!,
});
}

// TODO: chech whether city exists or not
setIsCityNew(true);
};

useEffect(() => {
if (year && ocCityData) {
setData({
name: ocCityData.name,
locode: ocCityData.actor_id,
year: year!,
});
}
}, [year, ocCityData, setData]);

useEffect(() => {
if (!cityInputQuery || cityInputQuery.length === 0) {
setOnInputClicked(false);
Expand All @@ -172,17 +200,15 @@ function SetupStep({
const { data: countryData } = useGetOCCityDataQuery(countryLocode!, {
skip: !countryLocode,
});
const regionLocode = cityData?.data.is_part_of;
const regionLocode = cityData?.is_part_of;
const { data: regionData } = useGetOCCityDataQuery(regionLocode!, {
skip: !regionLocode,
});

// react to API data changes and different year selections
useEffect(() => {
if (cityData && year) {
setOcCityData(cityData);

const population = findClosestYear(cityData?.data.population, year);
const population = findClosestYear(cityData.population, year);
if (!population) {
console.error("Failed to find population data for city");
return;
Expand All @@ -195,7 +221,7 @@ function SetupStep({

useEffect(() => {
if (regionData && year) {
const population = findClosestYear(regionData.data.population, year);
const population = findClosestYear(regionData.population, year);
if (!population) {
console.error("Failed to find population data for region");
return;
Expand All @@ -207,7 +233,7 @@ function SetupStep({

useEffect(() => {
if (countryData && year) {
const population = findClosestYear(countryData.data.population, year);
const population = findClosestYear(countryData.population, year);
if (!population) {
console.error("Failed to find population data for region");
return;
Expand Down Expand Up @@ -667,11 +693,11 @@ export default function OnboardingSetup({
const [addInventory] = useAddInventoryMutation();
const [setUserInfo] = useSetUserInfoMutation();

const [data, setData] = useState<{
name: string;
locode: string;
year: number;
}>({ name: "", locode: "", year: -1 });
const [data, setData] = useState<OnboardingData>({
name: "",
locode: "",
year: -1,
});
const [ocCityData, setOcCityData] = useState<OCCityAttributes>();
const [isConfirming, setConfirming] = useState(false);

Expand Down Expand Up @@ -753,17 +779,18 @@ export default function OnboardingSetup({
const onSubmit: SubmitHandler<Inputs> = async (newData) => {
const year = Number(newData.year);

if (!newData.city || !ocCityData?.actor_id || year < 0 || !data.locode) {
// TODO show user toast? These should be caught by validation logic
return;
}

setData({
name: newData.city,
locode: data.locode!,
year,
});

if (!newData.city || !ocCityData?.actor_id || year < 0 || !data.locode) {
// TODO show user toast? These should normally be caught by validation logic
console.error("Missing data, can't go to next step!");
return;
}

goToNext();
};

Expand All @@ -789,7 +816,9 @@ export default function OnboardingSetup({
setValue={setValue}
register={register}
watch={watch}
ocCityData={ocCityData}
setOcCityData={setOcCityData}
setData={setData}
t={t}
/>
)}
Expand Down
3 changes: 3 additions & 0 deletions app/src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ export const openclimateAPI = createApi({
}),
getOCCityData: builder.query<any, string>({
query: (locode) => `/api/v1/actor/${locode}`,
transformResponse: (response: any) => {
return response.data;
},
}),
}),
});
Expand Down

0 comments on commit 795c5c1

Please sign in to comment.