acikyazilimagi / deprem-yardim-frontend

release canditate: https://rc.afetharita.com/
https://afetharita.com/
Apache License 2.0
898 stars 284 forks source link

refactor: useVerifiedLocations and types #1175

Closed eraygundogmus closed 1 year ago

eraygundogmus commented 1 year ago

Even if we see many function below ,

const transformHospitalResponse: RT<HospitalResponse, HospitalData> = (res) => {
  return {
    channel: "hastane",
    geometry: createGeometry(res),
    properties: {
      name: res.extraParams?.name ?? null,
      icon: "images/icon-10.png",
      city: res.extraParams?.il ?? null,
      description: null,
    },
    reference: res.entry_id ?? null,
  };
};

const transformTeleteyitResponse: RT<TeleteyitResponse, TeleteyitData> = (
  res
) => {
  return {
    channel: "teleteyit",
    geometry: createGeometry(res),
    properties: {
      name: res.extraParams?.["isim-soyisim"] ?? null,
      description: res.extraParams?.aciklama ?? null,
      icon: "images/icon-14.png",
      reason: res.reason ?? null,
      city: res.extraParams?.il ?? null,
      district: res.extraParams?.ilce ?? null,
      status: res.extraParams?.durum ?? null,
    },
    reference: res.entry_id ?? null,
  };
};

const transformSatelliteResponse: RT<SatelliteResponse, SatelliteData> = (
  res
) => {
  return {
    channel: "uydu",
    geometry: createGeometry(res),
    properties: {
      damage: res.extraParams?.damage ?? null,
      verified: res.is_location_verified ?? false,
      icon: "images/icon-13.png",
      name: null,
      description: null,
    },
    reference: res.entry_id ?? null,
  };
};

const transformSahraResponse: RT<SahraResponse, SahraData> = (res) => {
  return {
    channel: "sahra",
    geometry: createGeometry(res),
    properties: {
      name: res.extraParams?.name ?? null,
      reason: res.reason ?? null,
      icon: res.extraParams?.icon ?? null,
      verified: res.is_location_verified ?? false,
      description: null,
    },
    reference: res.entry_id ?? null,
  };
};

const transformPharmacyResponse: RT<PharmacyResponse, PharmacyData> = (res) => {
  return {
    channel: "eczane",
    geometry: createGeometry(res),
    properties: {
      name: res.extraParams?.name ?? null,
      reason: res.reason ?? null,
      icon: "images/icon-15.png",
      verified: res.is_location_verified ?? false,
      description: null,
    },
    reference: res.entry_id ?? null,
  };
};

const transformSafePlaceResponse: RT<SafePlaceResponse, SafePlaceData> = (
  res
) => {
  return {
    channel: "guvenli",
    geometry: createGeometry(res),
    properties: {
      description: res.extraParams?.description ?? null,
      icon: "images/icon-16.png",
      reason: res.reason ?? null,
      name: res.extraParams?.name ?? null,
      verified: res.is_location_verified ?? false,
    },
    reference: res.entry_id ?? null,
  };
};

There is only one in a generic way.

const transformFunction = (channel, res) => {
  channel,
  geometry: ...
  ...
}

And why don't we use enums for API channels?

export type APIChannel =
  | "ahbap_location"
  | "sicak_yemek"
  | "hastahane_locations"
  | "teleteyit"
  | "uydu"
  | "sahra_mutfak"
  | "turk_eczane"
  | "eczane_excel"
  | "guvenli_yerler_oteller"
  | "twitter"
  | "teyit_enkaz"
  | "babala"; 

Let me explain in an easier way.

enum Channels {
  A = 'verified',
  B= 'unverified',
  C = 'govern'
  ...
}

Now remember the generic transform function above.

const transformers: Record<APIChannel, RT> = Channels.map((channel) => {
// now it is impossible to use any channel expect inside enums.
   transformFunction(channel, res)
   })

 Then remove unused 200 line...

usirin commented 1 year ago

I am OK with having explicit transformers with each one as we had to battle with all the different responses we got from all the other channels. I am thinking about reaching out to the data/backend team to make it unified across all the responses so that we can do the generalization i am talking about.

For example, this gives us flexibility to not have an icon for a channel type and return a null until we have the image uploaded, etc.

Though i agree, there are some improvements we can make to make this easier to grasp/use.

absolutezero13 commented 1 year ago

I've tried to come up with a solution but both our object shapes and API response shapes vary a lot there's just no point to make it generic since you have to write bunch of if statements.