Skip to content

Commit

Permalink
add date range
Browse files Browse the repository at this point in the history
  • Loading branch information
sevenreup committed Jul 16, 2024
1 parent c9a8f22 commit 3152bf1
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 36 deletions.
29 changes: 26 additions & 3 deletions src/app/(dashboard)/stats/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createPatientFilters,
createQuestionnaireResponseFilters,
} from "./filters";
import { eachDayOfInterval } from "@/lib/utils";

export async function fetchRequiredData() {
const locationQuery = paramGenerator("/Location", {
Expand All @@ -30,10 +31,10 @@ export async function fetchData(formData: FormData) {
) as FilterFormData;

console.log(JSON.stringify(data));
let rawDate: string | null = null;
let rawDate: string | string[] | null = null;
const baseFilter = data.filters.map((filter) => {
const temp: Record<string, string> = {};

if (filter.template == "_tag_location") {
console.log(filter);
const template = `http://smartregister.org/fhir/location-tag|${
Expand All @@ -47,14 +48,36 @@ export async function fetchData(formData: FormData) {
rawDate =
filter.params.find((e) => e.name == "date")?.value?.split("T")[0] ??
null;
} else if (filter.template == "dateRange") {
const value = filter.params[0].value;
if (value) {
const { from, to } = value as any;
console.log(from, to);

if (from && to) {
const start = new Date(from.split("T")[0]);
const end = new Date(to.split("T")[0]);
console.log({ start, end });

rawDate = eachDayOfInterval({
start,
end,
}).map((e) => {
console.log(e);

return e.toISOString().split("T")[0];
});
} else if (from) {
rawDate = [from.split("T")[0]];
}
}
}
return temp;
});

if (rawDate) {
rawDate = fixDate(rawDate);
}


const bundle = await fetchBundle([
createQuestionnaireResponseFilters(
Expand Down
12 changes: 10 additions & 2 deletions src/app/(dashboard)/stats/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ type Props = {};
const Content = (props: Props) => {
const { data } = useGenericContext();
const { summaries, date } = data as SummaryResponse;

return (
<div>
{date && <h3>Results for: {date.split("T")[0]}</h3>}
{date && <h3>Results for: {formatDate(date)}</h3>}
{summaries.map((summary) => (
<div key={summary.name} className="card w-full bg-base-100 shadow-xl">
<div className="card-body">
Expand All @@ -28,4 +28,12 @@ const Content = (props: Props) => {
);
};

const formatDate = (date: string | string[]) => {
if (typeof date === "string") {
return date.split("T")[0];
} else {
return date.map((d) => format(d, "dd/MM/yyyy")).join(", ");
}
};

export default Content;
50 changes: 39 additions & 11 deletions src/app/(dashboard)/stats/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type PatientType =

export const createQuestionnaireResponseFilters = (
questionnaire: string,
date: string | null,
date: string | string[] | null,
baseFilter: Record<string, string>[],
hasCount = true
) => {
Expand All @@ -22,19 +22,33 @@ export const createQuestionnaireResponseFilters = (
query.fromArray(baseFilter);

query.remove("date");
query.remove("dateRange");

if (date) {
query.add(
"_tag",
`https://d-tree.org/fhir/created-on-tag|${format(date, "dd/MM/yyyy")}`
);
if (typeof date === "string") {
date = [date];
} else {
query.add(
"_tag",
date
.map(
(d) =>
`https://d-tree.org/fhir/created-on-tag|${format(
d,
"dd/MM/yyyy"
)}`
)
.join(",")
);
date.forEach((d) => {});
}
}
return query.toUrl("/QuestionnaireResponse");
};


export const createPatientFilters = (
types: PatientType[] | undefined = undefined,
date: string | null,
date: string | string[] | null,
baseFilter: Record<string, string>[]
) => {
const query = new QueryParam({
Expand All @@ -43,11 +57,25 @@ export const createPatientFilters = (
query.fromArray(baseFilter);

query.remove("date");
query.remove("dateRange");
if (date) {
query.add(
"_tag",
`https://d-tree.org/fhir/created-on-tag|${format(date, "dd/MM/yyyy")}`
);
if (typeof date === "string") {
date = [date];
} else {
query.add(
"_tag",
date
.map(
(d) =>
`https://d-tree.org/fhir/created-on-tag|${format(
d,
"dd/MM/yyyy"
)}`
)
.join(",")
);
date.forEach((d) => {});
}
}
if (types) {
query.add(
Expand Down
17 changes: 4 additions & 13 deletions src/app/(dashboard)/stats/model.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { randomInt } from "crypto";
import { addDays } from "date-fns";

export const fixDate = (date: string) => {
export const fixDate = (date: string | string[]) => {
return date;
// return addDays(date, 1).toISOString();
}
};

export class QueryParam {
queries: Map<string, string> = new Map();
Expand All @@ -13,8 +10,6 @@ export class QueryParam {
}

add(key: string, value: any) {
console.log(key,this.queries.has(key) );

if (this.queries.has(key)) {
this.queries.set(`${key}[${Math.random()}]`, value);
} else {
Expand Down Expand Up @@ -48,13 +43,9 @@ export class QueryParam {
for (const key in values) {
this.from(values[key]);
}
console.log(values);
console.log(this.queries);

}

toUrl( resources: string,) {
console.log(this.queries);
toUrl(resources: string) {
const query = Array.from(this.queries)
.map(([key, value]) => {
if (key.includes("[")) {
Expand All @@ -63,6 +54,6 @@ export class QueryParam {
return `${key}=${value}`;
})
.join("&");
return `${resources}?${query}`;
return `${resources}?${query}`;
}
}
9 changes: 8 additions & 1 deletion src/components/filters/filter-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,14 @@ const GetInput = ({
mode="range"
selected={field.value}
onSelect={(date) => {
field.onChange(date);
if (date) {
field.onChange({
from: date.from ? formatISO(date.from) : undefined,
to: date.to ? formatISO(date.to) : undefined,
});
} else {
field.onChange(null);
}
}}
// disabled={(date) =>
// date > new Date() || date < new Date("1900-01-01")
Expand Down
2 changes: 1 addition & 1 deletion src/lib/models/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export type LocationData = {

export type SummaryResponse = {
summaries: SummaryItem[],
date: string | null
date: string | string[] | null
}

export type SummaryItem = {
Expand Down
38 changes: 34 additions & 4 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"

import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
import { addDays, subMinutes, startOfDay } from "date-fns";

export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
return twMerge(clsx(inputs));
}

export function eachDayOfInterval({
start,
end = new Date(Date.now()),
}: {
start: Date;
end?: Date;
}) {
const offset = {
start: start.getTimezoneOffset(),
end: end.getTimezoneOffset(),
};

// Adjust start and end dates for timezone offset to use start of UTC date
const adjusted = {
start: subMinutes(startOfDay(start.getTime()), offset.start),
end: subMinutes(startOfDay(end.getTime()), offset.end),
};

let accumulator = adjusted.start;
const days: Date[] = [];

while (end >= accumulator) {
days.push(accumulator);
accumulator = addDays(accumulator, 1);
}

return days;
}
2 changes: 1 addition & 1 deletion src/model/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const statsFilters: Filter[] = [
{
id: "filter-by-date-rage",
name: "Search by date range",
template: "date",
template: "dateRange",
isObject: true,
params: [
{
Expand Down

0 comments on commit 3152bf1

Please sign in to comment.