Skip to content

Commit c7ff5c8

Browse files
committed
stashing for the night -- new direction client side sorting
1 parent ebd9dcf commit c7ff5c8

File tree

13 files changed

+153
-30
lines changed

13 files changed

+153
-30
lines changed

.eslintrc.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ module.exports = {
3131
],
3232
"react/display-name": ["off", { ignoreTranspilerName: true }],
3333
"sort-imports": "error",
34+
"max-len": ["warn", {
35+
code: 100,
36+
ignoreComments: true,
37+
ignoreStrings: true,
38+
ignoreTemplateLiterals: true,
39+
tabWidth: 4,
40+
}],
41+
3442
},
3543
settings: {
3644
react: {

packages/common/src/colors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const colors: Record<string, string> = {
88
greyDark: "#3c3c3c",
99
greyMedium: "#707070",
1010
greyLight: "#A2A2A2",
11-
lavendar: "#9F6CBA",
11+
lavender: "#9F6CBA",
1212
orangeDark: "#CE5A30",
1313
orangePrimary: "#F05A28",
1414
orangeSecondary: "#DB5427",

packages/native/src/App.styles.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export const colors = {
88
greyDark: "#3c3c3c",
99
greyMedium: "#707070",
1010
greyLight: "#A2A2A2",
11-
lavendar: "#9F6CBA",
11+
lavender: "#9F6CBA",
1212
orangeDark: "#CE5A30",
1313
orangePrimary: "#F05A28",
1414
orangeSecondary: "#DB5427",

packages/native/src/components/Categories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ const categories: Record<TCategoryName, any> = {
101101
],
102102
},
103103
JobTraining: {
104-
color: "lavendar",
104+
color: "lavender",
105105
mainCategory: {
106106
text: "Job Training",
107107
query: "CATEGORY-jobTraining",

packages/native/src/components/HomeButtons.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const routerLinkButtons: THomeButtonRouterLink[] = [
7070
text: "Job Training",
7171
icon: BusinessCenterIcon,
7272
linkState: "/job-training",
73-
color: colors.lavendar,
73+
color: colors.lavender,
7474
},
7575
{
7676
text: "Social Services",

packages/server/src/bin/setupCategories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ const categories: {
4949
],
5050
},
5151
{
52-
color: "lavendar",
52+
color: "lavender",
5353
name: "Job Training",
5454
stub: "job_training",
5555
subcategories: [

packages/server/src/models/Category.ts

Lines changed: 87 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,25 @@ export interface TCategoryDocument extends Document {
2121
export async function categoryDocumentToCategory(
2222
d: TCategoryDocument
2323
): Promise<TCategory | null> {
24-
let c = d;
25-
if (d.toObject) {
26-
c = d.toObject();
27-
} else {
24+
if (!d || !d.toObject) {
2825
// console.warn(
2926
// `\`categoryToDocumentCategory\` received category which does not appear to be a Mongoose Document [${Object.keys(
3027
// d
3128
// )}]:\n${JSON.stringify(d, null, 2)}`
3229
// );
33-
if (d.hasOwnProperty("_bsontype")) {
34-
// console.warn("This appears to be an ObjectId");
35-
// console.trace();
36-
return null;
37-
}
30+
return null;
31+
} else if (d.hasOwnProperty("_bsontype")) {
32+
// console.warn("This appears to be an ObjectId");
33+
// console.trace();
34+
return null;
3835
}
3936

4037
const result = {
41-
...c,
42-
_id: c._id.toHexString(),
38+
...d,
39+
_id: d._id.toHexString(),
4340
subcategories: (
4441
await Promise.all(
45-
((c.subcategories || []) as TSubcategoryDocument[]).map(
42+
((d.subcategories || []) as TSubcategoryDocument[]).map(
4643
subcategoryDocumentToSubcategory
4744
)
4845
)
@@ -110,6 +107,72 @@ CategorySchema.statics.getByStub = async function(
110107
return result;
111108
};
112109

110+
CategorySchema.statics.getByStubLocation = async function(
111+
stub: string,
112+
options: {
113+
includeDeleted?: boolean;
114+
latitude: number;
115+
longitude: number;
116+
}
117+
): Promise<TCategoryDocument | null> {
118+
const { includeDeleted = false, latitude, longitude } = options;
119+
120+
const match: {
121+
stub: string;
122+
deleted?: { $ne: true };
123+
} = { stub };
124+
125+
if (!includeDeleted) {
126+
match.deleted = { $ne: true };
127+
}
128+
129+
const result = await this.aggregate([
130+
{ $match: match },
131+
{ $limit: 1 },
132+
{
133+
$lookup: {
134+
from: "subcategories",
135+
as: "subcategories",
136+
let: { subcategories: "$subcategories" },
137+
pipeline: [
138+
{
139+
$lookup: {
140+
localField: "subcategories",
141+
foreignField: "_id",
142+
143+
from: "subcategories",
144+
as: "resources",
145+
146+
// let: { resources: "$resources" },
147+
// pipeline: [{
148+
// $geoNear: {
149+
// near: {
150+
// type: "Point",
151+
// coordinates: [longitude, latitude],
152+
// },
153+
// spherical: true,
154+
// distanceField: "dist",
155+
// },
156+
// }],
157+
},
158+
},
159+
],
160+
},
161+
},
162+
{
163+
$unwind: {
164+
path: `$subcategories`,
165+
preserveNullAndEmptyArrays: false,
166+
},
167+
},
168+
]);
169+
170+
if (result.length) {
171+
return result[0];
172+
}
173+
return null;
174+
};
175+
113176
/**
114177
* Creates or finds an existing subcategory by its name and adds
115178
* it as a child of this category
@@ -137,5 +200,16 @@ export default Category as typeof Category & {
137200
color?: string
138201
) => Promise<TCategoryDocument>;
139202
getCategoryList: () => Promise<TCategoryDocument[]>;
140-
getByStub: (stub: string) => Promise<TCategoryDocument | null>;
203+
getByStub: (
204+
stub: string,
205+
includeDeletedResources?: boolean
206+
) => Promise<TCategoryDocument | null>;
207+
getByStubLocation: (
208+
stub: string,
209+
options: {
210+
includeDeletedResources?: boolean;
211+
latitude: number;
212+
longitude: number;
213+
}
214+
) => Promise<TCategoryDocument | null>;
141215
};

packages/server/src/models/Resource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface TResourceDocument extends Document {
3636
lastModifiedAt: Date;
3737
lastModifiedBy: ObjectId | undefined; // always populate
3838
legacyId: string | null | undefined;
39-
location: { type: string; coordinates: number[] };
39+
location: { type: "Point"; coordinates: number[] };
4040
name: string;
4141
phone: string;
4242
schedule: TResourceScheduleData;

packages/server/src/routes/api/category/[stub].ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,33 @@ import Category, { categoryDocumentToCategory } from "../../../models/Category";
33
export async function get(req, res, _next) {
44
const { stub } = req.params;
55

6-
const categoryDocument = await Category.getByStub(stub);
7-
8-
if (categoryDocument) {
9-
res.status(200).json({
10-
category: await categoryDocumentToCategory(categoryDocument),
11-
});
12-
} else {
13-
res.status(404).json({ message: `Category ${stub} not found` });
6+
let categoryDocument;
7+
try {
8+
if (req.query.latitude && req.query.longitude) {
9+
categoryDocument = await Category.getByStubLocation(stub, {
10+
latitude: parseInt(req.query.latitude, 10),
11+
longitude: parseInt(req.query.longitude, 10),
12+
});
13+
} else {
14+
categoryDocument = await Category.getByStub(stub);
15+
}
16+
17+
if (categoryDocument) {
18+
console.log(
19+
`\n categoryDocument:\n\n\n\t${JSON.stringify(categoryDocument)}\n\n`
20+
);
21+
22+
res.status(200).json({
23+
category: await categoryDocumentToCategory(categoryDocument),
24+
categoryDocument,
25+
});
26+
} else {
27+
res.status(404).json({ message: `Category ${stub} not found` });
28+
}
29+
} catch (e) {
30+
console.error(e);
31+
return res
32+
.status(500)
33+
.json({ message: `Error retrieving category: ${e.message}` });
1434
}
1535
}

packages/web/src/components/Categories.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export const categories: Record<TCategoryName, TCategoryDefinition> = {
119119
],
120120
},
121121
JobTraining: {
122-
color: "lavendar",
122+
color: "lavender",
123123
placeholder: BusinessCenterIcon,
124124
mainCategory: {
125125
translationKey: "jobTraining",

packages/web/src/components/HomeButtons.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export const routerLinkButtons: THomeButtonRouterLink[] = [
101101
linkProps: {
102102
to: "/job_training",
103103
},
104-
color: colors.lavendar,
104+
color: colors.lavender,
105105
},
106106
{
107107
text: "Social Services",

packages/web/src/components/useResourcesByCategory.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
import { TResource } from "@upswyng/types";
22
import { TResourcesByCategoryPayload } from "../webTypes";
33
import apiClient from "../utils/apiClient";
4+
import { getUserCoordinates } from "../utils/location";
45
import { useQuery } from "react-query";
56

67
const getResourcesByCategory = async (
78
_queryKey: string,
89
params: { category: string }
910
): Promise<TResourcesByCategoryPayload> => {
11+
console.log(`getResourcesByCategory`);
12+
13+
const userPosition = await getUserCoordinates();
14+
1015
const { data } = await apiClient.get<TResourcesByCategoryPayload>(
11-
`/category/${params.category}`
16+
`/category/${params.category}`,
17+
{
18+
params: {
19+
latitude: userPosition.latitude,
20+
longitude: userPosition.longitude,
21+
},
22+
}
1223
);
1324

1425
if (!data.category) {

packages/web/src/utils/location.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const getUserCoordinates = async () => {
2+
const pos = (await new Promise((resolve, reject) => {
3+
navigator.geolocation.getCurrentPosition(resolve, reject);
4+
})) as Position;
5+
6+
return {
7+
longitude: pos.coords.longitude,
8+
latitude: pos.coords.latitude,
9+
};
10+
};

0 commit comments

Comments
 (0)